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

98.86% Statements 524/530
95.53% Branches 107/112
100% Functions 25/25
98.85% Lines 518/524

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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 5252x 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 2x 2x 2x 2x 2x 2x 4438x     4438x 4438x     4438x 4438x     4438x 2x 2x 2x 2x 2x 2x 31645x 31645x 15644x 31645x 16001x 16001x 16001x 16001x 31645x 2x 2x 2x 2x 2x 2x 2x 2x 39710x 39710x 39710x 39710x 39710x 39710x 39710x 39710x 39710x 39710x 39710x 39710x 39710x 39710x 39710x 39710x 39710x 39710x 39710x 39710x 39710x 39710x 39710x 39710x 39710x 36010x 36010x 36010x 36010x 36010x 36010x 36010x 78x 78x 36010x 36010x 36010x 39710x 3110x 3110x 39632x 39632x 39632x 39632x 39710x 39710x 39710x 39710x 39710x 39710x 39710x 31643x 31633x 31633x 31643x 31643x 31643x 12x 12x 31643x 39632x 39632x 39632x 2x 2x 2x 2x 2x 2x 16x 4x 4x 12x 12x 12x 2x 2x 2x 2x 2x 590x 590x 590x 590x 590x 2x 2x 2x 2x 2x 2x 2286x 2286x 2286x 2286x 2286x 2286x 2286x 2262x 2286x 2286x 2286x 2286x 2286x 2286x 2286x 2286x 2286x 2286x 2260x 2260x 2286x 26x 26x 26x 2286x 2x 2x 2x 2x 2x 2x 2x 2130x 2130x 2130x 2130x 2130x 2130x 2130x 2130x 2x 2x 2x 22x 22x 2x 2x 2x 2x 2x 2x 2x 3222x 3222x 3177x 3222x 3222x 2x 2x 2x 2x 2x 2x 3110x 3110x 2x 2x 2x 2x 2x 2x 2x 4400x 4400x 4400x 4400x 4400x 4400x 4400x 6858x 6858x 6858x 6858x 6858x 4853x 4853x 4853x 4853x 4400x 4400x 2x 2x 2309x 2309x 2309x 2701x 2701x 2701x 2701x 4901x 4901x 4901x 2004x 2004x 4901x 4901x 4901x 2701x 2701x 2309x 2309x 2x 2x 2x 2x 2x 2x 18636x 18636x 2x 2x 2x 2x 2x 2x 8683x 8683x 8683x 8683x 8683x 8683x 8683x 2x 2x 2x 2x 2x 2x 2493x 2493x 2x 2x 2x 2x 2x 2x 11637x 11637x 2x 2x 2x 2x 2x 93855x 93855x 6574x 6574x 6574x 6574x 6574x 6574x 6574x 6574x 6574x 6574x 6574x 93855x 2x 2x 2x 2x 2x 2x 2x 38916x 38916x 38916x 5928x 5928x 5928x 5928x 5928x 21938x 21938x 21938x 21938x 21938x 21938x 21938x 5928x 5928x 5928x 38916x 38916x 38916x 38916x 38916x 38916x 405x 465x 465x 405x 38916x 38916x 38916x 38916x 38916x 38916x 38916x 2845x 2845x 38916x 38916x 38916x 38916x 38916x 38916x 38916x 38916x 38916x 38916x 38916x 38916x 2x 2x 2x 2x 2x 2x 2x 3129x 3129x 3129x 3129x 3129x 3129x 3129x 3129x 3123x 3123x 3123x 3129x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 767x 767x 767x 767x 767x 767x 687x 687x 767x 767x 2x 2x 2x 2x 2x 2x 1158x 1158x 208x 208x 258x 258x 1158x 950x 950x 1158x 2x 2x 2x 2x 2x 2x 2x 5756x 5756x 5756x 5756x 262x 310x 258x 258x 310x 262x 5756x 5756x 5756x 5756x 2957x 2957x 2957x 2957x 2957x 2957x 2957x 2957x 5756x 2x 2x 2x 2x 2x 2x 2x 96x 96x 2x 2x 2x 2x 2x 168x 168x 128x 128x 128x 128x 160x 30x 30x 128x 128x 128x 160x 72x 72x 72x 72x 72x 72x 72x 72x 128x 160x 68x 86x 78x 78x 86x 68x 168x  
import {
	check_dirtiness,
	current_component_context,
	current_effect,
	current_reaction,
	destroy_effect_children,
	dev_current_component_function,
	update_effect,
	get,
	is_destroying_effect,
	is_flushing_effect,
	remove_reactions,
	schedule_effect,
	set_current_reaction,
	set_is_destroying_effect,
	set_is_flushing_effect,
	set_signal_status,
	untrack
} from '../runtime.js';
import {
	DIRTY,
	BRANCH_EFFECT,
	RENDER_EFFECT,
	EFFECT,
	DESTROYED,
	INERT,
	EFFECT_RAN,
	BLOCK_EFFECT,
	ROOT_EFFECT,
	EFFECT_TRANSPARENT,
	DERIVED,
	UNOWNED,
	CLEAN,
	INSPECT_EFFECT,
	HEAD_EFFECT
} from '../constants.js';
import { set } from './sources.js';
import * as e from '../errors.js';
import { DEV } from 'esm-env';
import { define_property } from '../utils.js';
 
/**
 * @param {'$effect' | '$effect.pre' | '$inspect'} rune
 */
export function validate_effect(rune) {
	if (current_effect === null && current_reaction === null) {
		e.effect_orphan(rune);
	}
 
	if (current_reaction !== null && (current_reaction.f & UNOWNED) !== 0) {
		e.effect_in_unowned_derived();
	}
 
	if (is_destroying_effect) {
		e.effect_in_teardown(rune);
	}
}
 
/**
 * @param {import("#client").Effect} effect
 * @param {import("#client").Reaction} parent_effect
 */
export function push_effect(effect, parent_effect) {
	var parent_last = parent_effect.last;
	if (parent_last === null) {
		parent_effect.last = parent_effect.first = effect;
	} else {
		parent_last.next = effect;
		effect.prev = parent_last;
		parent_effect.last = effect;
	}
}
 
/**
 * @param {number} type
 * @param {null | (() => void | (() => void))} fn
 * @param {boolean} sync
 * @param {boolean} push
 * @returns {import('#client').Effect}
 */
function create_effect(type, fn, sync, push = true) {
	var is_root = (type & ROOT_EFFECT) !== 0;
 
	/** @type {import('#client').Effect} */
	var effect = {
		ctx: current_component_context,
		deps: null,
		nodes: null,
		f: type | DIRTY,
		first: null,
		fn,
		last: null,
		next: null,
		parent: is_root ? null : current_effect,
		prev: null,
		teardown: null,
		transitions: null,
		version: 0
	};
 
	if (DEV) {
		effect.component_function = dev_current_component_function;
	}
 
	if (sync) {
		var previously_flushing_effect = is_flushing_effect;
 
		try {
			set_is_flushing_effect(true);
			update_effect(effect);
			effect.f |= EFFECT_RAN;
		} catch (e) {
			destroy_effect(effect);
			throw e;
		} finally {
			set_is_flushing_effect(previously_flushing_effect);
		}
	} else if (fn !== null) {
		schedule_effect(effect);
	}
 
	// if an effect has no dependencies, no DOM and no teardown function,
	// don't bother adding it to the effect tree
	var inert =
		sync &&
		effect.deps === null &&
		effect.first === null &&
		effect.nodes === null &&
		effect.teardown === null;
 
	if (!inert && !is_root && push) {
		if (current_effect !== null) {
			push_effect(effect, current_effect);
		}
 
		// if we're in a derived, add the effect there too
		if (current_reaction !== null && (current_reaction.f & DERIVED) !== 0) {
			push_effect(effect, current_reaction);
		}
	}
 
	return effect;
}
 
/**
 * Internal representation of `$effect.tracking()`
 * @returns {boolean}
 */
export function effect_tracking() {
	if (current_reaction === null) {
		return false;
	}
 
	return (current_reaction.f & UNOWNED) === 0;
}
 
/**
 * @param {() => void} fn
 */
export function teardown(fn) {
	const effect = create_effect(RENDER_EFFECT, null, false);
	set_signal_status(effect, CLEAN);
	effect.teardown = fn;
	return effect;
}
 
/**
 * Internal representation of `$effect(...)`
 * @param {() => void | (() => void)} fn
 */
export function user_effect(fn) {
	validate_effect('$effect');
 
	// Non-nested `$effect(...)` in a component should be deferred
	// until the component is mounted
	var defer =
		current_effect !== null &&
		(current_effect.f & RENDER_EFFECT) !== 0 &&
		// TODO do we actually need this? removing them changes nothing
		current_component_context !== null &&
		!current_component_context.m;
 
	if (DEV) {
		define_property(fn, 'name', {
			value: '$effect'
		});
	}
 
	if (defer) {
		var context = /** @type {import('#client').ComponentContext} */ (current_component_context);
		(context.e ??= []).push(fn);
	} else {
		var signal = effect(fn);
		return signal;
	}
}
 
/**
 * Internal representation of `$effect.pre(...)`
 * @param {() => void | (() => void)} fn
 * @returns {import('#client').Effect}
 */
export function user_pre_effect(fn) {
	validate_effect('$effect.pre');
	if (DEV) {
		define_property(fn, 'name', {
			value: '$effect.pre'
		});
	}
	return render_effect(fn);
}
 
/** @param {() => void | (() => void)} fn */
export function inspect_effect(fn) {
	return create_effect(INSPECT_EFFECT, fn, true);
}
 
/**
 * Internal representation of `$effect.root(...)`
 * @param {() => void | (() => void)} fn
 * @returns {() => void}
 */
export function effect_root(fn) {
	const effect = create_effect(ROOT_EFFECT, fn, true);
	return () => {
		destroy_effect(effect);
	};
}
 
/**
 * @param {() => void | (() => void)} fn
 * @returns {import('#client').Effect}
 */
export function effect(fn) {
	return create_effect(EFFECT, fn, false);
}
 
/**
 * Internal representation of `$: ..`
 * @param {() => any} deps
 * @param {() => void | (() => void)} fn
 */
export function legacy_pre_effect(deps, fn) {
	var context = /** @type {import('#client').ComponentContextLegacy} */ (current_component_context);
 
	/** @type {{ effect: null | import('#client').Effect, ran: boolean }} */
	var token = { effect: null, ran: false };
	context.l.r1.push(token);
 
	token.effect = render_effect(() => {
		deps();
 
		// If this legacy pre effect has already run before the end of the reset, then
		// bail-out to emulate the same behavior.
		if (token.ran) return;
 
		token.ran = true;
		set(context.l.r2, true);
		untrack(fn);
	});
}
 
export function legacy_pre_effect_reset() {
	var context = /** @type {import('#client').ComponentContextLegacy} */ (current_component_context);
 
	render_effect(() => {
		if (!get(context.l.r2)) return;
 
		// Run dirty `$:` statements
		for (var token of context.l.r1) {
			var effect = token.effect;
 
			if (check_dirtiness(effect)) {
				update_effect(effect);
			}
 
			token.ran = false;
		}
 
		context.l.r2.v = false; // set directly to avoid rerunning this effect
	});
}
 
/**
 * @param {() => void | (() => void)} fn
 * @returns {import('#client').Effect}
 */
export function render_effect(fn) {
	return create_effect(RENDER_EFFECT, fn, true);
}
 
/**
 * @param {() => void | (() => void)} fn
 * @returns {import('#client').Effect}
 */
export function template_effect(fn) {
	if (DEV) {
		define_property(fn, 'name', {
			value: '{expression}'
		});
	}
	return render_effect(fn);
}
 
/**
 * @param {(() => void)} fn
 * @param {number} flags
 */
export function block(fn, flags = 0) {
	return create_effect(RENDER_EFFECT | BLOCK_EFFECT | flags, fn, true);
}
 
/**
 * @param {(() => void)} fn
 * @param {boolean} [push]
 */
export function branch(fn, push = true) {
	return create_effect(RENDER_EFFECT | BRANCH_EFFECT, fn, true, push);
}
 
/**
 * @param {import("#client").Effect} effect
 */
export function execute_effect_teardown(effect) {
	var teardown = effect.teardown;
	if (teardown !== null) {
		const previously_destroying_effect = is_destroying_effect;
		const previous_reaction = current_reaction;
		set_is_destroying_effect(true);
		set_current_reaction(null);
		try {
			teardown.call(null);
		} finally {
			set_is_destroying_effect(previously_destroying_effect);
			set_current_reaction(previous_reaction);
		}
	}
}
 
/**
 * @param {import('#client').Effect} effect
 * @param {boolean} [remove_dom]
 * @returns {void}
 */
export function destroy_effect(effect, remove_dom = true) {
	var removed = false;
 
	if ((remove_dom || (effect.f & HEAD_EFFECT) !== 0) && effect.nodes !== null) {
		/** @type {import('#client').TemplateNode | null} */
		var node = effect.nodes.start;
		var end = effect.nodes.end;
 
		while (node !== null) {
			/** @type {import('#client').TemplateNode | null} */
			var next =
				node === end ? null : /** @type {import('#client').TemplateNode} */ (node.nextSibling);
 
			node.remove();
			node = next;
		}
 
		removed = true;
	}
 
	destroy_effect_children(effect, remove_dom && !removed);
	remove_reactions(effect, 0);
	set_signal_status(effect, DESTROYED);
 
	if (effect.transitions) {
		for (const transition of effect.transitions) {
			transition.stop();
		}
	}
 
	execute_effect_teardown(effect);
 
	var parent = effect.parent;
 
	// If the parent doesn't have any children, then skip this work altogether
	if (parent !== null && (effect.f & BRANCH_EFFECT) !== 0 && parent.first !== null) {
		unlink_effect(effect);
	}
 
	// `first` and `child` are nulled out in destroy_effect_children
	effect.next =
		effect.prev =
		effect.teardown =
		effect.ctx =
		effect.deps =
		effect.parent =
		effect.fn =
		effect.nodes =
			null;
}
 
/**
 * Detach an effect from the effect tree, freeing up memory and
 * reducing the amount of work that happens on subsequent traversals
 * @param {import('#client').Effect} effect
 */
export function unlink_effect(effect) {
	var parent = effect.parent;
	var prev = effect.prev;
	var next = effect.next;
 
	if (prev !== null) prev.next = next;
	if (next !== null) next.prev = prev;
 
	if (parent !== null) {
		if (parent.first === effect) parent.first = next;
		if (parent.last === effect) parent.last = prev;
	}
}
 
/**
 * When a block effect is removed, we don't immediately destroy it or yank it
 * out of the DOM, because it might have transitions. Instead, we 'pause' it.
 * It stays around (in memory, and in the DOM) until outro transitions have
 * completed, and if the state change is reversed then we _resume_ it.
 * A paused effect does not update, and the DOM subtree becomes inert.
 * @param {import('#client').Effect} effect
 * @param {() => void} [callback]
 */
export function pause_effect(effect, callback) {
	/** @type {import('#client').TransitionManager[]} */
	var transitions = [];
 
	pause_children(effect, transitions, true);
 
	run_out_transitions(transitions, () => {
		destroy_effect(effect);
		if (callback) callback();
	});
}
 
/**
 * @param {import('#client').TransitionManager[]} transitions
 * @param {() => void} fn
 */
export function run_out_transitions(transitions, fn) {
	var remaining = transitions.length;
	if (remaining > 0) {
		var check = () => --remaining || fn();
		for (var transition of transitions) {
			transition.out(check);
		}
	} else {
		fn();
	}
}
 
/**
 * @param {import('#client').Effect} effect
 * @param {import('#client').TransitionManager[]} transitions
 * @param {boolean} local
 */
export function pause_children(effect, transitions, local) {
	if ((effect.f & INERT) !== 0) return;
	effect.f ^= INERT;
 
	if (effect.transitions !== null) {
		for (const transition of effect.transitions) {
			if (transition.is_global || local) {
				transitions.push(transition);
			}
		}
	}
 
	var child = effect.first;
 
	while (child !== null) {
		var sibling = child.next;
		var transparent = (child.f & EFFECT_TRANSPARENT) !== 0 || (child.f & BRANCH_EFFECT) !== 0;
		// TODO we don't need to call pause_children recursively with a linked list in place
		// it's slightly more involved though as we have to account for `transparent` changing
		// through the tree.
		pause_children(child, transitions, transparent ? local : false);
		child = sibling;
	}
}
 
/**
 * The opposite of `pause_effect`. We call this if (for example)
 * `x` becomes falsy then truthy: `{#if x}...{/if}`
 * @param {import('#client').Effect} effect
 */
export function resume_effect(effect) {
	resume_children(effect, true);
}
 
/**
 * @param {import('#client').Effect} effect
 * @param {boolean} local
 */
function resume_children(effect, local) {
	if ((effect.f & INERT) === 0) return;
	effect.f ^= INERT;
 
	// If a dependency of this effect changed while it was paused,
	// apply the change now
	if (check_dirtiness(effect)) {
		update_effect(effect);
	}
 
	var child = effect.first;
 
	while (child !== null) {
		var sibling = child.next;
		var transparent = (child.f & EFFECT_TRANSPARENT) !== 0 || (child.f & BRANCH_EFFECT) !== 0;
		// TODO we don't need to call resume_children recursively with a linked list in place
		// it's slightly more involved though as we have to account for `transparent` changing
		// through the tree.
		resume_children(child, transparent ? local : false);
		child = sibling;
	}
 
	if (effect.transitions !== null) {
		for (const transition of effect.transitions) {
			if (transition.is_global || local) {
				transition.in();
			}
		}
	}
}