-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathcontextManager.js
More file actions
57 lines (48 loc) · 1.75 KB
/
contextManager.js
File metadata and controls
57 lines (48 loc) · 1.75 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
import {
setContextKeys,
setTrustedContextSet,
__dangerous_do_not_use_addTrustedContext,
} from 'lwc';
const connectContext = Symbol('connectContext');
const disconnectContext = Symbol('disconnectContext');
const trustedContext = new WeakSet();
setTrustedContextSet(trustedContext);
setContextKeys({ connectContext, disconnectContext });
class MockContextSignal {
connectProvidedComponent;
disconnectProvidedComponent;
providedContextSignal;
constructor(initialValue, contextDefinition, fromContext) {
this.value = initialValue;
this.contextDefinition = contextDefinition;
this.fromContext = fromContext;
__dangerous_do_not_use_addTrustedContext(this);
}
[connectContext](runtimeAdapter) {
this.connectProvidedComponent = runtimeAdapter.component;
runtimeAdapter.provideContext(this.contextDefinition, this);
if (this.fromContext) {
runtimeAdapter.consumeContext(this.fromContext, (providedContextSignal) => {
this.providedContextSignal = providedContextSignal;
this.value = providedContextSignal.value;
});
}
}
[disconnectContext](component) {
this.disconnectProvidedComponent = component;
}
}
// This is a malformed context signal that does not implement the connectContext or disconnectContext methods
class MockMalformedContextSignal {
constructor() {
trustedContext.add(this);
}
}
export const defineContext = (fromContext) => {
const contextDefinition = (initialValue) =>
new MockContextSignal(initialValue, contextDefinition, fromContext);
return contextDefinition;
};
export const defineMalformedContext = () => {
return () => new MockMalformedContextSignal();
};