All files / src/reactivity map.js

98.9% Statements 181/183
96.96% Branches 32/33
100% Functions 13/13
98.88% Lines 177/179

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 1802x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 8x 20x 20x 8x 8x 12x 12x 12x 12x 12x 12x 12x 12x 7x 7x 4x 4x 7x 3x 3x 3x 3x 3x 7x 9x 9x 9x 9x 12x 12x 12x 12x 12x 12x 1x 1x 1x 12x 12x 12x 16x 16x 16x 16x 10x 10x 4x 4x 10x 6x 6x 6x 6x 6x 10x 10x 10x 10x 10x 12x 12x 12x 12x 12x 12x 17x 17x 17x 17x 17x 17x 12x 12x 12x 17x 5x 5x 17x 17x 17x 12x 12x 12x 9x 9x 9x 9x 9x 7x 7x 7x 7x 7x 9x 9x 9x 12x 12x 3x     3x 3x 3x 3x 3x 10x 10x 3x 3x 3x 12x 12x 27x 27x 27x 27x 5x 12x 11x 11x 12x 5x 27x 27x 46x 46x 27x 12x 12x 2x 2x 2x 12x 12x 8x 8x 8x 12x 12x 18x 18x 18x 12x 12x 16x 16x 12x 12x 18x 18x 18x 12x  
import { DEV } from 'esm-env';
import { source, set } from '../internal/client/reactivity/sources.js';
import { get } from '../internal/client/runtime.js';
import { increment } from './utils.js';
 
/**
 * @template K
 * @template V
 * @extends {Map<K, V>}
 */
export class ReactiveMap extends Map {
	/** @type {Map<K, import('#client').Source<number>>} */
	#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) {
			for (var [key, v] of value) {
				super.set(key, v);
			}
			this.#size.v = super.size;
		}
	}
 
	/** @param {K} key */
	has(key) {
		var sources = this.#sources;
		var s = sources.get(key);
 
		if (s === undefined) {
			var ret = super.get(key);
			if (ret !== undefined) {
				s = source(0);
				sources.set(key, s);
			} else {
				// 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) {
		this.#read_all();
		super.forEach(callbackfn, this_arg);
	}
 
	/** @param {K} key */
	get(key) {
		var sources = this.#sources;
		var s = sources.get(key);
 
		if (s === undefined) {
			var ret = super.get(key);
			if (ret !== undefined) {
				s = source(0);
				sources.set(key, s);
			} else {
				// We should always track the version in case
				// the Set ever gets this value in the future.
				get(this.#version);
				return undefined;
			}
		}
 
		get(s);
		return super.get(key);
	}
 
	/**
	 * @param {K} key
	 * @param {V} value
	 * */
	set(key, value) {
		var sources = this.#sources;
		var s = sources.get(key);
		var prev_res = super.get(key);
		var res = super.set(key, value);
 
		if (s === undefined) {
			sources.set(key, source(0));
			set(this.#size, super.size);
			increment(this.#version);
		} else if (prev_res !== value) {
			increment(s);
		}
 
		return res;
	}
 
	/** @param {K} key */
	delete(key) {
		var sources = this.#sources;
		var s = sources.get(key);
		var res = super.delete(key);
 
		if (s !== undefined) {
			sources.delete(key);
			set(this.#size, super.size);
			set(s, -1);
			increment(this.#version);
		}
 
		return res;
	}
 
	clear() {
		if (super.size === 0) {
			return;
		}
		// Clear first, so we get nice console.log outputs with $inspect
		super.clear();
		var sources = this.#sources;
		set(this.#size, 0);
		for (var s of sources.values()) {
			set(s, -1);
		}
		increment(this.#version);
		sources.clear();
	}
 
	#read_all() {
		get(this.#version);
 
		var sources = this.#sources;
		if (this.#size.v !== sources.size) {
			for (var key of super.keys()) {
				if (!sources.has(key)) {
					sources.set(key, source(0));
				}
			}
		}
 
		for (var [, s] of this.#sources) {
			get(s);
		}
	}
 
	keys() {
		get(this.#version);
		return super.keys();
	}
 
	values() {
		this.#read_all();
		return super.values();
	}
 
	entries() {
		this.#read_all();
		return super.entries();
	}
 
	[Symbol.iterator]() {
		return this.entries();
	}
 
	get size() {
		get(this.#size);
		return super.size;
	}
}