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

99.05% Statements 209/211
97.82% Branches 45/46
100% Functions 6/6
99.02% Lines 203/205

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 2062x 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 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 29902x 29902x 29902x 29902x 29902x 29902x 29902x 29902x 2x 2x 2x 2x 2x 2x 2x 8x 8x 8x 8x 26x 26x 26x 26x 4x 4x 4x 22x 22x 8x 8x 8x 48x 4x 4x 4x 4x 4x 44x 44x 8x 8x 2x 2x 2x 2x 2x 2x 2x 2x 21135x 21135x 21135x 21135x 21135x 21135x 15868x 15868x 21135x 21135x 21135x 2x 2x 2x 2x 2x 2x 2x 778x 778x 778x 778x 778x 778x 2x 2x 2x 2x 2x 2x 2x 2x 24065x     24065x 24065x 20772x 20772x 20772x 20772x 20772x 20772x 20772x 20772x 20772x 20772x 20772x 20772x 20772x 20772x 144x 20772x 101x 14x 14x 101x 87x 79x 87x 8x 8x 87x 101x 20772x 20772x 20772x 24x 24x 20772x 20772x 20772x 20772x 24065x 24065x 24065x 2x 2x 2x 2x 2x 2x 27062x 27062x 27062x 20281x 20281x 20281x 20281x 27057x 26707x 26707x 26707x 26707x 26707x 24204x 24204x 26707x 24048x 24048x 26707x 25x 25x 25x 24023x 24023x 24023x 24023x 26701x 22400x 6290x 22275x 16110x 16110x 22400x 26707x 27062x  
/** @import { Derived, Effect, Source, Value } from '#client' */
import { DEV } from 'esm-env';
import {
	current_component_context,
	current_reaction,
	new_deps,
	current_effect,
	current_untracked_writes,
	get,
	is_runes,
	schedule_effect,
	set_current_untracked_writes,
	set_signal_status,
	untrack,
	increment_version,
	update_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';
import { derived } from './deriveds.js';
 
let inspect_effects = new Set();
 
/**
 * @template V
 * @param {V} v
 * @returns {Source<V>}
 */
/*#__NO_SIDE_EFFECTS__*/
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} get_value
 * @returns {(value?: V) => V}
 */
export function source_link(get_value) {
	var was_local = false;
	var local_source = source(/** @type {V} */ (undefined));
 
	var linked_derived = derived(() => {
		var local_value = /** @type {V} */ (get(local_source));
		var linked_value = get_value();
 
		if (was_local) {
			was_local = false;
			return local_value;
		}
 
		return linked_value;
	});
 
	return function (/** @type {any} */ value) {
		if (arguments.length > 0) {
			was_local = true;
			set(local_source, value);
			get(linked_derived);
			return value;
		}
 
		return (local_source.v = get(linked_derived));
	};
}
 
/**
 * @template V
 * @param {V} initial_value
 * @returns {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 !== null && current_component_context.l !== null) {
		(current_component_context.l.s ??= []).push(s);
	}
 
	return s;
}
 
/**
 * @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 (current_reaction !== null && is_runes() && (current_reaction.f & DERIVED) !== 0) {
		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() &&
			current_effect !== null &&
			(current_effect.f & CLEAN) !== 0 &&
			(current_effect.f & BRANCH_EFFECT) === 0
		) {
			if (new_deps !== null && new_deps.includes(source)) {
				set_signal_status(current_effect, DIRTY);
				schedule_effect(current_effect);
			} else {
				if (current_untracked_writes === null) {
					set_current_untracked_writes([source]);
				} else {
					current_untracked_writes.push(source);
				}
			}
		}
 
		if (DEV) {
			for (const effect of inspect_effects) {
				update_effect(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 === current_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));
			}
		}
	}
}