Skip to content

Commit 2dbfba8

Browse files
committed
fix useAtomValue
1 parent 75918d6 commit 2dbfba8

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

src/react/useAtomValue.ts

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,13 @@ const use =
5454
}
5555
})
5656

57+
type ContinuablePromiseCache = WeakMap<PromiseLike<unknown>, Promise<unknown>>
58+
5759
type StoreHelpers = readonly [
5860
changedHook: { add(atom: Atom<unknown>, callback: () => void): () => void },
5961
promiseCache: {
60-
get(promise: PromiseLike<unknown>): Promise<unknown> | undefined
61-
set(promise: PromiseLike<unknown>, value: Promise<unknown>): void
62+
get(atom: Atom<unknown>): ContinuablePromiseCache | undefined
63+
set(atom: Atom<unknown>, value: ContinuablePromiseCache): void
6264
},
6365
]
6466

@@ -70,7 +72,7 @@ const getStoreHelpers = (store: Store) => {
7072
const buildingBlocks = getBuildingBlocks(store)
7173
const storeHooks = initializeStoreHooks(buildingBlocks[6])
7274
const changedHook = storeHooks.c
73-
const promiseCache = new Map<PromiseLike<unknown>, Promise<unknown>>()
75+
const promiseCache = new WeakMap<Atom<unknown>, ContinuablePromiseCache>()
7476
helpers = [changedHook, promiseCache]
7577
StoreHelpersMap.set(store, helpers)
7678
}
@@ -83,17 +85,30 @@ const createContinuablePromise = <T>(
8385
promise: PromiseLike<T>,
8486
) => {
8587
const [changedHook, promiseCache] = getStoreHelpers(store)
86-
let continuablePromise = promiseCache.get(promise)
88+
let continuablePromiseCache = promiseCache.get(atom)
89+
if (!continuablePromiseCache) {
90+
continuablePromiseCache = new WeakMap()
91+
promiseCache.set(atom, continuablePromiseCache)
92+
}
93+
let continuablePromise = continuablePromiseCache.get(promise)
8794
if (!continuablePromise) {
8895
continuablePromise = new Promise<T>((resolve, reject) => {
89-
let curr = promise
96+
let curr: PromiseLike<T> | undefined = promise
9097
const cleanup = changedHook.add(atom, () => {
91-
const nextValue = store.get(atom)
92-
if (isPromiseLike(nextValue)) {
93-
curr = nextValue
94-
nextValue.then(onFulfilled(nextValue), onRejected(nextValue))
95-
} else {
96-
resolve(nextValue as T)
98+
try {
99+
const nextValue = store.get(atom)
100+
if (isPromiseLike(nextValue)) {
101+
curr = nextValue
102+
nextValue.then(onFulfilled(nextValue), onRejected(nextValue))
103+
} else {
104+
curr = undefined
105+
resolve(nextValue as T)
106+
cleanup()
107+
}
108+
} catch (e) {
109+
curr = undefined
110+
reject(e)
111+
cleanup()
97112
}
98113
})
99114
const onFulfilled = (me: PromiseLike<T>) => (v: T) => {
@@ -110,7 +125,7 @@ const createContinuablePromise = <T>(
110125
}
111126
promise.then(onFulfilled(promise), onRejected(promise))
112127
})
113-
promiseCache.set(promise, continuablePromise)
128+
continuablePromiseCache.set(promise, continuablePromise)
114129
}
115130
return continuablePromise
116131
}

0 commit comments

Comments
 (0)