Skip to content

Commit

Permalink
remove WeapMap implementation from hydration and testing
Browse files Browse the repository at this point in the history
  • Loading branch information
danielleroux committed Feb 13, 2025
1 parent f727432 commit 6424713
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 33 deletions.
19 changes: 13 additions & 6 deletions src/hydrate/platform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,22 @@ export const supportsListenerOptions = false;

export const supportsConstructableStylesheets = false;

const hostRefs: WeakMap<d.RuntimeRef, d.HostRef> = new WeakMap();
export const getHostRef = (ref: d.RuntimeRef) => {
if (ref.__stencil__getHostRef) {
return ref.__stencil__getHostRef();
}

export const getHostRef = (ref: d.RuntimeRef) => hostRefs.get(ref);
export const deleteHostRef = (ref: d.RuntimeRef) => hostRefs.delete(ref);
return undefined;
};

export const registerInstance = (lazyInstance: any, hostRef: d.HostRef) => {
const ref = hostRefs.set((hostRef.$lazyInstance$ = lazyInstance), hostRef);
lazyInstance.__stencil__getHostRef = () => hostRef;
hostRef.$lazyInstance$ = lazyInstance;

if (BUILD.modernPropertyDecls && (BUILD.state || BUILD.prop)) {
reWireGetterSetter(lazyInstance, hostRef);
}
return ref;
return hostRef;
};

export const registerHost = (elm: d.HostElement, cmpMeta: d.ComponentRuntimeMeta) => {
Expand All @@ -152,7 +157,9 @@ export const registerHost = (elm: d.HostElement, cmpMeta: d.ComponentRuntimeMeta
hostRef.$onReadyPromise$ = new Promise((r) => (hostRef.$onReadyResolve$ = r));
elm['s-p'] = [];
elm['s-rc'] = [];
return hostRefs.set(elm, hostRef);
elm.__stencil__getHostRef = () => hostRef;

return hostRef;
};

export const Build: d.UserBuildConditionals = {
Expand Down
2 changes: 1 addition & 1 deletion src/testing/platform/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export { Build } from './testing-build';
export { modeResolutionChain, styles } from './testing-constants';
export { deleteHostRef, getHostRef, registerHost, registerInstance } from './testing-host-ref';
export { getHostRef, registerHost, registerInstance } from './testing-host-ref';
export { consoleDevError, consoleDevInfo, consoleDevWarn, consoleError, setErrorHandler } from './testing-log';
export {
isMemberInElement,
Expand Down
5 changes: 0 additions & 5 deletions src/testing/platform/testing-constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,3 @@ export const queuedLoadModules: QueuedLoadModule[] = [];
* A collection of errors that were detected to surface during the rendering process
*/
export const caughtErrors: Error[] = [];
/**
* A mapping of runtime references to HTML elements to the data structure Stencil uses to track the element alongside
* additional metadata
*/
export const hostRefs = new Map<d.RuntimeRef, d.HostRef>();
27 changes: 8 additions & 19 deletions src/testing/platform/testing-host-ref.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,26 @@
import type * as d from '@stencil/core/internal';

import { hostRefs } from './testing-constants';

/**
* Retrieve the data structure tracking the component by its runtime reference
* @param elm the reference to the element
* @returns the corresponding Stencil reference data structure, or undefined if one cannot be found
*/
export const getHostRef = (elm: d.RuntimeRef | undefined): d.HostRef | undefined => {
return hostRefs.get(elm);
};
if (elm.__stencil__getHostRef) {
return elm.__stencil__getHostRef();
}

/**
* Given a {@link d.RuntimeRef} remove the corresponding {@link d.HostRef} from
* the {@link hostRefs} WeakMap.
*
* @param ref the runtime ref of interest
* @returns — true if the element was successfully removed, or false if it was not present.
*/
export const deleteHostRef = (ref: d.RuntimeRef) => hostRefs.delete(ref);
return undefined;
};

/**
* Add the provided `hostRef` instance to the global {@link hostRefs} map, using the provided `lazyInstance` as a key.
* @param lazyInstance a Stencil component instance
* @param hostRef an optional reference to Stencil's tracking data for the component. If none is provided, one will be created.
* @returns the updated `hostRefs` data structure
* @throws if the provided `lazyInstance` coerces to `null`, or if the `lazyInstance` does not have a `constructor`
* property
*/
export const registerInstance = (
lazyInstance: any,
hostRef: d.HostRef | null | undefined,
): Map<d.RuntimeRef, d.HostRef> => {
export const registerInstance = (lazyInstance: any, hostRef: d.HostRef | null | undefined) => {
if (lazyInstance == null || lazyInstance.constructor == null) {
throw new Error(`Invalid component constructor`);
}
Expand All @@ -44,8 +33,8 @@ export const registerInstance = (
hostRef = getHostRef(elm);
}

lazyInstance.__stencil__getHostRef = () => hostRef;
hostRef.$lazyInstance$ = lazyInstance;
return hostRefs.set(lazyInstance, hostRef);
};

/**
Expand All @@ -65,5 +54,5 @@ export const registerHost = (elm: d.HostElement, cmpMeta: d.ComponentRuntimeMeta
hostRef.$onReadyPromise$ = new Promise((r) => (hostRef.$onReadyResolve$ = r));
elm['s-p'] = [];
elm['s-rc'] = [];
hostRefs.set(elm, hostRef);
elm.__stencil__getHostRef = () => hostRef;
};
3 changes: 1 addition & 2 deletions src/testing/platform/testing-platform.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type * as d from '@stencil/core/internal';

import { cstrs, hostRefs, moduleLoaded, styles } from './testing-constants';
import { cstrs, moduleLoaded, styles } from './testing-constants';
import { flushAll, resetTaskQueue } from './testing-task-queue';
import { win } from './testing-window';

Expand Down Expand Up @@ -54,7 +54,6 @@ export function resetPlatform(defaults: Partial<d.PlatformRuntime> = {}) {
win.close();
}

hostRefs.clear();
styles.clear();
plt.$flags$ = 0;
Object.assign(plt, defaults);
Expand Down

0 comments on commit 6424713

Please sign in to comment.