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 3 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
88 changes: 50 additions & 38 deletions src/vanilla/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,17 +331,21 @@
}
return returnAtomValue(aState)
}
// 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)
let aState: AtomState<V>
try {
// a !== atom
aState = readAtomState(pending, a, dirtyAtoms)
dai-shi marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -492,29 +496,32 @@
a: WritableAtom<V, As, R>,
...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')
let aState: AtomState<V>
aState = getAtomState(a)

Check failure on line 500 in src/vanilla/store.ts

View workflow job for this annotation

GitHub Actions / lint

'aState' is never reassigned. Use 'const' instead
dai-shi marked this conversation as resolved.
Show resolved Hide resolved
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 +535,11 @@
...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 +593,13 @@
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 Expand Up @@ -641,7 +652,8 @@
const subscribeAtom = (atom: AnyAtom, listener: () => void) => {
const pending = createPending()
const atomState = getAtomState(atom)
const mounted = mountAtom(pending, atom, atomState)
let mounted: Mounted
mounted = mountAtom(pending, atom, atomState)

Check failure on line 656 in src/vanilla/store.ts

View workflow job for this annotation

GitHub Actions / lint

'mounted' is never reassigned. Use 'const' instead
dai-shi marked this conversation as resolved.
Show resolved Hide resolved
flushPending(pending)
const listeners = mounted.l
listeners.add(listener)
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',
])
})
})
Loading