|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT |
| 6 | + */ |
| 7 | +import { |
| 8 | + isUndefined, |
| 9 | + getPrototypeOf, |
| 10 | + keys, |
| 11 | + getContextKeys, |
| 12 | + ArrayFilter, |
| 13 | + ContextEventName, |
| 14 | + isTrustedContext, |
| 15 | + type ContextProvidedCallback, |
| 16 | + type ContextBinding as IContextBinding, |
| 17 | +} from '@lwc/shared'; |
| 18 | +import { type VM } from '../vm'; |
| 19 | +import { logWarnOnce } from '../../shared/logger'; |
| 20 | +import type { Signal } from '@lwc/signals'; |
| 21 | +import type { RendererAPI } from '../renderer'; |
| 22 | +import type { ShouldContinueBubbling } from '../wiring/types'; |
| 23 | + |
| 24 | +type ContextVarieties = Map<unknown, Signal<unknown>>; |
| 25 | + |
| 26 | +class ContextBinding<C extends object> implements IContextBinding<C> { |
| 27 | + component: C; |
| 28 | + #renderer: RendererAPI; |
| 29 | + #providedContextVarieties: ContextVarieties; |
| 30 | + #elm: HTMLElement; |
| 31 | + |
| 32 | + constructor(vm: VM, component: C, providedContextVarieties: ContextVarieties) { |
| 33 | + this.component = component; |
| 34 | + this.#renderer = vm.renderer; |
| 35 | + this.#elm = vm.elm; |
| 36 | + this.#providedContextVarieties = providedContextVarieties; |
| 37 | + |
| 38 | + // Register the component as a context provider. |
| 39 | + this.#renderer.registerContextProvider( |
| 40 | + this.#elm, |
| 41 | + ContextEventName, |
| 42 | + (contextConsumer): ShouldContinueBubbling => { |
| 43 | + // This callback is invoked when the provided context is consumed somewhere down |
| 44 | + // in the component's subtree. |
| 45 | + return contextConsumer.setNewContext(this.#providedContextVarieties); |
| 46 | + } |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + provideContext<V extends object>( |
| 51 | + contextVariety: V, |
| 52 | + providedContextSignal: Signal<unknown> |
| 53 | + ): void { |
| 54 | + if (this.#providedContextVarieties.has(contextVariety)) { |
| 55 | + logWarnOnce( |
| 56 | + 'Multiple contexts of the same variety were provided. Only the first context will be used.' |
| 57 | + ); |
| 58 | + return; |
| 59 | + } |
| 60 | + this.#providedContextVarieties.set(contextVariety, providedContextSignal); |
| 61 | + } |
| 62 | + |
| 63 | + consumeContext<V extends object>( |
| 64 | + contextVariety: V, |
| 65 | + contextProvidedCallback: ContextProvidedCallback |
| 66 | + ): void { |
| 67 | + this.#renderer.registerContextConsumer(this.#elm, ContextEventName, { |
| 68 | + setNewContext: (providerContextVarieties: ContextVarieties): ShouldContinueBubbling => { |
| 69 | + // If the provider has the specified context variety, then it is consumed |
| 70 | + // and true is returned to stop bubbling. |
| 71 | + if (providerContextVarieties.has(contextVariety)) { |
| 72 | + contextProvidedCallback(providerContextVarieties.get(contextVariety)); |
| 73 | + return true; |
| 74 | + } |
| 75 | + // Return false as context has not been found/consumed |
| 76 | + // and the consumer should continue traversing the context tree |
| 77 | + return false; |
| 78 | + }, |
| 79 | + }); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +export function connectContext(vm: VM) { |
| 84 | + const contextKeys = getContextKeys(); |
| 85 | + |
| 86 | + if (isUndefined(contextKeys)) { |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + const { connectContext } = contextKeys; |
| 91 | + const { component } = vm; |
| 92 | + |
| 93 | + const enumerableKeys = keys(getPrototypeOf(component)); |
| 94 | + const contextfulKeys = ArrayFilter.call(enumerableKeys, (enumerableKey) => |
| 95 | + isTrustedContext((component as any)[enumerableKey]) |
| 96 | + ); |
| 97 | + |
| 98 | + if (contextfulKeys.length === 0) { |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + const providedContextVarieties: ContextVarieties = new Map(); |
| 103 | + |
| 104 | + try { |
| 105 | + for (let i = 0; i < contextfulKeys.length; i++) { |
| 106 | + (component as any)[contextfulKeys[i]][connectContext]( |
| 107 | + new ContextBinding(vm, component, providedContextVarieties) |
| 108 | + ); |
| 109 | + } |
| 110 | + } catch (err: any) { |
| 111 | + logWarnOnce( |
| 112 | + `Attempted to connect to trusted context but received the following error: ${ |
| 113 | + err.message |
| 114 | + }` |
| 115 | + ); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +export function disconnectContext(vm: VM) { |
| 120 | + const contextKeys = getContextKeys(); |
| 121 | + |
| 122 | + if (!contextKeys) { |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + const { disconnectContext } = contextKeys; |
| 127 | + const { component } = vm; |
| 128 | + |
| 129 | + const enumerableKeys = keys(getPrototypeOf(component)); |
| 130 | + const contextfulKeys = ArrayFilter.call(enumerableKeys, (enumerableKey) => |
| 131 | + isTrustedContext((component as any)[enumerableKey]) |
| 132 | + ); |
| 133 | + |
| 134 | + if (contextfulKeys.length === 0) { |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + try { |
| 139 | + for (let i = 0; i < contextfulKeys.length; i++) { |
| 140 | + (component as any)[contextfulKeys[i]][disconnectContext](component); |
| 141 | + } |
| 142 | + } catch (err: any) { |
| 143 | + logWarnOnce( |
| 144 | + `Attempted to disconnect from trusted context but received the following error: ${ |
| 145 | + err.message |
| 146 | + }` |
| 147 | + ); |
| 148 | + } |
| 149 | +} |
0 commit comments