Skip to content

Commit

Permalink
fix recurse
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaskasky committed Jan 21, 2025
1 parent 87f50ba commit 030508d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
2 changes: 1 addition & 1 deletion tests/syncEffect/syncEffect.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ it('should not cause infinite loops when effect updates the watched atom asynchr
expect(runCount).toBe(2)
})

it.only('should allow synchronous recursion with set.recurse for first run', function test() {
it('should allow synchronous recursion with set.recurse for first run', function test() {
const watchedAtom = atom(0)
watchedAtom.debugLabel = 'watchedAtom'

Expand Down
30 changes: 13 additions & 17 deletions tests/syncEffect/syncEffect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export function syncEffect(effect: Effect): Atom<void> & { effect: Effect } {
let isMounted = false
let isRecursing = false
let fromCleanup = false
let hasChanged = false
let runCleanup: (() => void) | undefined

function makeGetAndSet(isSync: boolean) {
Expand Down Expand Up @@ -105,11 +106,16 @@ export function syncEffect(effect: Effect): Atom<void> & { effect: Effect } {
INTERNAL_mountDependencies(internalAtom, atomState)
return set(a, ...args)
} finally {
INTERNAL_recomputeInvalidatedAtoms()
isRecursing = false
const depsChanged = Array.from(deps).some(areDifferent)
if (depsChanged) {
refresh()
if (hasChanged) {
hasChanged = false
runEffect()
}
// const depsChanged = Array.from(deps).some(areDifferent)
// if (depsChanged) {
// refresh()
// }
}
}

Expand Down Expand Up @@ -149,20 +155,6 @@ export function syncEffect(effect: Effect): Atom<void> & { effect: Effect } {
}
}

function refresh() {
try {
ref.isRefreshing = true
delete atomState.v
INTERNAL_readAtomState(internalAtom)
} finally {
ref.isRefreshing = false
}
}

function areDifferent([a, v]: [Atom<unknown>, unknown]) {
return store.get(a) !== v
}

const [
,
,
Expand Down Expand Up @@ -193,6 +185,10 @@ export function syncEffect(effect: Effect): Atom<void> & { effect: Effect } {
})

hookInto(atomState, 'u', function atomOnUpdate() {
if (isRecursing) {
hasChanged = true
return
}
syncEffectChannel.add(runEffect)
})
}
Expand Down

0 comments on commit 030508d

Please sign in to comment.