All files / src/internal/client/dom/blocks each.js

99.09% Statements 550/555
97.58% Branches 121/124
100% Functions 9/9
99.08% Lines 541/546

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 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 5472x 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 2x 2x 2x 462x 462x 2x 2x 2x 2x 2x 2x 8070x 8070x 2x 2x 2x 2x 2x 2x 2x 2x 2x 396x 396x 396x 396x 396x 396x 2067x 2067x 396x 396x 396x 396x 396x 2x 2x 2x 2x 2x 2x 2x 2x 396x 396x 392x 2059x 2059x 2053x 2053x 2053x 2059x 2059x 396x 396x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 967x 967x 967x 967x 967x 967x 967x 967x 212x 212x 212x 212x 212x 212x 967x 967x 452x 452x 967x 967x 967x 967x 967x 2275x 2275x 2275x 2275x 2275x 62x 2275x 2275x 2275x 2275x 2275x 2275x 2275x 2275x 452x 452x 452x 25x 25x 25x 25x 25x 25x 25x 452x 2275x 2275x 2275x 427x 427x 427x 427x 427x 427x 427x 2151x 2151x 182x 2151x 24x 24x 24x 24x 24x 24x 24x 2127x 2127x 2127x 2127x 2127x 2127x 2127x 2127x 427x 427x 427x 421x 421x 427x 2275x 2275x 1872x 1872x 2274x 2275x 76x 37x 2x 37x 35x 35x 76x 20x 20x 20x 20x 76x 2274x 2275x 49x 49x 49x 967x 967x 967x 452x 452x 967x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1872x 1872x 1872x 1872x 1872x 1872x 1872x 1872x 1872x 1872x 1872x 1872x 1872x 1872x 1872x 1872x 1872x 1872x 1872x 1872x 1872x 1872x 1872x 1872x 1872x 1872x 1872x 1872x 1872x 1872x 1872x 1872x 1872x 1872x 1872x 1872x 1872x 27x 129x 129x 129x 129x 129x 92x 92x 92x 129x 27x 1872x 1872x 10037x 10037x 10037x 10037x 10037x 4366x 4366x 4366x 4366x 4366x 4366x 4366x 4366x 4366x 4366x 4366x 4366x 4366x 4366x 4366x 4366x 4366x 4366x 4366x 4366x 4366x 4366x 5671x 5914x 5634x 5634x 5671x 10005x 12x 12x       12x 5671x 10005x 2047x 1191x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 393x 393x 377x 377x 2405x 2405x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 1191x 814x 814x 814x 814x 814x 814x 814x 814x 814x 814x 1191x 1191x 1191x 856x 856x 856x 856x 2047x 3783x 3783x 3783x 3783x 856x 2047x     856x 856x 856x 4480x 4480x 4480x 4480x 4480x 1871x 1872x 559x 559x 559x 1503x 1503x 1503x 559x 559x 559x 559x 396x 396x 396x 2x 2x 2x 2x 2x 2x 2x 2x 396x 396x 396x 559x 1871x 1872x 27x 27x 25x 92x 92x 27x 27x 1871x 1872x 1872x 1872x 2x 2x 2x 2x 2x 2x 2x 2x 5634x 5634x 5634x 5634x 5634x 5634x 66x 5634x 5568x 5568x 5634x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 6493x 6493x 6493x 6493x 6493x 6493x 6493x 6493x 6493x 6493x 6493x 6493x 6493x 6493x 6493x 6493x 6493x 6493x 6493x 6493x 6493x 6493x 6493x 6493x 6493x 6493x 6493x 6493x 6493x 1047x 6493x 5445x 5445x 5445x 6492x 6493x 519x 519x 519x 6492x 6492x 6493x 6493x 6493x 6493x 2x 2x 2x 2x 2x 2x 1207x 1207x 1207x 1207x 1207x 1207x 1207x 1324x 1324x 1324x 1324x 1207x 2x 2x 2x 2x 2x 2x 5628x 5628x 659x 5628x 4969x 4969x 4969x 5628x 5628x 4800x 4800x 4800x 5628x  
/** @import { EachItem, EachState, Effect, MaybeSource, Source, TemplateNode, TransitionManager, Value } from '#client' */
import {
	EACH_INDEX_REACTIVE,
	EACH_IS_ANIMATED,
	EACH_IS_CONTROLLED,
	EACH_ITEM_IMMUTABLE,
	EACH_ITEM_REACTIVE,
	HYDRATION_END,
	HYDRATION_START_ELSE
} from '../../../../constants.js';
import {
	hydrate_next,
	hydrate_node,
	hydrating,
	remove_nodes,
	set_hydrate_node,
	set_hydrating
} from '../hydration.js';
import {
	clear_text_content,
	create_text,
	get_first_child,
	get_next_sibling
} from '../operations.js';
import {
	block,
	branch,
	destroy_effect,
	run_out_transitions,
	pause_children,
	pause_effect,
	resume_effect
} from '../../reactivity/effects.js';
import { source, mutable_source, set } from '../../reactivity/sources.js';
import { array_from, is_array } from '../../../shared/utils.js';
import { INERT } from '../../constants.js';
import { queue_micro_task } from '../task.js';
import { current_effect } from '../../runtime.js';
 
/**
 * The row of a keyed each block that is currently updating. We track this
 * so that `animate:` directives have something to attach themselves to
 * @type {EachItem | null}
 */
export let current_each_item = null;
 
/** @param {EachItem | null} item */
export function set_current_each_item(item) {
	current_each_item = item;
}
 
/**
 * @param {any} _
 * @param {number} i
 */
export function index(_, i) {
	return i;
}
 
/**
 * Pause multiple effects simultaneously, and coordinate their
 * subsequent destruction. Used in each blocks
 * @param {EachState} state
 * @param {EachItem[]} items
 * @param {null | Node} controlled_anchor
 * @param {Map<any, EachItem>} items_map
 */
function pause_effects(state, items, controlled_anchor, items_map) {
	/** @type {TransitionManager[]} */
	var transitions = [];
	var length = items.length;
 
	for (var i = 0; i < length; i++) {
		pause_children(items[i].e, transitions, true);
	}
 
	var is_controlled = length > 0 && transitions.length === 0 && controlled_anchor !== null;
	// If we have a controlled anchor, it means that the each block is inside a single
	// DOM element, so we can apply a fast-path for clearing the contents of the element.
	if (is_controlled) {
		var parent_node = /** @type {Element} */ (
			/** @type {Element} */ (controlled_anchor).parentNode
		);
		clear_text_content(parent_node);
		parent_node.append(/** @type {Element} */ (controlled_anchor));
		items_map.clear();
		link(state, items[0].prev, items[length - 1].next);
	}
 
	run_out_transitions(transitions, () => {
		for (var i = 0; i < length; i++) {
			var item = items[i];
			if (!is_controlled) {
				items_map.delete(item.k);
				link(state, item.prev, item.next);
			}
			destroy_effect(item.e, !is_controlled);
		}
	});
}
 
/**
 * @template V
 * @param {Element | Comment} node The next sibling node, or the parent node if this is a 'controlled' block
 * @param {number} flags
 * @param {() => V[]} get_collection
 * @param {(value: V, index: number) => any} get_key
 * @param {(anchor: Node, item: MaybeSource<V>, index: MaybeSource<number>) => void} render_fn
 * @param {null | ((anchor: Node) => void)} fallback_fn
 * @returns {void}
 */
export function each(node, flags, get_collection, get_key, render_fn, fallback_fn = null) {
	var anchor = node;
 
	/** @type {EachState} */
	var state = { flags, items: new Map(), first: null };
 
	var is_controlled = (flags & EACH_IS_CONTROLLED) !== 0;
 
	if (is_controlled) {
		var parent_node = /** @type {Element} */ (node);
 
		anchor = hydrating
			? set_hydrate_node(/** @type {Comment | Text} */ (get_first_child(parent_node)))
			: parent_node.appendChild(create_text());
	}
 
	if (hydrating) {
		hydrate_next();
	}
 
	/** @type {Effect | null} */
	var fallback = null;
 
	block(() => {
		var collection = get_collection();
 
		var array = is_array(collection)
			? collection
			: collection == null
				? []
				: array_from(collection);
 
		var length = array.length;
 
		/** `true` if there was a hydration mismatch. Needs to be a `let` or else it isn't treeshaken out */
		let mismatch = false;
 
		if (hydrating) {
			var is_else = /** @type {Comment} */ (anchor).data === HYDRATION_START_ELSE;
 
			if (is_else !== (length === 0)) {
				// hydration mismatch — remove the server-rendered DOM and start over
				anchor = remove_nodes();
 
				set_hydrate_node(anchor);
				set_hydrating(false);
				mismatch = true;
			}
		}
 
		// this is separate to the previous block because `hydrating` might change
		if (hydrating) {
			/** @type {EachItem | null} */
			var prev = null;
 
			/** @type {EachItem} */
			var item;
 
			for (var i = 0; i < length; i++) {
				if (
					hydrate_node.nodeType === 8 &&
					/** @type {Comment} */ (hydrate_node).data === HYDRATION_END
				) {
					// The server rendered fewer items than expected,
					// so break out and continue appending non-hydrated items
					anchor = /** @type {Comment} */ (hydrate_node);
					mismatch = true;
					set_hydrating(false);
					break;
				}
 
				var value = array[i];
				var key = get_key(value, i);
				item = create_item(hydrate_node, state, prev, null, value, key, i, render_fn, flags);
				state.items.set(key, item);
 
				prev = item;
			}
 
			// remove excess nodes
			if (length > 0) {
				set_hydrate_node(remove_nodes());
			}
		}
 
		if (!hydrating) {
			reconcile(array, state, anchor, render_fn, flags, get_key);
		}
 
		if (fallback_fn !== null) {
			if (length === 0) {
				if (fallback) {
					resume_effect(fallback);
				} else {
					fallback = branch(() => fallback_fn(anchor));
				}
			} else if (fallback !== null) {
				pause_effect(fallback, () => {
					fallback = null;
				});
			}
		}
 
		if (mismatch) {
			// continue in hydration mode
			set_hydrating(true);
		}
	});
 
	if (hydrating) {
		anchor = hydrate_node;
	}
}
 
/**
 * Add, remove, or reorder items output by an each block as its input changes
 * @template V
 * @param {Array<V>} array
 * @param {EachState} state
 * @param {Element | Comment | Text} anchor
 * @param {(anchor: Node, item: MaybeSource<V>, index: number | Source<number>) => void} render_fn
 * @param {number} flags
 * @param {(value: V, index: number) => any} get_key
 * @returns {void}
 */
function reconcile(array, state, anchor, render_fn, flags, get_key) {
	var is_animated = (flags & EACH_IS_ANIMATED) !== 0;
	var should_update = (flags & (EACH_ITEM_REACTIVE | EACH_INDEX_REACTIVE)) !== 0;
 
	var length = array.length;
	var items = state.items;
	var first = state.first;
	var current = first;
 
	/** @type {undefined | Set<EachItem>} */
	var seen;
 
	/** @type {EachItem | null} */
	var prev = null;
 
	/** @type {undefined | Set<EachItem>} */
	var to_animate;
 
	/** @type {EachItem[]} */
	var matched = [];
 
	/** @type {EachItem[]} */
	var stashed = [];
 
	/** @type {V} */
	var value;
 
	/** @type {any} */
	var key;
 
	/** @type {EachItem | undefined} */
	var item;
 
	/** @type {number} */
	var i;
 
	if (is_animated) {
		for (i = 0; i < length; i += 1) {
			value = array[i];
			key = get_key(value, i);
			item = items.get(key);
 
			if (item !== undefined) {
				item.a?.measure();
				(to_animate ??= new Set()).add(item);
			}
		}
	}
 
	for (i = 0; i < length; i += 1) {
		value = array[i];
		key = get_key(value, i);
		item = items.get(key);
 
		if (item === undefined) {
			var child_anchor = current ? /** @type {TemplateNode} */ (current.e.nodes_start) : anchor;
 
			prev = create_item(
				child_anchor,
				state,
				prev,
				prev === null ? state.first : prev.next,
				value,
				key,
				i,
				render_fn,
				flags
			);
 
			items.set(key, prev);
 
			matched = [];
			stashed = [];
 
			current = prev.next;
			continue;
		}
 
		if (should_update) {
			update_item(item, value, i, flags);
		}
 
		if ((item.e.f & INERT) !== 0) {
			resume_effect(item.e);
			if (is_animated) {
				item.a?.unfix();
				(to_animate ??= new Set()).delete(item);
			}
		}
 
		if (item !== current) {
			if (seen !== undefined && seen.has(item)) {
				if (matched.length < stashed.length) {
					// more efficient to move later items to the front
					var start = stashed[0];
					var j;
 
					prev = start.prev;
 
					var a = matched[0];
					var b = matched[matched.length - 1];
 
					for (j = 0; j < matched.length; j += 1) {
						move(matched[j], start, anchor);
					}
 
					for (j = 0; j < stashed.length; j += 1) {
						seen.delete(stashed[j]);
					}
 
					link(state, a.prev, b.next);
					link(state, prev, a);
					link(state, b, start);
 
					current = start;
					prev = b;
					i -= 1;
 
					matched = [];
					stashed = [];
				} else {
					// more efficient to move earlier items to the back
					seen.delete(item);
					move(item, current, anchor);
 
					link(state, item.prev, item.next);
					link(state, item, prev === null ? state.first : prev.next);
					link(state, prev, item);
 
					prev = item;
				}
 
				continue;
			}
 
			matched = [];
			stashed = [];
 
			while (current !== null && current.k !== key) {
				(seen ??= new Set()).add(current);
				stashed.push(current);
				current = current.next;
			}
 
			if (current === null) {
				continue;
			}
 
			item = current;
		}
 
		matched.push(item);
		prev = item;
		current = item.next;
	}
 
	if (current !== null || seen !== undefined) {
		var to_destroy = seen === undefined ? [] : array_from(seen);
 
		while (current !== null) {
			to_destroy.push(current);
			current = current.next;
		}
 
		var destroy_length = to_destroy.length;
 
		if (destroy_length > 0) {
			var controlled_anchor = (flags & EACH_IS_CONTROLLED) !== 0 && length === 0 ? anchor : null;
 
			if (is_animated) {
				for (i = 0; i < destroy_length; i += 1) {
					to_destroy[i].a?.measure();
				}
 
				for (i = 0; i < destroy_length; i += 1) {
					to_destroy[i].a?.fix();
				}
			}
 
			pause_effects(state, to_destroy, controlled_anchor, items);
		}
	}
 
	if (is_animated) {
		queue_micro_task(() => {
			if (to_animate === undefined) return;
			for (item of to_animate) {
				item.a?.apply();
			}
		});
	}
 
	/** @type {Effect} */ (current_effect).first = state.first && state.first.e;
	/** @type {Effect} */ (current_effect).last = prev && prev.e;
}
 
/**
 * @param {EachItem} item
 * @param {any} value
 * @param {number} index
 * @param {number} type
 * @returns {void}
 */
function update_item(item, value, index, type) {
	if ((type & EACH_ITEM_REACTIVE) !== 0) {
		set(item.v, value);
	}
 
	if ((type & EACH_INDEX_REACTIVE) !== 0) {
		set(/** @type {Value<number>} */ (item.i), index);
	} else {
		item.i = index;
	}
}
 
/**
 * @template V
 * @param {Node} anchor
 * @param {EachState} state
 * @param {EachItem | null} prev
 * @param {EachItem | null} next
 * @param {V} value
 * @param {unknown} key
 * @param {number} index
 * @param {(anchor: Node, item: V | Source<V>, index: number | Value<number>) => void} render_fn
 * @param {number} flags
 * @returns {EachItem}
 */
function create_item(anchor, state, prev, next, value, key, index, render_fn, flags) {
	var previous_each_item = current_each_item;
 
	try {
		var reactive = (flags & EACH_ITEM_REACTIVE) !== 0;
		var mutable = (flags & EACH_ITEM_IMMUTABLE) === 0;
 
		var v = reactive ? (mutable ? mutable_source(value) : source(value)) : value;
		var i = (flags & EACH_INDEX_REACTIVE) === 0 ? index : source(index);
 
		/** @type {EachItem} */
		var item = {
			i,
			v,
			k: key,
			a: null,
			// @ts-expect-error
			e: null,
			prev,
			next
		};
 
		current_each_item = item;
		item.e = branch(() => render_fn(anchor, v, i), hydrating);
 
		item.e.prev = prev && prev.e;
		item.e.next = next && next.e;
 
		if (prev === null) {
			state.first = item;
		} else {
			prev.next = item;
			prev.e.next = item.e;
		}
 
		if (next !== null) {
			next.prev = item;
			next.e.prev = item.e;
		}
 
		return item;
	} finally {
		current_each_item = previous_each_item;
	}
}
 
/**
 * @param {EachItem} item
 * @param {EachItem | null} next
 * @param {Text | Element | Comment} anchor
 */
function move(item, next, anchor) {
	var end = item.next ? /** @type {TemplateNode} */ (item.next.e.nodes_start) : anchor;
 
	var dest = next ? /** @type {TemplateNode} */ (next.e.nodes_start) : anchor;
	var node = /** @type {TemplateNode} */ (item.e.nodes_start);
 
	while (node !== end) {
		var next_node = /** @type {TemplateNode} */ (get_next_sibling(node));
		dest.before(node);
		node = next_node;
	}
}
 
/**
 * @param {EachState} state
 * @param {EachItem | null} prev
 * @param {EachItem | null} next
 */
function link(state, prev, next) {
	if (prev === null) {
		state.first = next;
	} else {
		prev.next = next;
		prev.e.next = next && next.e;
	}
 
	if (next !== null) {
		next.prev = prev;
		next.e.prev = prev && prev.e;
	}
}