Skip to content

Commit

Permalink
fix(runtime): clear up detached hostRefs and rootAppliedStyles
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-bromann committed Jan 7, 2025
1 parent 22684f3 commit 7f45919
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
20 changes: 18 additions & 2 deletions src/runtime/disconnected-callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import { getHostRef, plt } from '@platform';

import type * as d from '../declarations';
import { PLATFORM_FLAGS } from './runtime-constants';
import { rootAppliedStyles } from './styles';
import { safeCall } from './update-component';

const disconnectInstance = (instance: any) => {
const callbackResult: unknown[] = [];
if (BUILD.lazyLoad && BUILD.disconnectedCallback) {
safeCall(instance, 'disconnectedCallback');
callbackResult.push(safeCall(instance, 'disconnectedCallback'));
}
if (BUILD.cmpDidUnload) {
safeCall(instance, 'componentDidUnload');
callbackResult.push(safeCall(instance, 'componentDidUnload'));
}
};

Expand All @@ -33,4 +35,18 @@ export const disconnectedCallback = async (elm: d.HostElement) => {
hostRef.$onReadyPromise$.then(() => disconnectInstance(hostRef.$lazyInstance$));
}
}

/**
* Remove the element from the `rootAppliedStyles` WeakMap
*/
if(rootAppliedStyles.has(elm)) {
rootAppliedStyles.delete(elm);
}

/**
* Remove the shadow root from the `rootAppliedStyles` WeakMap
*/
if(elm.shadowRoot && rootAppliedStyles.has(elm.shadowRoot as unknown as Element)) {
rootAppliedStyles.delete(elm.shadowRoot as unknown as Element);
}
};
2 changes: 1 addition & 1 deletion src/runtime/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type * as d from '../declarations';
import { createTime } from './profile';
import { HYDRATED_STYLE_ID, NODE_TYPE, SLOT_FB_CSS } from './runtime-constants';

const rootAppliedStyles: d.RootAppliedStyleMap = /*@__PURE__*/ new WeakMap();
export const rootAppliedStyles: d.RootAppliedStyleMap = /*@__PURE__*/ new WeakMap();

/**
* Register the styles for a component by creating a stylesheet and then
Expand Down

0 comments on commit 7f45919

Please sign in to comment.