From 1c9c386641b5125286265d37b4b2c4ea197c6052 Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Thu, 14 Mar 2024 15:55:30 +0000 Subject: [PATCH] address feedback --- packages/svelte/src/reactivity/set.js | 38 ++++++++++++++------------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/packages/svelte/src/reactivity/set.js b/packages/svelte/src/reactivity/set.js index 6259fc1347..bea9c1619c 100644 --- a/packages/svelte/src/reactivity/set.js +++ b/packages/svelte/src/reactivity/set.js @@ -3,16 +3,8 @@ import { source, set } from '../internal/client/reactivity/sources.js'; import { get } from '../internal/client/runtime.js'; import { make_iterable } from './utils.js'; -var methods = [ - 'difference', - 'forEach', - 'intersection', - 'isDisjointFrom', - 'isSubsetOf', - 'isSupersetOf', - 'symmetricDifference', - 'union' -]; +var read_methods = ['forEach', 'isDisjointFrom', 'isSubsetOf', 'isSupersetOf']; +var set_like_methods = ['difference', 'intersection', 'symmetricDifference', 'union']; var inited = false; @@ -26,7 +18,7 @@ export class ReactiveSet extends Set { #size = source(0); /** - * @param {Iterable | null | undefined} [value] + * @param {T[] | null} [value] */ constructor(value) { super(); @@ -35,8 +27,8 @@ export class ReactiveSet extends Set { if (DEV) new Set(value); if (value) { - for (var element of value) { - this.add(element); + for (var element of value.keys()) { + this.add(/** @type {T} */ (element)); } } @@ -49,18 +41,28 @@ export class ReactiveSet extends Set { var proto = ReactiveSet.prototype; var set_proto = Set.prototype; + /** + * @type {string | number} + */ + var method; - for (var method of methods) { + for (method of read_methods) { + // @ts-ignore + proto[method] = function (...v) { + get(this.#version); + // @ts-ignore + return set_proto[method].apply(this, v); + }; + } + + for (method of set_like_methods) { // @ts-ignore proto[method] = function (...v) { get(this.#version); // @ts-ignore var val = set_proto[method].apply(this, v); // Return a reactive set if the return value is a Set - if (val instanceof Set) { - return new ReactiveSet(val); - } - return val; + return new ReactiveSet(/** @type {T[]} */ (val)); }; } }