forked from stenciljs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient-host-ref.ts
More file actions
80 lines (70 loc) · 2.47 KB
/
Copy pathclient-host-ref.ts
File metadata and controls
80 lines (70 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { BUILD } from '@app-data';
import { CMP_FLAGS } from '@utils/constants';
import { reWireGetterSetter } from '@utils/es2022-rewire-class-members';
import type * as d from '../declarations';
/**
* Given a {@link d.RuntimeRef} retrieve the corresponding {@link d.HostRef}
*
* @param ref the runtime ref of interest
* @returns the Host reference (if found) or undefined
*/
export const getHostRef = (ref: d.RuntimeRef): d.HostRef | undefined => {
if (ref.__stencil__getHostRef) {
return ref.__stencil__getHostRef();
}
return undefined;
};
/**
* Register a lazy instance with the {@link hostRefs} object so it's
* corresponding {@link d.HostRef} can be retrieved later.
*
* @param lazyInstance the lazy instance of interest
* @param hostRef that instances `HostRef` object
*/
export const registerInstance = (lazyInstance: any, hostRef: d.HostRef) => {
if (!hostRef) return;
lazyInstance.__stencil__getHostRef = () => hostRef;
hostRef.$lazyInstance$ = lazyInstance;
if (hostRef.$cmpMeta$.$flags$ & CMP_FLAGS.hasModernPropertyDecls && (BUILD.state || BUILD.prop)) {
reWireGetterSetter(lazyInstance, hostRef);
}
};
/**
* Register a host element for a Stencil component, setting up various metadata
* and callbacks based on {@link BUILD} flags as well as the component's runtime
* metadata.
*
* @param hostElement the host element to register
* @param cmpMeta runtime metadata for that component
* @returns a reference to the host ref WeakMap
*/
export const registerHost = (hostElement: d.HostElement, cmpMeta: d.ComponentRuntimeMeta) => {
const hostRef: d.HostRef = {
$flags$: 0,
$hostElement$: hostElement,
$cmpMeta$: cmpMeta,
$instanceValues$: new Map(),
$serializerValues$: new Map(),
};
if (BUILD.isDev) {
hostRef.$renderCount$ = 0;
}
if (BUILD.method && BUILD.lazyLoad) {
hostRef.$onInstancePromise$ = new Promise((r) => (hostRef.$onInstanceResolve$ = r));
}
if (BUILD.asyncLoading) {
hostRef.$onReadyPromise$ = new Promise((r) => (hostRef.$onReadyResolve$ = r));
hostElement['s-p'] = [];
hostElement['s-rc'] = [];
}
if (BUILD.lazyLoad) {
hostRef.$fetchedCbList$ = [];
}
const ref = hostRef;
hostElement.__stencil__getHostRef = () => ref;
if (!BUILD.lazyLoad && cmpMeta.$flags$ & CMP_FLAGS.hasModernPropertyDecls && (BUILD.state || BUILD.prop)) {
reWireGetterSetter(hostElement, hostRef);
}
return ref;
};
export const isMemberInElement = (elm: any, memberName: string) => memberName in elm;