Skip to content

Commit

Permalink
never throw on suspend
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Jan 2, 2022
1 parent 281a617 commit 4fd5afa
Showing 1 changed file with 3 additions and 19 deletions.
22 changes: 3 additions & 19 deletions src/core/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -903,36 +903,20 @@ export const createStoreForExport = (
initialValues?: Iterable<readonly [AnyAtom, AnyAtomValue]>
) => {
const store = createStore(initialValues)
const get: {
<Value>(atom: Atom<Value>): ResolveType<Value>
<Value>(
atom: Atom<Value>,
options: {
unstable_promise: true
}
): Promise<ResolveType<Value>> | ResolveType<Value>
} = <Value>(
atom: Atom<Value>,
options?: {
unstable_promise: boolean
}
) => {
const get = <Value>(atom: Atom<Value>) => {
const atomState = store[READ_ATOM](atom)
if ('e' in atomState) {
throw atomState.e // read error
}
if ('p' in atomState) {
if (options?.unstable_promise) {
return atomState.p.then(() => get(atom))
}
throw atomState.p
return undefined // suspended
}
return atomState.v
}
const set = <Value, Update, Result extends void | Promise<void>>(
atom: WritableAtom<Value, Update, Result>,
update: Update
): Result => store[WRITE_ATOM](atom, update)
) => store[WRITE_ATOM](atom, update)
const sub = (atom: AnyAtom, callback: () => void) =>
store[SUBSCRIBE_ATOM](atom, callback)
return {
Expand Down

0 comments on commit 4fd5afa

Please sign in to comment.