All files / src/internal/client/reactivity sources.js

94.19% Statements 146/155
96% Branches 24/25
100% Functions 4/4
94% Lines 141/150

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 1512x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 39588x 39588x 39588x 39588x 39588x 39588x 39588x 39588x 39588x 39588x 39588x 39588x 39588x 39588x 39588x 2x 2x 2x 2x 2x 2x 2x 2x 24055x 24055x 24055x 24055x 24055x 24055x 21104x 21104x 24055x 24055x 24055x 2x 2x 2x 2x 2x 2x 2x 758x 758x 758x 758x 758x 758x 2x 2x 2x 2x 2x 2x 2x 2x 23728x 23728x 23728x 23728x 23728x 23728x 23728x 1315x 23728x                   23728x 23728x 20504x 20504x 20504x 20504x 20504x 20504x 20504x 20504x 20504x 20504x 20504x 20504x 20504x 20504x 20504x 20504x 20504x 20504x 20504x 1329x 20504x 1275x 12x 12x 1275x 1263x 1219x 1263x 44x 44x 1263x 1275x 20504x 20504x 20504x 20504x 20504x 445x 20504x 20059x 20059x 20504x 20504x 23728x 23728x 23728x  
import { DEV } from 'esm-env';
import {
	current_component_context,
	current_reaction,
	current_dependencies,
	current_effect,
	current_untracked_writes,
	current_untracking,
	get,
	is_batching_effect,
	is_runes,
	mark_reactions,
	schedule_effect,
	set_current_untracked_writes,
	set_last_inspected_signal,
	set_signal_status,
	untrack
} from '../runtime.js';
import { equals, safe_equals } from './equality.js';
import { CLEAN, DERIVED, DIRTY, BRANCH_EFFECT } from '../constants.js';
import { UNINITIALIZED } from '../../../constants.js';
 
/**
 * @template V
 * @param {V} value
 * @returns {import('#client').Source<V>}
 */
/*#__NO_SIDE_EFFECTS__*/
export function source(value) {
	/** @type {import('#client').Source<V>} */
	const source = {
		f: 0, // TODO ideally we could skip this altogether, but it causes type errors
		reactions: null,
		equals: equals,
		v: value,
		version: 0
	};
 
	if (DEV) {
		/** @type {import('#client').ValueDebug<V>} */ (source).inspect = new Set();
	}
 
	return source;
}
 
/**
 * @template V
 * @param {V} initial_value
 * @returns {import('#client').Source<V>}
 */
/*#__NO_SIDE_EFFECTS__*/
export function mutable_source(initial_value) {
	const s = source(initial_value);
	s.equals = safe_equals;
 
	// bind the signal to the component context, in case we need to
	// track updates to trigger beforeUpdate/afterUpdate callbacks
	if (current_component_context) {
		(current_component_context.d ??= []).push(s);
	}
 
	return s;
}
 
/**
 * @template V
 * @param {import('#client').Value<V>} source
 * @param {V} value
 */
export function mutate(source, value) {
	set(
		source,
		untrack(() => get(source))
	);
	return value;
}
 
/**
 * @template V
 * @param {import('#client').Source<V>} signal
 * @param {V} value
 * @returns {V}
 */
export function set(signal, value) {
	var initialized = signal.v !== UNINITIALIZED;
 
	if (
		!current_untracking &&
		initialized &&
		current_reaction !== null &&
		is_runes() &&
		(current_reaction.f & DERIVED) !== 0
	) {
		throw new Error(
			'ERR_SVELTE_UNSAFE_MUTATION' +
				(DEV
					? ": Unsafe mutations during Svelte's render or derived phase are not permitted in runes mode. " +
						'This can lead to unexpected errors and possibly cause infinite loops.\n\nIf this mutation is not meant ' +
						'to be reactive do not use the "$state" rune for that declaration.'
					: '')
		);
	}
 
	if (!signal.equals(value)) {
		signal.v = value;
 
		// Increment write version so that unowned signals can properly track dirtiness
		signal.version++;
 
		// If the current signal is running for the first time, it won't have any
		// reactions as we only allocate and assign the reactions after the signal
		// has fully executed. So in the case of ensuring it registers the reaction
		// properly for itself, we need to ensure the current effect actually gets
		// scheduled. i.e:
		//
		// $effect(() => x++)
		//
		// We additionally want to skip this logic when initialising store sources
		if (
			is_runes() &&
			initialized &&
			current_effect !== null &&
			(current_effect.f & CLEAN) !== 0 &&
			(current_effect.f & BRANCH_EFFECT) === 0
		) {
			if (current_dependencies !== null && current_dependencies.includes(signal)) {
				set_signal_status(current_effect, DIRTY);
				schedule_effect(current_effect);
			} else {
				if (current_untracked_writes === null) {
					set_current_untracked_writes([signal]);
				} else {
					current_untracked_writes.push(signal);
				}
			}
		}
 
		mark_reactions(signal, DIRTY, true);
 
		if (DEV) {
			if (is_batching_effect) {
				set_last_inspected_signal(/** @type {import('#client').ValueDebug} */ (signal));
			} else {
				for (const fn of /** @type {import('#client').ValueDebug} */ (signal).inspect) fn();
			}
		}
	}
 
	return value;
}