All files / src/internal/client/dev inspect.js

96.77% Statements 60/62
95.23% Branches 20/21
100% Functions 2/2
96.61% Lines 57/59

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 602x 2x 2x 2x 2x 2x 2x 2x 2x 2x 22x 22x 22x 22x 22x 44x 44x 22x 22x 2x 2x 2x 2x 2x 2x 2x 116x 116x 78x 78x 78x 18x 18x 18x 60x 60x 60x 60x 78x 60x 60x 60x 60x 60x 72x 72x 72x 34x 34x 72x 60x 60x 78x     78x 98x 116x 116x  
import { snapshot } from '../proxy.js';
import { inspect_effect, validate_effect } from '../reactivity/effects.js';
import { array_prototype, get_prototype_of, object_prototype } from '../utils.js';
 
/**
 * @param {() => any[]} get_value
 * @param {Function} [inspector]
 */
// eslint-disable-next-line no-console
export function inspect(get_value, inspector = console.log) {
	validate_effect('$inspect');
 
	let initial = true;
 
	inspect_effect(() => {
		inspector(initial ? 'init' : 'update', ...deep_snapshot(get_value()));
		initial = false;
	});
}
 
/**
 * Like `snapshot`, but recursively traverses into normal arrays/objects to find potential states in them.
 * @param {any} value
 * @param {Map<any, any>} visited
 * @returns {any}
 */
function deep_snapshot(value, visited = new Map()) {
	if (typeof value === 'object' && value !== null && !visited.has(value)) {
		const unstated = snapshot(value);
 
		if (unstated !== value) {
			visited.set(value, unstated);
			return unstated;
		}
 
		const prototype = get_prototype_of(value);
 
		// Only deeply snapshot plain objects and arrays
		if (prototype === object_prototype || prototype === array_prototype) {
			let contains_unstated = false;
			/** @type {any} */
			const nested_unstated = Array.isArray(value) ? [] : {};
 
			for (let key in value) {
				const result = deep_snapshot(value[key], visited);
				nested_unstated[key] = result;
				if (result !== value[key]) {
					contains_unstated = true;
				}
			}
 
			visited.set(value, contains_unstated ? nested_unstated : value);
		} else {
			visited.set(value, value);
		}
	}
 
	return visited.get(value) ?? value;
}