Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/funny-baths-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solid-primitives/set": minor
---

Avoid notifying non-existent members on Set.clear()
5 changes: 5 additions & 0 deletions .changeset/red-islands-tickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solid-primitives/map": minor
---

Avoid notifying non-existent members on Map.clear()
17 changes: 10 additions & 7 deletions packages/map/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,17 @@ export class ReactiveMap<K, V> extends Map<K, V> {
}

clear(): void {
if (super.size) {
super.clear();
if (super.size === 0) return;
batch(() => {
this.#keyTriggers.dirty($OBJECT);
this.#valueTriggers.dirty($OBJECT);
for (const key of super.keys()) {
this.#keyTriggers.dirty(key);
this.#valueTriggers.dirty(key);
}

batch(() => {
this.#keyTriggers.dirtyAll();
this.#valueTriggers.dirtyAll();
});
}
super.clear();
});
}
}

Expand Down
39 changes: 39 additions & 0 deletions packages/map/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,45 @@ describe("ReactiveMap", () => {

dispose();
});
test(".clear() notifies only listeners of existing members", () =>
createRoot(dispose => {
const map = new ReactiveMap([
[1, "a"],
[2, "b"],
[3, "c"],
]);

const existingKey = vi.fn();
createComputed(() => existingKey(map.has(2)));

const existingValue = vi.fn();
createComputed(() => existingValue(map.get(2)));

const nonexistingKey = vi.fn();
createComputed(() => nonexistingKey(map.has(4)));

const nonexistingValue = vi.fn();
createComputed(() => nonexistingValue(map.get(4)));

expect(existingKey).toHaveBeenNthCalledWith(1, true);
expect(existingValue).toHaveBeenNthCalledWith(1, "b");

expect(nonexistingKey).toHaveBeenNthCalledWith(1, false);
expect(nonexistingValue).toHaveBeenNthCalledWith(1, undefined);

map.clear();

expect(existingKey).toHaveBeenCalledTimes(2);
expect(existingKey).toHaveBeenNthCalledWith(2, false);

expect(existingValue).toHaveBeenCalledTimes(2);
expect(existingValue).toHaveBeenNthCalledWith(2, undefined);

expect(nonexistingKey).toHaveBeenCalledTimes(1);
expect(nonexistingValue).toHaveBeenCalledTimes(1);

dispose();
}));
});

describe("ReactiveWeakMap", () => {
Expand Down
13 changes: 7 additions & 6 deletions packages/set/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,14 @@ export class ReactiveSet<T> extends Set<T> {
}

clear(): void {
if (super.size) {
if (!super.size) return;
batch(() => {
this.#triggers.dirty($KEYS);
for (const member of super.values()) {
this.#triggers.dirty(member);
}
super.clear();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did some benchmarking and did not see a performance hit to moving super.clear() into the batch operation. But if you like, I can save a snapshot prior to .clear() and iterate over that in batch.


batch(() => {
this.#triggers.dirtyAll();
});
}
});
}
}

Expand Down
23 changes: 23 additions & 0 deletions packages/set/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,29 @@ describe("ReactiveSet", () => {

dispose();
});

test("clear notifies only listeners of existing members", () =>
createRoot(dispose => {
const set = new ReactiveSet([1, 2, 3, 4]);

const existing = vi.fn();
createComputed(() => existing(set.has(2)));

const nonexisting = vi.fn();
createComputed(() => nonexisting(set.has(5)));

expect(existing).toHaveBeenNthCalledWith(1, true);
expect(nonexisting).toHaveBeenNthCalledWith(1, false);

set.clear();

expect(existing).toHaveBeenCalledTimes(2);
expect(existing).toHaveBeenNthCalledWith(2, false);

expect(nonexisting).toHaveBeenCalledTimes(1);

dispose();
}));
});

describe("ReactiveWeakSet", () => {
Expand Down
Loading