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/flush pending finally everywhere #2816

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
77 changes: 43 additions & 34 deletions src/vanilla/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,15 +333,18 @@ const buildStore = (
}
// a !== atom
const aState = readAtomState(pending, a, dirtyAtoms)
if (isSync) {
addDependency(pending, atom, atomState, a, aState)
} else {
const pending = createPending()
addDependency(pending, atom, atomState, a, aState)
mountDependencies(pending, atom, atomState)
flushPending(pending)
try {
return returnAtomValue(aState)
} finally {
if (isSync) {
addDependency(pending, atom, atomState, a, aState!)
} else {
const pending = createPending()
addDependency(pending, atom, atomState, a, aState!)
mountDependencies(pending, atom, atomState)
flushPending(pending)
}
}
return returnAtomValue(aState)
}
let controller: AbortController | undefined
let setSelf: ((...args: unknown[]) => unknown) | undefined
Expand Down Expand Up @@ -493,28 +496,30 @@ const buildStore = (
...args: As
) => {
const aState = getAtomState(a)
let r: R | undefined
if (isSelfAtom(atom, a)) {
if (!hasInitialValue(a)) {
// NOTE technically possible but restricted as it may cause bugs
throw new Error('atom not writable')
try {
if (isSelfAtom(atom, a)) {
if (!hasInitialValue(a)) {
// NOTE technically possible but restricted as it may cause bugs
throw new Error('atom not writable')
}
const hasPrevValue = 'v' in aState
const prevValue = aState.v
const v = args[0] as V
setAtomStateValueOrPromise(a, aState, v)
mountDependencies(pending, a, aState)
if (!hasPrevValue || !Object.is(prevValue, aState.v)) {
addPendingAtom(pending, a, aState)
recomputeDependents(pending, a, aState)
}
return undefined as R
} else {
return writeAtomState(pending, a, ...args)
}
const hasPrevValue = 'v' in aState
const prevValue = aState.v
const v = args[0] as V
setAtomStateValueOrPromise(a, aState, v)
mountDependencies(pending, a, aState)
if (!hasPrevValue || !Object.is(prevValue, aState.v)) {
addPendingAtom(pending, a, aState)
recomputeDependents(pending, a, aState)
} finally {
if (!isSync) {
flushPending(pending)
}
} else {
r = writeAtomState(pending, a, ...args) as R
}
if (!isSync) {
flushPending(pending)
}
return r as R
}
try {
return atomWrite(atom, getter, setter, ...args)
Expand All @@ -528,9 +533,11 @@ const buildStore = (
...args: Args
): Result => {
const pending = createPending()
const result = writeAtomState(pending, atom, ...args)
flushPending(pending)
return result
try {
return writeAtomState(pending, atom, ...args)
} finally {
flushPending(pending)
}
}

const mountDependencies = (
Expand Down Expand Up @@ -584,11 +591,13 @@ const buildStore = (
let isSync = true
try {
const onUnmount = atomOnMount(atom, (...args) => {
const result = writeAtomState(pending, atom, ...args)
if (!isSync) {
flushPending(pending)
try {
return writeAtomState(pending, atom, ...args)
} finally {
if (!isSync) {
flushPending(pending)
}
}
return result
})
if (onUnmount) {
mounted.u = () => {
Expand Down
20 changes: 2 additions & 18 deletions tests/vanilla/store.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ it('should update derived atom even if dependances changed (#2697)', () => {

describe('should invoke flushPending only after all atoms are updated (#2804)', () => {
const store = createStore()

it('should invoke flushPending only after all atoms are updated with set', () => {
const a = atom(0)
const setResult = []
Expand Down Expand Up @@ -611,6 +612,7 @@ describe('should invoke flushPending only after all atoms are updated (#2804)',
'after store.set',
])
})

it('should invoke flushPending only after all atoms are updated with mount', () => {
const mountResult = []
const a = atom(0)
Expand All @@ -621,44 +623,26 @@ describe('should invoke flushPending only after all atoms are updated (#2804)',
mountResult.push('before onMount setSelf')
setSelf(1)
mountResult.push('after onMount setSelf')
return () => {
mountResult.push('before onUnmount setSelf')
setSelf(2)
mountResult.push('after onUnmount setSelf')
}
}
mountResult.push('before store.sub')
store.sub(a, () => {
mountResult.push('a value changed - ' + store.get(a))
})
const unsub = store.sub(m, () => {})
mountResult.push('after store.sub')
mountResult.push('before store.unsub')
unsub()
mountResult.push('after store.unsub')
expect(mountResult).not.toEqual([
'before store.sub',
'before onMount setSelf',
'a value changed - 1',
'after onMount setSelf',
'after store.sub',
'before store.unsub',
'before onUnmount setSelf',
'a value changed - 2',
'after onUnmount setSelf',
'after store.unsub',
])
expect(mountResult).toEqual([
'before store.sub',
'before onMount setSelf',
'after onMount setSelf',
'a value changed - 1',
'after store.sub',
'before store.unsub',
'before onUnmount setSelf',
'after onUnmount setSelf',
'a value changed - 2',
'after store.unsub',
])
})
})