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

100% Statements 31/31
100% Branches 5/5
100% Functions 1/1
100% Lines 28/28

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 292x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 28x 28x 28x 28x 28x 28x 28x 60x 54x 26x 26x 54x 54x 54x 28x 28x  
import { UNINITIALIZED } from '../../../../constants.js';
import { block, branch, pause_effect } from '../../reactivity/effects.js';
import { safe_not_equal } from '../../reactivity/equality.js';
 
/**
 * @template V
 * @param {Comment} anchor
 * @param {() => V} get_key
 * @param {(anchor: Node) => import('#client').Dom | void} render_fn
 * @returns {void}
 */
export function key_block(anchor, get_key, render_fn) {
	/** @type {V | typeof UNINITIALIZED} */
	let key = UNINITIALIZED;
 
	/** @type {import('#client').Effect} */
	let effect;
 
	block(() => {
		if (safe_not_equal(key, (key = get_key()))) {
			if (effect) {
				pause_effect(effect);
			}
 
			effect = branch(() => render_fn(anchor));
		}
	});
}