Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unwatch multiple signals #26

Merged
merged 1 commit into from
Jul 23, 2024
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
37 changes: 17 additions & 20 deletions src/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,28 +227,25 @@ export namespace Signal {
const node = this[NODE];
assertConsumerNode(node);

let indicesToShift = [];
for (let i = 0; i < node.producerNode.length; i++) {
for (let i = node.producerNode.length - 1; i >= 0; i--) {
if (signals.includes(node.producerNode[i].wrapper)) {
producerRemoveLiveConsumerAtIndex(node.producerNode[i], node.producerIndexOfThis[i]);
indicesToShift.push(i);
}
}
for (const idx of indicesToShift) {
// Logic copied from producerRemoveLiveConsumerAtIndex, but reversed
const lastIdx = node.producerNode!.length - 1;
node.producerNode![idx] = node.producerNode![lastIdx];
node.producerIndexOfThis[idx] = node.producerIndexOfThis[lastIdx];

node.producerNode.length--;
node.producerIndexOfThis.length--;
node.nextProducerIndex--;

if (idx < node.producerNode.length) {
const idxConsumer = node.producerIndexOfThis[idx];
const producer = node.producerNode[idx];
assertProducerNode(producer);
producer.liveConsumerIndexOfThis[idxConsumer] = idx;

// Logic copied from producerRemoveLiveConsumerAtIndex, but reversed
const lastIdx = node.producerNode!.length - 1;
node.producerNode![i] = node.producerNode![lastIdx];
node.producerIndexOfThis[i] = node.producerIndexOfThis[lastIdx];
Comment on lines +235 to +237
Copy link
Contributor

Choose a reason for hiding this comment

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

this stuff can be skipped if lastIdx === i

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're correct, but I believe it won't make any difference. I prefer to implement only the necessary changes needed to fix the issue, making it easier for the owner to review.

Copy link
Contributor

Choose a reason for hiding this comment

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

I certainly appreciate that! I also think it would make this code path slightly easier to read if the special case where the producer is the last item in the array was more clearly delineated like it is below the linked snippet.


node.producerNode.length--;
node.producerIndexOfThis.length--;
node.nextProducerIndex--;

if (i < node.producerNode.length) {
const idxConsumer = node.producerIndexOfThis[i];
const producer = node.producerNode[i];
assertProducerNode(producer);
producer.liveConsumerIndexOfThis[idxConsumer] = i;
}
}
}
}
Expand Down
32 changes: 31 additions & 1 deletion tests/Signal/subtle/watch-unwatch.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {describe, expect, it} from 'vitest';
import {describe, expect, it, vi} from 'vitest';
import {Signal} from '../../../src/wrapper.js';

describe('watch and unwatch', () => {
Expand Down Expand Up @@ -160,4 +160,34 @@ describe('watch and unwatch', () => {
expect(w.getPending()).toStrictEqual([]);
expect(d).toBe(4);
});
it('can unwatch multiple signals', async () => {
const signals = [...Array(7)].map((_, i) => new Signal.State(i));
const notify = vi.fn();
const watcher = new Signal.subtle.Watcher(notify);
const expectSources = (expected: typeof signals) => {
const sources = Signal.subtle.introspectSources(watcher) as typeof signals;
sources.sort((a, b) => signals.indexOf(a) - signals.indexOf(b));
expected.sort((a, b) => signals.indexOf(a) - signals.indexOf(b));
return expect(sources).toEqual(expected);
};

watcher.watch(...signals);
expectSources(signals);

const unwatched = [0, 3, 4, 6].map((i) => signals[i]);
const watched = signals.filter((s) => !unwatched.includes(s));

watcher.unwatch(...unwatched);
expectSources(watched);

let expectedNotifyCalls = 0;
for (const signal of signals) {
signal.set(signal.get() + 1);
if (watched.includes(signal)) ++expectedNotifyCalls;

expect(notify).toHaveBeenCalledTimes(expectedNotifyCalls);

watcher.watch();
}
});
Copy link
Contributor

Choose a reason for hiding this comment

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

i might just add expect(expectedNotifyCalls).toBe(3) at the end here just to be clear.

Copy link
Contributor Author

@tuhm1 tuhm1 Jul 4, 2024

Choose a reason for hiding this comment

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

I appreciate the suggestion, but I think it's not really necessary, considering the logic and other checks.

});
Loading