From 45db9423587b9103d9f50451a2cfc5527fbef357 Mon Sep 17 00:00:00 2001 From: daishi Date: Wed, 18 Oct 2023 11:20:51 +0900 Subject: [PATCH] wip: delay micro task after promise resolving --- src/vanilla/store.ts | 33 ++++++++++----------------------- 1 file changed, 10 insertions(+), 23 deletions(-) diff --git a/src/vanilla/store.ts b/src/vanilla/store.ts index d2f7bcc2a2..734c513866 100644 --- a/src/vanilla/store.ts +++ b/src/vanilla/store.ts @@ -262,28 +262,18 @@ export const createStore = () => { const promise: Promise> & PromiseMeta> = 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) - 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) @@ -293,8 +283,6 @@ export const createStore = () => { promise as Value, nextDependencies ) - rejectPromise(promise, e) - reject(e) if ( mountedMap.has(atom) && prevAtomState?.d !== nextAtomState.d @@ -302,8 +290,7 @@ export const createStore = () => { mountDependencies(atom, nextAtomState, prevAtomState?.d) } } - } - ) + }) continuePromise = (next) => { if (!settled) { settled = true