Skip to content

Commit 4de408a

Browse files
committed
Avoid notifying non-existent members on Set.clear()
1 parent 9914bfc commit 4de408a

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

.changeset/funny-baths-drive.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solid-primitives/set": minor
3+
---
4+
5+
Avoid notifying non-existent members on Set.clear()

packages/set/src/index.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,14 @@ export class ReactiveSet<T> extends Set<T> {
8888
}
8989

9090
clear(): void {
91-
if (super.size) {
91+
if (!super.size) return;
92+
batch(() => {
93+
this.#triggers.dirty($KEYS);
94+
for (const member of super.values()) {
95+
this.#triggers.dirty(member);
96+
}
9297
super.clear();
93-
94-
batch(() => {
95-
this.#triggers.dirtyAll();
96-
});
97-
}
98+
});
9899
}
99100
}
100101

packages/set/test/index.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,29 @@ describe("ReactiveSet", () => {
182182

183183
dispose();
184184
});
185+
186+
test("clear notifies only listeners of existing members", () =>
187+
createRoot(dispose => {
188+
const set = new ReactiveSet([1, 2, 3, 4]);
189+
190+
const existing = vi.fn();
191+
createComputed(() => existing(set.has(2)));
192+
193+
const nonexisting = vi.fn();
194+
createComputed(() => nonexisting(set.has(5)));
195+
196+
expect(existing).toHaveBeenNthCalledWith(1, true);
197+
expect(nonexisting).toHaveBeenNthCalledWith(1, false);
198+
199+
set.clear();
200+
201+
expect(existing).toHaveBeenCalledTimes(2);
202+
expect(existing).toHaveBeenNthCalledWith(2, false);
203+
204+
expect(nonexisting).toHaveBeenCalledTimes(1);
205+
206+
dispose();
207+
}));
185208
});
186209

187210
describe("ReactiveWeakSet", () => {

0 commit comments

Comments
 (0)