All files / src/reactivity map.js

98.78% Statements 162/164
96.15% Branches 25/26
100% Functions 13/13
98.74% Lines 157/159

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 1602x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 5x 5x 5x 17x 17x 5x 5x 5x 10x 10x 10x 20x 20x 10x 10x 10x 8x 8x 8x 3x 3x 3x 3x 3x 3x 5x 5x 5x 5x 10x 10x 10x 10x 10x 10x 1x 1x 1x 1x 1x 10x 10x 10x 8x 8x 8x 2x 2x 2x 2x 2x 2x 6x 6x 6x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x     10x 10x 10x 10x 10x 10x 8x 8x 8x 8x 7x 7x 7x 7x 7x 7x 1x 1x 1x 10x 10x 3x 3x 3x 3x 3x 8x 8x 3x 3x 3x 3x 3x 10x 10x 2x 2x 2x 10x 10x 6x 6x 6x 10x 10x 14x 14x 14x 14x 14x 14x 14x 10x 10x 12x 12x 10x 10x 17x 17x 10x  
import { DEV } from 'esm-env';
import { source, set } from '../internal/client/reactivity/sources.js';
import { get } from '../internal/client/runtime.js';
import { UNINITIALIZED } from '../constants.js';
import { map } from './utils.js';
 
/**
 * @template K
 * @template V
 * @extends {Map<K, V>}
 */
export class ReactiveMap extends Map {
	/** @type {Map<K, import('#client').Source<V>>} */
	#sources = new Map();
	#version = source(0);
	#size = source(0);
 
	/**
	 * @param {Iterable<readonly [K, V]> | null | undefined} [value]
	 */
	constructor(value) {
		super();
 
		// If the value is invalid then the native exception will fire here
		if (DEV) new Map(value);
 
		if (value) {
			var sources = this.#sources;
 
			for (var [key, v] of value) {
				sources.set(key, source(v));
			}
 
			this.#size.v = sources.size;
		}
	}
 
	#increment_version() {
		set(this.#version, this.#version.v + 1);
	}
 
	/** @param {K} key */
	has(key) {
		var s = this.#sources.get(key);
 
		if (s === undefined) {
			// We should always track the version in case
			// the Set ever gets this value in the future.
			get(this.#version);
 
			return false;
		}
 
		get(s);
		return true;
	}
 
	/**
	 * @param {(value: V, key: K, map: Map<K, V>) => void} callbackfn
	 * @param {any} [this_arg]
	 */
	forEach(callbackfn, this_arg) {
		get(this.#version);
 
		var bound_callbackfn = callbackfn.bind(this_arg);
		this.#sources.forEach((s, key) => bound_callbackfn(s.v, key, this));
	}
 
	/** @param {K} key */
	get(key) {
		var s = this.#sources.get(key);
 
		if (s === undefined) {
			// We should always track the version in case
			// the Set ever gets this value in the future.
			get(this.#version);
 
			return undefined;
		}
 
		return get(s);
	}
 
	/**
	 * @param {K} key
	 * @param {V} value
	 * */
	set(key, value) {
		var sources = this.#sources;
		var s = sources.get(key);
 
		if (s === undefined) {
			sources.set(key, source(value));
			set(this.#size, sources.size);
			this.#increment_version();
		} else {
			set(s, value);
		}
 
		return this;
	}
 
	/** @param {K} key */
	delete(key) {
		var sources = this.#sources;
		var s = sources.get(key);
 
		if (s !== undefined) {
			var removed = sources.delete(key);
			set(this.#size, sources.size);
			set(s, /** @type {V} */ (UNINITIALIZED));
			this.#increment_version();
			return removed;
		}
 
		return false;
	}
 
	clear() {
		var sources = this.#sources;
 
		if (sources.size !== 0) {
			set(this.#size, 0);
			for (var s of sources.values()) {
				set(s, /** @type {V} */ (UNINITIALIZED));
			}
			this.#increment_version();
		}
 
		sources.clear();
	}
 
	keys() {
		get(this.#version);
		return this.#sources.keys();
	}
 
	values() {
		get(this.#version);
		return map(this.#sources.values(), get, 'Map Iterator');
	}
 
	entries() {
		get(this.#version);
		return map(
			this.#sources.entries(),
			([key, source]) => /** @type {[K, V]} */ ([key, get(source)]),
			'Map Iterator'
		);
	}
 
	[Symbol.iterator]() {
		return this.entries();
	}
 
	get size() {
		return get(this.#size);
	}
}