All files / src/reactivity set.js

95.06% Statements 154/162
91.66% Branches 22/24
92.3% Functions 12/13
94.93% Lines 150/158

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 1592x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 7x 20x 20x 7x 7x 16x 16x 16x 16x 16x 16x 3x 3x 3x 3x 3x 3x 12x 12x 2x 2x 2x 12x 12x 3x 3x 12x 12x         12x 12x 3x 16x 16x 16x 17x 17x 17x 17x 17x 17x 7x 7x 7x 7x 7x 10x 10x 10x 10x 10x 10x 10x 10x 16x 16x 16x 15x 15x 15x 15x 15x 15x 15x 15x 16x 16x 16x 9x 9x 9x 9x 9x 3x 3x 3x 9x 9x 8x 8x 8x 9x 9x 9x 16x 16x 3x     3x 3x 3x 3x 3x     3x 3x 3x 3x 3x 16x 16x 27x 27x 16x 16x 30x 30x 30x 16x 16x 2x 2x 2x 16x 16x 25x 25x 16x 16x 25x 25x 16x  
/** @import { Source } from '#client' */
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';
 
var read_methods = ['forEach', 'isDisjointFrom', 'isSubsetOf', 'isSupersetOf'];
var set_like_methods = ['difference', 'intersection', 'symmetricDifference', 'union'];
 
var inited = false;
 
/**
 * @template T
 * @extends {Set<T>}
 */
export class SvelteSet extends Set {
	/** @type {Map<T, Source<boolean>>} */
	#sources = new Map();
	#version = source(0);
	#size = source(0);
 
	/**
	 * @param {Iterable<T> | null | undefined} [value]
	 */
	constructor(value) {
		super();
 
		// If the value is invalid then the native exception will fire here
		if (DEV) new Set(value);
 
		if (value) {
			for (var element of value) {
				super.add(element);
			}
			this.#size.v = super.size;
		}
 
		if (!inited) this.#init();
	}
 
	// We init as part of the first instance so that we can treeshake this class
	#init() {
		inited = true;
 
		var proto = SvelteSet.prototype;
		var set_proto = Set.prototype;
 
		for (const method of read_methods) {
			// @ts-ignore
			proto[method] = function (...v) {
				get(this.#version);
				// @ts-ignore
				return set_proto[method].apply(this, v);
			};
		}
 
		for (const method of set_like_methods) {
			// @ts-ignore
			proto[method] = function (...v) {
				get(this.#version);
				// @ts-ignore
				var set = /** @type {Set<T>} */ (set_proto[method].apply(this, v));
				return new SvelteSet(set);
			};
		}
	}
 
	/** @param {T} value */
	has(value) {
		var has = super.has(value);
		var sources = this.#sources;
		var s = sources.get(value);
 
		if (s === undefined) {
			if (!has) {
				// If the value doesn't exist, track the version in case it's added later
				// but don't create sources willy-nilly to track all possible values
				get(this.#version);
				return false;
			}
 
			s = source(true);
			sources.set(value, s);
		}
 
		get(s);
		return has;
	}
 
	/** @param {T} value */
	add(value) {
		if (!super.has(value)) {
			super.add(value);
			set(this.#size, super.size);
			increment(this.#version);
		}
 
		return this;
	}
 
	/** @param {T} value */
	delete(value) {
		var deleted = super.delete(value);
		var sources = this.#sources;
		var s = sources.get(value);
 
		if (s !== undefined) {
			sources.delete(value);
			set(s, false);
		}
 
		if (deleted) {
			set(this.#size, super.size);
			increment(this.#version);
		}
 
		return deleted;
	}
 
	clear() {
		if (super.size === 0) {
			return;
		}
		// Clear first, so we get nice console.log outputs with $inspect
		super.clear();
		var sources = this.#sources;
 
		for (var s of sources.values()) {
			set(s, false);
		}
 
		sources.clear();
		set(this.#size, 0);
		increment(this.#version);
	}
 
	keys() {
		return this.values();
	}
 
	values() {
		get(this.#version);
		return super.values();
	}
 
	entries() {
		get(this.#version);
		return super.entries();
	}
 
	[Symbol.iterator]() {
		return this.keys();
	}
 
	get size() {
		return get(this.#size);
	}
}