@@ -3,41 +3,19 @@ import { reWireGetterSetter } from '@utils/es2022-rewire-class-members';
33
44import type * as d from '../declarations' ;
55
6- /**
7- * A WeakMap mapping runtime component references to their corresponding host reference
8- * instances.
9- *
10- * **Note**: If we're in an HMR context we need to store a reference to this
11- * value on `window` in order to maintain the mapping of {@link d.RuntimeRef}
12- * to {@link d.HostRef} across HMR updates.
13- *
14- * This is necessary because when HMR updates for a component are processed by
15- * the browser-side dev server client the JS bundle for that component is
16- * re-fetched. Since the module containing {@link hostRefs} is included in
17- * that bundle, if we do not store a reference to it the new iteration of the
18- * component will not have access to the previous hostRef map, leading to a
19- * bug where the new version of the component cannot properly initialize.
20- */
21- const hostRefs : WeakMap < d . RuntimeRef , d . HostRef > = /*@__PURE__ */ BUILD . hotModuleReplacement
22- ? ( ( window as any ) . __STENCIL_HOSTREFS__ ||= new WeakMap ( ) )
23- : new WeakMap ( ) ;
24-
25- /**
26- * Given a {@link d.RuntimeRef} remove the corresponding {@link d.HostRef} from
27- * the {@link hostRefs} WeakMap.
28- *
29- * @param ref the runtime ref of interest
30- * @returns — true if the element was successfully removed, or false if it was not present.
31- */
32- export const deleteHostRef = ( ref : d . RuntimeRef ) => hostRefs . delete ( ref ) ;
33-
346/**
357 * Given a {@link d.RuntimeRef} retrieve the corresponding {@link d.HostRef}
368 *
379 * @param ref the runtime ref of interest
3810 * @returns the Host reference (if found) or undefined
3911 */
40- export const getHostRef = ( ref : d . RuntimeRef ) : d . HostRef | undefined => hostRefs . get ( ref ) ;
12+ export const getHostRef = ( ref : d . RuntimeRef ) : d . HostRef | undefined => {
13+ if ( ref . __stencil__getHostRef ) {
14+ return ref . __stencil__getHostRef ( ) ;
15+ }
16+
17+ return undefined ;
18+ } ;
4119
4220/**
4321 * Register a lazy instance with the {@link hostRefs} object so it's
@@ -47,7 +25,9 @@ export const getHostRef = (ref: d.RuntimeRef): d.HostRef | undefined => hostRefs
4725 * @param hostRef that instances `HostRef` object
4826 */
4927export const registerInstance = ( lazyInstance : any , hostRef : d . HostRef ) => {
50- hostRefs . set ( ( hostRef . $lazyInstance$ = lazyInstance ) , hostRef ) ;
28+ lazyInstance . __stencil__getHostRef = ( ) => hostRef ;
29+ hostRef . $lazyInstance$ = lazyInstance ;
30+
5131 if ( BUILD . modernPropertyDecls && ( BUILD . state || BUILD . prop ) ) {
5232 reWireGetterSetter ( lazyInstance , hostRef ) ;
5333 }
@@ -81,7 +61,8 @@ export const registerHost = (hostElement: d.HostElement, cmpMeta: d.ComponentRun
8161 hostElement [ 's-rc' ] = [ ] ;
8262 }
8363
84- const ref = hostRefs . set ( hostElement , hostRef ) ;
64+ const ref = hostRef ;
65+ hostElement . __stencil__getHostRef = ( ) => ref ;
8566
8667 if ( ! BUILD . lazyLoad && BUILD . modernPropertyDecls && ( BUILD . state || BUILD . prop ) ) {
8768 reWireGetterSetter ( hostElement , hostRef ) ;
0 commit comments