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

95.78% Statements 159/166
95.83% Branches 23/24
100% Functions 6/6
95.59% Lines 152/159

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 1772x 2x                 2x                   2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 10551x 10551x 10551x 72x 10551x 10479x 10479x 10479x 10479x 10551x 10551x 10551x 10551x 10551x 10551x 10551x 10551x 10551x 10551x 10551x 10551x 10551x 10551x 10551x 23x 23x 23x 10551x 10479x 10479x 10551x 10551x 10551x 2x 2x 2x 2x 2x 2x 2x 2x 5663x 5663x 5663x 5663x 2x 2x 2x 2x 2x 28467x 28467x 28467x 28467x 45x 45x 45x 49x 49x 19x 49x 30x 30x 49x 45x 28467x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 17236x 17236x 17236x 17236x 17236x 17236x 17236x 17236x 17236x 17236x 1x 1x 17235x 17235x 17235x 17235x 17235x 17236x 17236x 17236x 17236x 17236x 17236x               17231x 17231x 17231x 2x 2x 2x 2x 2x 2x 16500x 16500x 16500x 16500x 16500x 16500x 16500x 16264x 16264x 16264x 16500x 2x 2x 2x 2x 2x 2x 11232x 11232x 11232x 11232x 11232x 11232x 11232x  
/** @import { Derived, Effect } from '#client' */
import { DEV } from 'esm-env';
import {
	CLEAN,
	DERIVED,
	DESTROYED,
	DIRTY,
	EFFECT_HAS_DERIVED,
	MAYBE_DIRTY,
	UNOWNED
} from '../constants.js';
import {
	active_reaction,
	active_effect,
	remove_reactions,
	set_signal_status,
	skip_reaction,
	update_reaction,
	increment_version,
	set_active_effect
} from '../runtime.js';
import { equals, safe_equals } from './equality.js';
import * as e from '../errors.js';
import { destroy_effect } from './effects.js';
import { inspect_effects, set_inspect_effects } from './sources.js';
 
/**
 * @template V
 * @param {() => V} fn
 * @returns {Derived<V>}
 */
/*#__NO_SIDE_EFFECTS__*/
export function derived(fn) {
	let flags = DERIVED | DIRTY;
 
	if (active_effect === null) {
		flags |= UNOWNED;
	} else {
		// Since deriveds are evaluated lazily, any effects created inside them are
		// created too late to ensure that the parent effect is added to the tree
		active_effect.f |= EFFECT_HAS_DERIVED;
	}
 
	/** @type {Derived<V>} */
	const signal = {
		children: null,
		deps: null,
		equals,
		f: flags,
		fn,
		reactions: null,
		v: /** @type {V} */ (null),
		version: 0,
		parent: active_effect
	};
 
	if (active_reaction !== null && (active_reaction.f & DERIVED) !== 0) {
		var derived = /** @type {Derived} */ (active_reaction);
		(derived.children ??= []).push(signal);
	}
	if (active_effect !== null) {
		(active_effect.deriveds ??= []).push(signal);
	}
 
	return signal;
}
 
/**
 * @template V
 * @param {() => V} fn
 * @returns {Derived<V>}
 */
/*#__NO_SIDE_EFFECTS__*/
export function derived_safe_equal(fn) {
	const signal = derived(fn);
	signal.equals = safe_equals;
	return signal;
}
 
/**
 * @param {Derived} derived
 * @returns {void}
 */
function destroy_derived_children(derived) {
	var children = derived.children;
 
	if (children !== null) {
		derived.children = null;
 
		for (var i = 0; i < children.length; i += 1) {
			var child = children[i];
			if ((child.f & DERIVED) !== 0) {
				destroy_derived(/** @type {Derived} */ (child));
			} else {
				destroy_effect(/** @type {Effect} */ (child));
			}
		}
	}
}
 
/**
 * The currently updating deriveds, used to detect infinite recursion
 * in dev mode and provide a nicer error than 'too much recursion'
 * @type {Derived[]}
 */
let stack = [];
 
/**
 * @template T
 * @param {Derived} derived
 * @returns {T}
 */
export function execute_derived(derived) {
	var value;
	var prev_active_effect = active_effect;
 
	set_active_effect(derived.parent);
 
	if (DEV) {
		let prev_inspect_effects = inspect_effects;
		set_inspect_effects(new Set());
		try {
			if (stack.includes(derived)) {
				e.derived_references_self();
			}
 
			stack.push(derived);
 
			destroy_derived_children(derived);
			value = update_reaction(derived);
		} finally {
			set_active_effect(prev_active_effect);
			set_inspect_effects(prev_inspect_effects);
			stack.pop();
		}
	} else {
		try {
			destroy_derived_children(derived);
			value = update_reaction(derived);
		} finally {
			set_active_effect(prev_active_effect);
		}
	}
 
	return value;
}
 
/**
 * @param {Derived} derived
 * @returns {void}
 */
export function update_derived(derived) {
	var value = execute_derived(derived);
	var status =
		(skip_reaction || (derived.f & UNOWNED) !== 0) && derived.deps !== null ? MAYBE_DIRTY : CLEAN;
 
	set_signal_status(derived, status);
 
	if (!derived.equals(value)) {
		derived.v = value;
		derived.version = increment_version();
	}
}
 
/**
 * @param {Derived} signal
 * @returns {void}
 */
export function destroy_derived(signal) {
	destroy_derived_children(signal);
	remove_reactions(signal, 0);
	set_signal_status(signal, DESTROYED);
 
	// TODO we need to ensure we remove the derived from any parent derives
	signal.v = signal.children = signal.deps = signal.reactions = null;
}