deno.land / x / jotai@v1.8.4 / src / utils / atomWithDefault.ts

atomWithDefault.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { atom } from 'jotai'import type { Atom, SetStateAction, WritableAtom } from 'jotai'import { RESET } from './constants'
type Read<Value> = Atom<Value>['read']
export function atomWithDefault<Value>( getDefault: Read<Value | Promise<Value>>): WritableAtom<Value, SetStateAction<Value> | typeof RESET>
export function atomWithDefault<Value>( getDefault: Read<Promise<Value>>): WritableAtom<Value, SetStateAction<Value> | typeof RESET>
export function atomWithDefault<Value>( getDefault: Read<Value>): WritableAtom<Value, SetStateAction<Value> | typeof RESET>
export function atomWithDefault<Value>(getDefault: Read<Value>) { const EMPTY = Symbol() const overwrittenAtom = atom<Value | typeof EMPTY>(EMPTY) const anAtom: WritableAtom<Value, SetStateAction<Value> | typeof RESET> = atom( (get) => { const overwritten = get(overwrittenAtom) if (overwritten !== EMPTY) { return overwritten } return getDefault(get) }, (get, set, update: SetStateAction<Value> | typeof RESET) => { if (update === RESET) { return set(overwrittenAtom, EMPTY) } return set( overwrittenAtom, typeof update === 'function' ? (update as (prev: Value) => Value)(get(anAtom)) : update ) } ) return anAtom}
jotai

Version Info

Tagged at
2 years ago