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 infinite loop when the action was called during notifying by another action #80

Merged
merged 1 commit into from
Dec 6, 2023
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
8 changes: 8 additions & 0 deletions packages/core/src/atom.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ describe('atom', () => {
expect(cb).toHaveBeenCalledTimes(1)
expect(cb).toHaveBeenLastCalledWith(4)
})
it('does not enter an infinite loop if the action was called during notifying by another action', () => {
const a = newAtom(0)
const fn = jest.fn()
const cb = () => action(fn)
a.subscribe({ next: cb })
action(() => a.set(1))
expect(fn).toHaveBeenCalledTimes(1)
})
it('skips duplicates', () => {
const { set, subscribe } = newAtom(0)
const f = jest.fn()
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ export const action = (f: () => void): void => {
isLocked = false
if (lastTime !== undefined) {
for (const listener of Array.from(lockedListeners)) {
lockedListeners.delete(listener)
listener(lastTime)
}
lastTime = undefined
lockedListeners.clear()
}
}

Expand Down