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

99.08% Statements 216/218
98% Branches 49/50
100% Functions 9/9
99.06% Lines 211/213

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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 2412x 2x                                       2x 2x                 2x 2x 2x 2x 2x 2x 2x 2x 2x 34812x 34812x 2x 2x 2x 2x 2x 2x 2x 50973x 50973x 50973x 50973x 50973x 50973x 50973x 50973x 2x 2x 2x 2x 2x 2x 812x 812x 2x 2x 2x 2x 2x 2x 2x 2x 2x 21299x 21299x 21285x 21285x 21299x 21299x 21299x 21299x 15966x 15966x 21299x 21299x 21299x 2x 2x 2x 2x 2x 2x 2x 2x 4861x 4861x 2x 2x 2x 2x 2x 2x 5673x 5673x 22x 14x 22x 8x 8x 22x 5673x 5673x 5673x 2x 2x 2x 2x 2x 2x 2x 782x 782x 782x 782x 782x 782x 2x 2x 2x 2x 2x 2x 2x 2x 24584x 24584x 24584x 24584x 4x 4x 4x 24584x     24584x 24584x 21213x 21213x 21213x 21213x 21213x 21213x 21213x 21213x 21213x 21213x 21213x 21213x 21213x 21213x 172x 21213x 107x 14x 14x 107x 93x 83x 93x 10x 10x 93x 107x 21213x 21213x 42x 42x 42x 42x 42x 46x 46x 46x 46x 46x 46x 44x 44x 46x 42x 42x 42x 42x 42x 21213x 24584x 24584x 24584x 2x 2x 2x 2x 2x 2x 27561x 27561x 27561x 20626x 20626x 20626x 20626x 27548x 27654x 27654x 27654x 27654x 27654x 24920x 24920x 27654x 24770x 24770x 27654x 56x 56x 56x 24714x 24714x 24714x 24714x 27648x 23060x 6348x 23060x 16712x 16712x 23060x 27654x 27561x  
/** @import { Derived, Effect, Reaction, Source, Value } from '#client' */
import { DEV } from 'esm-env';
import {
	component_context,
	active_reaction,
	new_deps,
	active_effect,
	untracked_writes,
	get,
	is_runes,
	schedule_effect,
	set_untracked_writes,
	set_signal_status,
	untrack,
	increment_version,
	update_effect,
	derived_sources,
	set_derived_sources,
	check_dirtiness,
	set_is_flushing_effect,
	is_flushing_effect
} from '../runtime.js';
import { equals, safe_equals } from './equality.js';
import {
	CLEAN,
	DERIVED,
	DIRTY,
	BRANCH_EFFECT,
	INSPECT_EFFECT,
	UNOWNED,
	MAYBE_DIRTY
} from '../constants.js';
import * as e from '../errors.js';
 
export let inspect_effects = new Set();
 
/**
 * @param {Set<any>} v
 */
export function set_inspect_effects(v) {
	inspect_effects = v;
}
 
/**
 * @template V
 * @param {V} v
 * @returns {Source<V>}
 */
export function source(v) {
	return {
		f: 0, // TODO ideally we could skip this altogether, but it causes type errors
		v,
		reactions: null,
		equals,
		version: 0
	};
}
 
/**
 * @template V
 * @param {V} v
 */
export function state(v) {
	return push_derived_source(source(v));
}
 
/**
 * @template V
 * @param {V} initial_value
 * @param {boolean} [immutable]
 * @returns {Source<V>}
 */
/*#__NO_SIDE_EFFECTS__*/
export function mutable_source(initial_value, immutable = false) {
	const s = source(initial_value);
	if (!immutable) {
		s.equals = safe_equals;
	}
 
	// bind the signal to the component context, in case we need to
	// track updates to trigger beforeUpdate/afterUpdate callbacks
	if (component_context !== null && component_context.l !== null) {
		(component_context.l.s ??= []).push(s);
	}
 
	return s;
}
 
/**
 * @template V
 * @param {V} v
 * @param {boolean} [immutable]
 * @returns {Source<V>}
 */
export function mutable_state(v, immutable = false) {
	return push_derived_source(mutable_source(v, immutable));
}
 
/**
 * @template V
 * @param {Source<V>} source
 */
/*#__NO_SIDE_EFFECTS__*/
function push_derived_source(source) {
	if (active_reaction !== null && (active_reaction.f & DERIVED) !== 0) {
		if (derived_sources === null) {
			set_derived_sources([source]);
		} else {
			derived_sources.push(source);
		}
	}
 
	return source;
}
 
/**
 * @template V
 * @param {Value<V>} source
 * @param {V} value
 */
export function mutate(source, value) {
	set(
		source,
		untrack(() => get(source))
	);
	return value;
}
 
/**
 * @template V
 * @param {Source<V>} source
 * @param {V} value
 * @returns {V}
 */
export function set(source, value) {
	if (
		active_reaction !== null &&
		is_runes() &&
		(active_reaction.f & DERIVED) !== 0 &&
		// If the source was created locally within the current derived, then
		// we allow the mutation.
		(derived_sources === null || !derived_sources.includes(source))
	) {
		e.state_unsafe_mutation();
	}
 
	if (!source.equals(value)) {
		source.v = value;
		source.version = increment_version();
 
		mark_reactions(source, DIRTY);
 
		// 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++)`
		if (
			is_runes() &&
			active_effect !== null &&
			(active_effect.f & CLEAN) !== 0 &&
			(active_effect.f & BRANCH_EFFECT) === 0
		) {
			if (new_deps !== null && new_deps.includes(source)) {
				set_signal_status(active_effect, DIRTY);
				schedule_effect(active_effect);
			} else {
				if (untracked_writes === null) {
					set_untracked_writes([source]);
				} else {
					untracked_writes.push(source);
				}
			}
		}
 
		if (DEV && inspect_effects.size > 0) {
			const inspects = Array.from(inspect_effects);
			var previously_flushing_effect = is_flushing_effect;
			set_is_flushing_effect(true);
			try {
				for (const effect of inspects) {
					// Mark clean inspect-effects as maybe dirty and then check their dirtiness
					// instead of just updating the effects - this way we avoid overfiring.
					if ((effect.f & CLEAN) !== 0) {
						set_signal_status(effect, MAYBE_DIRTY);
					}
					if (check_dirtiness(effect)) {
						update_effect(effect);
					}
				}
			} finally {
				set_is_flushing_effect(previously_flushing_effect);
			}
			inspect_effects.clear();
		}
	}
 
	return value;
}
 
/**
 * @param {Value} signal
 * @param {number} status should be DIRTY or MAYBE_DIRTY
 * @returns {void}
 */
function mark_reactions(signal, status) {
	var reactions = signal.reactions;
	if (reactions === null) return;
 
	var runes = is_runes();
	var length = reactions.length;
 
	for (var i = 0; i < length; i++) {
		var reaction = reactions[i];
		var flags = reaction.f;
 
		// Skip any effects that are already dirty
		if ((flags & DIRTY) !== 0) continue;
 
		// In legacy mode, skip the current effect to prevent infinite loops
		if (!runes && reaction === active_effect) continue;
 
		// Inspect effects need to run immediately, so that the stack trace makes sense
		if (DEV && (flags & INSPECT_EFFECT) !== 0) {
			inspect_effects.add(reaction);
			continue;
		}
 
		set_signal_status(reaction, status);
 
		// If the signal a) was previously clean or b) is an unowned derived, then mark it
		if ((flags & (CLEAN | UNOWNED)) !== 0) {
			if ((flags & DERIVED) !== 0) {
				mark_reactions(/** @type {Derived} */ (reaction), MAYBE_DIRTY);
			} else {
				schedule_effect(/** @type {Effect} */ (reaction));
			}
		}
	}
}