-
Notifications
You must be signed in to change notification settings - Fork 26
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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', () => { | ||
|
@@ -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(); | ||
} | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i might just add There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
}); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.