|
| 1 | +/** @import { ComponentContext } from '#client' */ |
| 2 | + |
| 3 | +import { DEV } from 'esm-env'; |
| 4 | +import { add_owner } from './dev/ownership.js'; |
| 5 | +import { lifecycle_outside_component } from '../shared/errors.js'; |
| 6 | +import { source } from './reactivity/sources.js'; |
| 7 | +import { |
| 8 | + active_effect, |
| 9 | + active_reaction, |
| 10 | + set_active_effect, |
| 11 | + set_active_reaction |
| 12 | +} from './runtime.js'; |
| 13 | +import { effect } from './reactivity/effects.js'; |
| 14 | +import { legacy_mode_flag } from '../flags/index.js'; |
| 15 | + |
| 16 | +/** @type {ComponentContext | null} */ |
| 17 | +export let component_context = null; |
| 18 | + |
| 19 | +/** @param {ComponentContext | null} context */ |
| 20 | +export function set_component_context(context) { |
| 21 | + component_context = context; |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * The current component function. Different from current component context: |
| 26 | + * ```html |
| 27 | + * <!-- App.svelte --> |
| 28 | + * <Foo> |
| 29 | + * <Bar /> <!-- context == Foo.svelte, function == App.svelte --> |
| 30 | + * </Foo> |
| 31 | + * ``` |
| 32 | + * @type {ComponentContext['function']} |
| 33 | + */ |
| 34 | +export let dev_current_component_function = null; |
| 35 | + |
| 36 | +/** @param {ComponentContext['function']} fn */ |
| 37 | +export function set_dev_current_component_function(fn) { |
| 38 | + dev_current_component_function = fn; |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Retrieves the context that belongs to the closest parent component with the specified `key`. |
| 43 | + * Must be called during component initialisation. |
| 44 | + * |
| 45 | + * @template T |
| 46 | + * @param {any} key |
| 47 | + * @returns {T} |
| 48 | + */ |
| 49 | +export function getContext(key) { |
| 50 | + const context_map = get_or_init_context_map('getContext'); |
| 51 | + const result = /** @type {T} */ (context_map.get(key)); |
| 52 | + |
| 53 | + if (DEV) { |
| 54 | + const fn = /** @type {ComponentContext} */ (component_context).function; |
| 55 | + if (fn) { |
| 56 | + add_owner(result, fn, true); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return result; |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Associates an arbitrary `context` object with the current component and the specified `key` |
| 65 | + * and returns that object. The context is then available to children of the component |
| 66 | + * (including slotted content) with `getContext`. |
| 67 | + * |
| 68 | + * Like lifecycle functions, this must be called during component initialisation. |
| 69 | + * |
| 70 | + * @template T |
| 71 | + * @param {any} key |
| 72 | + * @param {T} context |
| 73 | + * @returns {T} |
| 74 | + */ |
| 75 | +export function setContext(key, context) { |
| 76 | + const context_map = get_or_init_context_map('setContext'); |
| 77 | + context_map.set(key, context); |
| 78 | + return context; |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Checks whether a given `key` has been set in the context of a parent component. |
| 83 | + * Must be called during component initialisation. |
| 84 | + * |
| 85 | + * @param {any} key |
| 86 | + * @returns {boolean} |
| 87 | + */ |
| 88 | +export function hasContext(key) { |
| 89 | + const context_map = get_or_init_context_map('hasContext'); |
| 90 | + return context_map.has(key); |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Retrieves the whole context map that belongs to the closest parent component. |
| 95 | + * Must be called during component initialisation. Useful, for example, if you |
| 96 | + * programmatically create a component and want to pass the existing context to it. |
| 97 | + * |
| 98 | + * @template {Map<any, any>} [T=Map<any, any>] |
| 99 | + * @returns {T} |
| 100 | + */ |
| 101 | +export function getAllContexts() { |
| 102 | + const context_map = get_or_init_context_map('getAllContexts'); |
| 103 | + |
| 104 | + if (DEV) { |
| 105 | + const fn = component_context?.function; |
| 106 | + if (fn) { |
| 107 | + for (const value of context_map.values()) { |
| 108 | + add_owner(value, fn, true); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return /** @type {T} */ (context_map); |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * @param {Record<string, unknown>} props |
| 118 | + * @param {any} runes |
| 119 | + * @param {Function} [fn] |
| 120 | + * @returns {void} |
| 121 | + */ |
| 122 | +export function push(props, runes = false, fn) { |
| 123 | + component_context = { |
| 124 | + p: component_context, |
| 125 | + c: null, |
| 126 | + e: null, |
| 127 | + m: false, |
| 128 | + s: props, |
| 129 | + x: null, |
| 130 | + l: null |
| 131 | + }; |
| 132 | + |
| 133 | + if (legacy_mode_flag && !runes) { |
| 134 | + component_context.l = { |
| 135 | + s: null, |
| 136 | + u: null, |
| 137 | + r1: [], |
| 138 | + r2: source(false) |
| 139 | + }; |
| 140 | + } |
| 141 | + |
| 142 | + if (DEV) { |
| 143 | + // component function |
| 144 | + component_context.function = fn; |
| 145 | + dev_current_component_function = fn; |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +/** |
| 150 | + * @template {Record<string, any>} T |
| 151 | + * @param {T} [component] |
| 152 | + * @returns {T} |
| 153 | + */ |
| 154 | +export function pop(component) { |
| 155 | + const context_stack_item = component_context; |
| 156 | + if (context_stack_item !== null) { |
| 157 | + if (component !== undefined) { |
| 158 | + context_stack_item.x = component; |
| 159 | + } |
| 160 | + const component_effects = context_stack_item.e; |
| 161 | + if (component_effects !== null) { |
| 162 | + var previous_effect = active_effect; |
| 163 | + var previous_reaction = active_reaction; |
| 164 | + context_stack_item.e = null; |
| 165 | + try { |
| 166 | + for (var i = 0; i < component_effects.length; i++) { |
| 167 | + var component_effect = component_effects[i]; |
| 168 | + set_active_effect(component_effect.effect); |
| 169 | + set_active_reaction(component_effect.reaction); |
| 170 | + effect(component_effect.fn); |
| 171 | + } |
| 172 | + } finally { |
| 173 | + set_active_effect(previous_effect); |
| 174 | + set_active_reaction(previous_reaction); |
| 175 | + } |
| 176 | + } |
| 177 | + component_context = context_stack_item.p; |
| 178 | + if (DEV) { |
| 179 | + dev_current_component_function = context_stack_item.p?.function ?? null; |
| 180 | + } |
| 181 | + context_stack_item.m = true; |
| 182 | + } |
| 183 | + // Micro-optimization: Don't set .a above to the empty object |
| 184 | + // so it can be garbage-collected when the return here is unused |
| 185 | + return component || /** @type {T} */ ({}); |
| 186 | +} |
| 187 | + |
| 188 | +/** |
| 189 | + * @param {string} name |
| 190 | + * @returns {Map<unknown, unknown>} |
| 191 | + */ |
| 192 | +function get_or_init_context_map(name) { |
| 193 | + if (component_context === null) { |
| 194 | + lifecycle_outside_component(name); |
| 195 | + } |
| 196 | + |
| 197 | + return (component_context.c ??= new Map(get_parent_context(component_context) || undefined)); |
| 198 | +} |
| 199 | + |
| 200 | +/** |
| 201 | + * @param {ComponentContext} component_context |
| 202 | + * @returns {Map<unknown, unknown> | null} |
| 203 | + */ |
| 204 | +function get_parent_context(component_context) { |
| 205 | + let parent = component_context.p; |
| 206 | + while (parent !== null) { |
| 207 | + const context_map = parent.c; |
| 208 | + if (context_map !== null) { |
| 209 | + return context_map; |
| 210 | + } |
| 211 | + parent = parent.p; |
| 212 | + } |
| 213 | + return null; |
| 214 | +} |
0 commit comments