Skip to content

Commit

Permalink
fix(core): fix infinite loop when the action was called during notify…
Browse files Browse the repository at this point in the history
…ing by another action (#80)

Co-authored-by: skriukov <skriukov@devexperts.com>
  • Loading branch information
prodderman and skriukov committed Dec 6, 2023
1 parent c5e6ca4 commit 336948c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
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

0 comments on commit 336948c

Please sign in to comment.