diff --git a/src/primitives/set.ts b/src/primitives/set.ts index 2759f83..8c0fc2d 100644 --- a/src/primitives/set.ts +++ b/src/primitives/set.ts @@ -6,9 +6,9 @@ const OBJECT_KEYS = Symbol("objectKeys"); export class SignaledSet extends Set { private readonly valuesCache = createCache(); - constructor(values?: readonly T[] | null) { + constructor(values?: Iterable | null) { super(); - if (values) for (const v of values) super.add(v); + if (values) for (const value of values) super.add(value); } [Symbol.iterator](): IterableIterator { @@ -25,24 +25,27 @@ export class SignaledSet extends Set { } *values(): IterableIterator { - for (const key of super.values()) { - track(key, this.valuesCache); - yield key; - } track(OBJECT_KEYS, this.valuesCache); + + for (const value of super.values()) { + yield value; + } } *entries(): IterableIterator<[T, T]> { - for (const [key, value] of super.entries()) { - track(key, this.valuesCache); - yield [key, value]; - } track(OBJECT_KEYS, this.valuesCache); + + for (const entry of super.entries()) { + yield entry; + } } - forEach(fn: (value1: T, value2: T, set: Set) => void): void { + forEach( + callbackfn: (value1: T, value2: T, set: Set) => void, + thisArg?: any + ): void { track(OBJECT_KEYS, this.valuesCache); - super.forEach(fn); + super.forEach(callbackfn, thisArg); } has(value: T): boolean { @@ -78,6 +81,7 @@ export class SignaledSet extends Set { clear(): void { if (super.size) { super.clear(); + batch(() => { dirtyAll(this.valuesCache); }); @@ -85,6 +89,6 @@ export class SignaledSet extends Set { } } -export function createSet(values?: readonly T[] | null): SignaledSet { +export function createSet(values?: Iterable | null): SignaledSet { return new SignaledSet(values); } diff --git a/src/primitives/weak-set.ts b/src/primitives/weak-set.ts index bc295b7..907c418 100644 --- a/src/primitives/weak-set.ts +++ b/src/primitives/weak-set.ts @@ -3,9 +3,9 @@ import { createWeakCache, track, dirty } from "../utils/cache"; export class SignaledWeakSet extends WeakSet { private readonly signalsCache = createWeakCache(); - constructor(values?: readonly T[] | null) { + constructor(values?: Iterable | null) { super(); - if (values) for (const v of values) super.add(v); + if (values) for (const value of values) super.add(value); } has(value: T): boolean { @@ -34,7 +34,7 @@ export class SignaledWeakSet extends WeakSet { } export function createWeakSet( - values?: readonly T[] | null + values?: Iterable | null ): SignaledWeakSet { return new SignaledWeakSet(values); } diff --git a/tests/src/primitives/set.test.ts b/tests/src/primitives/set.test.ts index a290ea9..48866ca 100644 --- a/tests/src/primitives/set.test.ts +++ b/tests/src/primitives/set.test.ts @@ -105,11 +105,8 @@ describe("ReactiveSet - Solid Primitives", () => { const dispose = createRoot((dispose) => { createEffect(() => { const run: number[] = []; - let i = 0; for (const key of fn(set)) { run.push(key); - if (i === 2) break; // don't iterate over all keys - i += 1; } captured.push(run); }); @@ -117,21 +114,30 @@ describe("ReactiveSet - Solid Primitives", () => { }); expect(captured).toHaveLength(1); - expect(captured[0]).toEqual([1, 2, 3]); + expect(captured[0]).toEqual([1, 2, 3, 4]); set.delete(4); - expect(captured, "deleted unseen key").toHaveLength(1); + expect(captured).toHaveLength(2); + expect(captured[1]).toEqual([1, 2, 3]); set.delete(1); - expect(captured, "deleted seen").toHaveLength(2); - expect(captured[1]).toEqual([2, 3]); + expect(captured).toHaveLength(3); + expect(captured[2]).toEqual([2, 3]); set.add(4); - expect(captured, "added key in reach").toHaveLength(3); - expect(captured[2]).toEqual([2, 3, 4]); + expect(captured).toHaveLength(4); + expect(captured[3]).toEqual([2, 3, 4]); set.add(5); - expect(captured, "added key out of reach").toHaveLength(3); + expect(captured).toHaveLength(5); + expect(captured[4]).toEqual([2, 3, 4, 5]); + + set.add(5); + expect(captured).toHaveLength(5); + + set.clear(); + expect(captured).toHaveLength(6); + expect(captured[5]).toEqual([]); dispose(); });