Skip to content

Commit

Permalink
wip: delay micro task after promise resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Oct 18, 2023
1 parent a77ac88 commit 45db942
Showing 1 changed file with 10 additions and 23 deletions.
33 changes: 10 additions & 23 deletions src/vanilla/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,28 +262,18 @@ export const createStore = () => {
const promise: Promise<Awaited<Value>> & PromiseMeta<Awaited<Value>> =
new Promise((resolve, reject) => {
let settled = false
valueOrPromise.then(
(v) => {
if (!settled) {
settled = true
const prevAtomState = getAtomState(atom)
// update dependencies, that could have changed
const nextAtomState = setAtomValue(
atom,
promise as Value,
nextDependencies
)
valueOrPromise
.then(
(v) => {
resolvePromise(promise, v)
resolve(v as Awaited<Value>)
if (
mountedMap.has(atom) &&
prevAtomState?.d !== nextAtomState.d
) {
mountDependencies(atom, nextAtomState, prevAtomState?.d)
}
},
(e) => {
rejectPromise(promise, e)
reject(e)
}
},
(e) => {
)
.then(() => {
if (!settled) {
settled = true
const prevAtomState = getAtomState(atom)
Expand All @@ -293,17 +283,14 @@ export const createStore = () => {
promise as Value,
nextDependencies
)
rejectPromise(promise, e)
reject(e)
if (
mountedMap.has(atom) &&
prevAtomState?.d !== nextAtomState.d
) {
mountDependencies(atom, nextAtomState, prevAtomState?.d)
}
}
}
)
})
continuePromise = (next) => {
if (!settled) {
settled = true
Expand Down

0 comments on commit 45db942

Please sign in to comment.