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

99.61% Statements 514/516
99.04% Branches 104/105
100% Functions 10/10
99.6% Lines 505/507

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 5082x 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 414x 414x 2x 2x 2x 2x 2x 2x 7861x 7861x 2x 2x 2x 2x 2x 2x 2x 2x 1811x 1811x 1811x 1811x 1811x 1811x 2059x 2059x 1811x 1811x 1811x 1811x 2x 2x 2x 2x 1811x 1811x 1807x 2051x 2051x 1807x 1807x 1811x 1811x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 927x 927x 927x 927x 927x 927x 199x 199x 199x 199x 97x 97x 199x 199x 927x 927x 927x 927x 927x 2209x 2209x 2209x 2209x 2209x 56x 2209x 2209x 2209x 2209x 2209x 2209x 2209x 2209x 86x 86x 86x 86x 10x 10x 86x 2209x 2209x 2209x 2209x 2209x 428x 428x 428x 26x 26x 26x 26x 26x 428x 2209x 2209x 2209x 402x 402x 402x 402x 402x 402x 402x 402x 402x 402x 2116x 2116x 2116x 2116x 6x 6x 6x 6x 6x 6x 2110x 2110x 2110x 2110x 2110x 2110x 2110x 2110x 2110x 2110x 402x 402x 402x 396x 24x 24x 24x 24x 396x 402x 2209x 2209x 1813x 1813x 2207x 2209x 76x 37x 2x 37x 35x 35x 76x 20x 20x 20x 20x 76x 2207x 2209x 32x 32x 32x 927x 927x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1813x 1813x 1813x 1813x 1813x 1813x 1813x 1813x 1813x 1813x 1813x 1813x 1813x 1813x 1813x 1813x 1813x 1813x 1813x 1813x 1813x 1813x 1813x 1813x 1813x 1813x 1813x 1813x 1813x 1813x 1813x 1813x 1813x 1813x 1813x 1813x 1813x 24x 114x 114x 114x 114x 114x 82x 82x 82x 114x 24x 1813x 1813x 9579x 9579x 9579x 9579x 9579x 4240x 4240x 4240x 4240x 4240x 4240x 4240x 4240x 4240x 4240x 4240x 4240x 4240x 4240x 4240x 4240x 4240x 4240x 4240x 5339x 5521x 5335x 5335x 5339x 5339x 5339x 9547x 1919x 1126x 314x 314x 314x 314x 314x 314x 314x 314x 314x 314x 314x 314x 314x 314x 314x 325x 325x 314x 314x 2024x 2024x 314x 314x 314x 314x 314x 314x 314x 1124x 812x 812x 812x 812x 812x 812x 812x 812x 812x 812x 1126x 1126x 1126x 793x 793x 793x 793x 1919x 3416x 3416x 3416x 3416x 793x 1919x     793x 793x 793x 4213x 4213x 4213x 4213x 4213x 1811x 1811x 1811x 1813x 1479x 1479x 1479x 1811x 1813x 1813x 1813x 1807x 2051x 2051x 2051x 2051x 1813x 1813x 1813x 24x 24x 24x 82x 82x 24x 24x 24x 1813x 2x 2x 2x 2x 2x 1210x 1210x 1210x 1210x 26x 26x 1184x 1184x 1184x 2x 2x 2x 2x 2x 2x 2x 2x 5335x 5335x 5335x 5335x 5335x 5335x 86x 5335x 5249x 5249x 5335x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 6350x 6350x 6350x 6350x 6350x 6350x 6350x 6350x 6350x 6350x 6350x 6350x 6350x 6350x 6350x 6350x 6350x 6350x 6350x 6350x 6350x 6350x 6350x 6350x 6350x 6350x 6350x 6350x 6350x 6350x 6350x 6350x 6350x 2x 2x 2x 2x 2x 1137x 1137x 1137x 1137x 1137x 28x 71x 71x 1137x 1109x 1109x 1137x 1137x 2x 2x 2x 2x 2x 2x 5429x 5429x 5429x 5429x  
import {
	EACH_INDEX_REACTIVE,
	EACH_IS_ANIMATED,
	EACH_IS_CONTROLLED,
	EACH_IS_STRICT_EQUALS,
	EACH_ITEM_REACTIVE,
	EACH_KEYED,
	HYDRATION_END_ELSE,
	HYDRATION_START
} from '../../../../constants.js';
import { hydrate_anchor, hydrate_nodes, hydrating, set_hydrating } from '../hydration.js';
import { empty } from '../operations.js';
import { remove } from '../reconciler.js';
import { untrack } from '../../runtime.js';
import {
	block,
	branch,
	destroy_effect,
	effect,
	run_out_transitions,
	pause_children,
	pause_effect,
	resume_effect
} from '../../reactivity/effects.js';
import { source, mutable_source, set } from '../../reactivity/sources.js';
import { is_array, is_frozen } from '../../utils.js';
import { STATE_SYMBOL } from '../../constants.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 {import('#client').EachItem | null}
 */
export let current_each_item = null;
 
/** @param {import('#client').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 {import('#client').EachItem[]} items
 * @param {null | Node} controlled_anchor
 * @param {() => void} [callback]
 */
function pause_effects(items, controlled_anchor, callback) {
	/** @type {import('#client').TransitionManager[]} */
	var transitions = [];
	var length = items.length;
 
	for (var i = 0; i < length; i++) {
		pause_children(items[i].e, transitions, true);
	}
 
	// 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 (length > 0 && transitions.length === 0 && controlled_anchor !== null) {
		var parent_node = /** @type {Element} */ (controlled_anchor.parentNode);
		parent_node.textContent = '';
		parent_node.append(controlled_anchor);
	}
 
	run_out_transitions(transitions, () => {
		for (var i = 0; i < length; i++) {
			destroy_effect(items[i].e);
		}
 
		if (callback !== undefined) callback();
	});
}
 
/**
 * @template V
 * @param {Element | Comment} anchor 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: import('#client').MaybeSource<V>, index: import('#client').MaybeSource<number>) => void} render_fn
 * @param {null | ((anchor: Node) => void)} fallback_fn
 * @returns {void}
 */
export function each(anchor, flags, get_collection, get_key, render_fn, fallback_fn = null) {
	/** @type {import('#client').EachState} */
	var state = { flags, items: new Map(), next: null };
 
	var is_controlled = (flags & EACH_IS_CONTROLLED) !== 0;
 
	if (is_controlled) {
		var parent_node = /** @type {Element} */ (anchor);
 
		anchor = hydrating
			? /** @type {Comment | Text} */ (
					hydrate_anchor(/** @type {Comment | Text} */ (parent_node.firstChild))
				)
			: parent_node.appendChild(empty());
	}
 
	/** @type {import('#client').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;
 
		// If we are working with an array that isn't proxied or frozen, then remove strict equality and ensure the items
		// are treated as reactive, so they get wrapped in a signal.
		var flags = state.flags;
		if ((flags & EACH_IS_STRICT_EQUALS) !== 0 && !is_frozen(array) && !(STATE_SYMBOL in array)) {
			flags ^= EACH_IS_STRICT_EQUALS;
 
			// Additionally if we're in an keyed each block, we'll need ensure the items are all wrapped in signals.
			if ((flags & EACH_KEYED) !== 0 && (flags & EACH_ITEM_REACTIVE) === 0) {
				flags ^= EACH_ITEM_REACTIVE;
			}
		}
 
		/** `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_END_ELSE;
 
			if (is_else !== (length === 0)) {
				// hydration mismatch — remove the server-rendered DOM and start over
				remove(hydrate_nodes);
				set_hydrating(false);
				mismatch = true;
			}
		}
 
		// this is separate to the previous block because `hydrating` might change
		if (hydrating) {
			/** @type {Node} */
			var child_anchor = hydrate_nodes[0];
 
			/** @type {import('#client').EachItem | import('#client').EachState} */
			var prev = state;
 
			/** @type {import('#client').EachItem} */
			var item;
 
			for (var i = 0; i < length; i++) {
				if (
					child_anchor.nodeType !== 8 ||
					/** @type {Comment} */ (child_anchor).data !== HYDRATION_START
				) {
					// If `nodes` is null, then that means that the server rendered fewer items than what
					// expected, so break out and continue appending non-hydrated items
					mismatch = true;
					set_hydrating(false);
					break;
				}
 
				child_anchor = hydrate_anchor(child_anchor);
				var value = array[i];
				var key = get_key(value, i);
				item = create_item(child_anchor, prev, null, value, key, i, render_fn, flags);
				state.items.set(key, item);
				child_anchor = /** @type {Comment} */ (child_anchor.nextSibling);
 
				prev = item;
			}
 
			// remove excess nodes
			if (length > 0) {
				while (child_anchor !== anchor) {
					var next = /** @type {import('#client').TemplateNode} */ (child_anchor.nextSibling);
					/** @type {import('#client').TemplateNode} */ (child_anchor).remove();
					child_anchor = next;
				}
			}
		}
 
		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);
		}
	});
}
 
/**
 * @template V
 * @param {Array<V>} array
 * @param {import('#client').EachState} state
 * @param {Element | Comment | Text} anchor
 * @param {(anchor: Node, item: import('#client').MaybeSource<V>, index: number | import('#client').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.next;
	var current = first;
 
	/** @type {Set<import('#client').EachItem>} */
	var seen = new Set();
 
	/** @type {import('#client').EachState | import('#client').EachItem} */
	var prev = state;
 
	/** @type {import('#client').EachItem[]} */
	var to_animate = [];
 
	/** @type {import('#client').EachItem[]} */
	var matched = [];
 
	/** @type {import('#client').EachItem[]} */
	var stashed = [];
 
	/** @type {V} */
	var value;
 
	/** @type {any} */
	var key;
 
	/** @type {import('#client').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.push(item);
			}
		}
	}
 
	for (i = 0; i < length; i += 1) {
		value = array[i];
		key = get_key(value, i);
		item = items.get(key);
 
		if (item === undefined) {
			prev = create_item(
				current ? get_first_child(current) : anchor,
				prev,
				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);
		}
 
		resume_effect(item.e);
 
		if (item !== current) {
			if (seen.has(item)) {
				if (matched.length < stashed.length) {
					// more efficient to move later items to the front
					var start = stashed[0];
					var local_anchor = get_first_child(start);
					var j;
 
					prev = start.prev;
 
					var a = matched[0];
					var b = matched[matched.length - 1];
 
					link(a.prev, b.next);
					link(prev, a);
					link(b, start);
 
					for (j = 0; j < matched.length; j += 1) {
						move(matched[j], local_anchor);
					}
 
					for (j = 0; j < stashed.length; j += 1) {
						seen.delete(stashed[j]);
					}
 
					current = start;
					prev = b;
					i -= 1;
 
					matched = [];
					stashed = [];
				} else {
					// more efficient to move earlier items to the back
					seen.delete(item);
					move(item, current ? get_first_child(current) : anchor);
 
					link(item.prev, item.next);
					link(item, prev.next);
					link(prev, item);
 
					prev = item;
				}
 
				continue;
			}
 
			matched = [];
			stashed = [];
 
			while (current !== null && current.k !== key) {
				seen.add(current);
				stashed.push(current);
				current = current.next;
			}
 
			if (current === null) {
				continue;
			}
 
			item = current;
		}
 
		matched.push(item);
		prev = item;
		current = item.next;
	}
 
	const to_destroy = Array.from(seen);
 
	while (current) {
		to_destroy.push(current);
		current = current.next;
	}
 
	var controlled_anchor = (flags & EACH_IS_CONTROLLED) !== 0 && length === 0 ? anchor : null;
 
	pause_effects(to_destroy, controlled_anchor, () => {
		for (var i = 0; i < to_destroy.length; i += 1) {
			var item = to_destroy[i];
			items.delete(item.k);
			link(item.prev, item.next);
		}
	});
 
	if (is_animated) {
		effect(() => {
			untrack(() => {
				for (item of to_animate) {
					item.a?.apply();
				}
			});
		});
	}
}
 
/**
 * @param {import('#client').EachItem} item
 * @returns {Text | Element | Comment}
 */
function get_first_child(item) {
	var current = item.e.dom;
 
	if (is_array(current)) {
		return /** @type {Text | Element | Comment} */ (current[0]);
	}
 
	return /** @type {Text | Element | Comment} */ (current);
}
 
/**
 * @param {import('#client').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 {import('#client').Value<number>} */ (item.i), index);
	} else {
		item.i = index;
	}
}
 
/**
 * @template V
 * @param {Node} anchor
 * @param {import('#client').EachItem | import('#client').EachState} prev
 * @param {import('#client').EachItem | null} next
 * @param {V} value
 * @param {unknown} key
 * @param {number} index
 * @param {(anchor: Node, item: V | import('#client').Source<V>, index: number | import('#client').Value<number>) => void} render_fn
 * @param {number} flags
 * @returns {import('#client').EachItem}
 */
function create_item(anchor, 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_IS_STRICT_EQUALS) === 0;
 
		var v = reactive ? (mutable ? mutable_source(value) : source(value)) : value;
		var i = (flags & EACH_INDEX_REACTIVE) === 0 ? index : source(index);
 
		/** @type {import('#client').EachItem} */
		var item = {
			i,
			v,
			k: key,
			a: null,
			// @ts-expect-error
			e: null,
			prev,
			next
		};
 
		prev.next = item;
		if (next !== null) next.prev = item;
 
		current_each_item = item;
		item.e = branch(() => render_fn(anchor, v, i));
 
		return item;
	} finally {
		current_each_item = previous_each_item;
	}
}
 
/**
 * @param {import('#client').EachItem} item
 * @param {Text | Element | Comment} anchor
 */
function move(item, anchor) {
	var dom = item.e.dom;
 
	if (dom !== null) {
		if (is_array(dom)) {
			for (var i = 0; i < dom.length; i++) {
				anchor.before(dom[i]);
			}
		} else {
			anchor.before(dom);
		}
	}
}
 
/**
 *
 * @param {import('#client').EachItem | import('#client').EachState} prev
 * @param {import('#client').EachItem | null} next
 */
function link(prev, next) {
	prev.next = next;
	if (next !== null) next.prev = prev;
}