@@ -3,41 +3,19 @@ import { reWireGetterSetter } from '@utils/es2022-rewire-class-members';
3
3
4
4
import type * as d from '../declarations' ;
5
5
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
-
34
6
/**
35
7
* Given a {@link d.RuntimeRef} retrieve the corresponding {@link d.HostRef}
36
8
*
37
9
* @param ref the runtime ref of interest
38
10
* @returns the Host reference (if found) or undefined
39
11
*/
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
+ } ;
41
19
42
20
/**
43
21
* 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
47
25
* @param hostRef that instances `HostRef` object
48
26
*/
49
27
export const registerInstance = ( lazyInstance : any , hostRef : d . HostRef ) => {
50
- hostRefs . set ( ( hostRef . $lazyInstance$ = lazyInstance ) , hostRef ) ;
28
+ lazyInstance . __stencil__getHostRef = ( ) => hostRef ;
29
+ hostRef . $lazyInstance$ = lazyInstance ;
30
+
51
31
if ( BUILD . modernPropertyDecls && ( BUILD . state || BUILD . prop ) ) {
52
32
reWireGetterSetter ( lazyInstance , hostRef ) ;
53
33
}
@@ -81,7 +61,8 @@ export const registerHost = (hostElement: d.HostElement, cmpMeta: d.ComponentRun
81
61
hostElement [ 's-rc' ] = [ ] ;
82
62
}
83
63
84
- const ref = hostRefs . set ( hostElement , hostRef ) ;
64
+ const ref = hostRef ;
65
+ hostElement . __stencil__getHostRef = ( ) => ref ;
85
66
86
67
if ( ! BUILD . lazyLoad && BUILD . modernPropertyDecls && ( BUILD . state || BUILD . prop ) ) {
87
68
reWireGetterSetter ( hostElement , hostRef ) ;
0 commit comments