MCPcopy
hub / github.com/sveltejs/svelte / prop

Function prop

packages/svelte/src/internal/client/reactivity/props.js:279–436  ·  view source on GitHub ↗
(props, key, flags, fallback)

Source from the content-addressed store, hash-verified

277 * @returns {(() => V | ((arg: V) => V) | ((arg: V, mutation: boolean) => V))}
278 */
279export function prop(props, key, flags, fallback) {
280 var runes = !legacy_mode_flag || (flags & PROPS_IS_RUNES) !== 0;
281 var bindable = (flags & PROPS_IS_BINDABLE) !== 0;
282 var lazy = (flags & PROPS_IS_LAZY_INITIAL) !== 0;
283
284 var fallback_value = /** @type {V} */ (fallback);
285 var fallback_dirty = true;
286 var fallback_signal = /** @type {Derived<V> | undefined} */ (undefined);
287
288 var get_fallback = () => {
289 if (lazy && runes) {
290 fallback_signal ??= derived(/** @type {() => V} */ (fallback));
291 return get(fallback_signal);
292 }
293
294 if (fallback_dirty) {
295 fallback_dirty = false;
296
297 fallback_value = lazy
298 ? untrack(/** @type {() => V} */ (fallback))
299 : /** @type {V} */ (fallback);
300 }
301
302 return fallback_value;
303 };
304
305 /** @type {((v: V) => void) | undefined} */
306 let setter;
307
308 if (bindable) {
309 // Can be the case when someone does `mount(Component, props)` with `let props = $state({...})`
310 // or `createClassComponent(Component, props)`
311 var is_entry_props = STATE_SYMBOL in props || LEGACY_PROPS in props;
312
313 setter =
314 get_descriptor(props, key)?.set ??
315 (is_entry_props && key in props ? (v) => (props[key] = v) : undefined);
316 }
317
318 /** @type {V} */
319 var initial_value;
320 var is_store_sub = false;
321
322 if (bindable) {
323 [initial_value, is_store_sub] = capture_store_binding(() => /** @type {V} */ (props[key]));
324 } else {
325 initial_value = /** @type {V} */ (props[key]);
326 }
327
328 if (initial_value === undefined && fallback !== undefined) {
329 initial_value = get_fallback();
330
331 if (setter) {
332 if (runes) e.props_invalid_value(key);
333 setter(initial_value);
334 }
335 }
336

Callers 1

setFunction · 0.70

Calls 5

capture_store_bindingFunction · 0.90
getFunction · 0.90
proxyFunction · 0.90
setFunction · 0.90
get_fallbackFunction · 0.85

Tested by

no test coverage detected