-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathcontext.ts
More file actions
61 lines (46 loc) · 1.71 KB
/
context.ts
File metadata and controls
61 lines (46 loc) · 1.71 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
/*
* Copyright (c) 2024, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
import { isFalse } from './assert';
export const ContextEventName = 'lightning:context-request';
let trustedContext: WeakSet<object>;
export type ContextKeys = {
connectContext: symbol;
disconnectContext: symbol;
};
export type ContextProvidedCallback = (contextSignal?: object) => void;
export interface ContextConnector<C extends object> {
component: C;
provideContext<V extends object>(contextVariety: V, providedContextSignal: object): void;
consumeContext<V extends object>(
contextVariety: V,
contextProvidedCallback: ContextProvidedCallback
): void;
}
let contextKeys: ContextKeys;
export function setContextKeys(config: ContextKeys) {
isFalse(contextKeys, '`setContextKeys` cannot be called more than once');
contextKeys = config;
}
export function getContextKeys() {
return contextKeys;
}
export function setTrustedContextSet(context: WeakSet<object>) {
isFalse(trustedContext, 'Trusted Context Set is already set!');
trustedContext = context;
}
export function addTrustedContext(contextParticipant: object) {
// This should be a no-op when the trustedSignals set isn't set by runtime
trustedContext?.add(contextParticipant);
}
export function isTrustedContext(target: object): boolean {
if (!trustedContext) {
// The runtime didn't set a trustedContext set
// this check should only be performed for runtimes that care about filtering context participants to track
return true;
}
return trustedContext.has(target);
}