deno.land / x / jotai@v1.8.4 / src / core / atom.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
type Getter = { <Value>(atom: Atom<Value | Promise<Value>>): Value <Value>(atom: Atom<Promise<Value>>): Value <Value>(atom: Atom<Value>): Awaited<Value>}
type WriteGetter = Getter & { <Value>( atom: Atom<Value | Promise<Value>>, options: { unstable_promise: true } ): Promise<Value> | Value <Value>(atom: Atom<Promise<Value>>, options: { unstable_promise: true }): | Promise<Value> | Value <Value>(atom: Atom<Value>, options: { unstable_promise: true }): | Promise<Awaited<Value>> | Awaited<Value>}
type Setter = { <Value, Result extends void | Promise<void>>( atom: WritableAtom<Value, undefined, Result> ): Result <Value, Update, Result extends void | Promise<void>>( atom: WritableAtom<Value, Update, Result>, update: Update ): Result}
type Read<Value> = (get: Getter) => Value
type Write<Update, Result extends void | Promise<void>> = ( get: WriteGetter, set: Setter, update: Update) => Result
type WithInitialValue<Value> = { init: Value}
// Not exported for public API// Are there better typings?export type SetAtom< Update, Result extends void | Promise<void>> = undefined extends Update ? (update?: Update) => Result : (update: Update) => Result
type OnUnmount = () => voidtype OnMount<Update, Result extends void | Promise<void>> = < S extends SetAtom<Update, Result>>( setAtom: S) => OnUnmount | void
export interface Atom<Value> { toString: () => string debugLabel?: string read: Read<Value>}
export interface WritableAtom< Value, Update, Result extends void | Promise<void> = void> extends Atom<Value> { write: Write<Update, Result> onMount?: OnMount<Update, Result>}
type SetStateAction<Value> = Value | ((prev: Value) => Value)
export type PrimitiveAtom<Value> = WritableAtom<Value, SetStateAction<Value>>
let keyCount = 0 // global key count for all atoms
// writable derived atomexport function atom<Value, Update, Result extends void | Promise<void> = void>( read: Read<Value>, write: Write<Update, Result>): WritableAtom<Value, Update, Result>
// read-only derived atomexport function atom<Value>(read: Read<Value>): Atom<Value>
// invalid function in the first argumentexport function atom(invalidFunction: (...args: any) => any, write?: any): never
// write-only derived atomexport function atom<Value, Update, Result extends void | Promise<void> = void>( initialValue: Value, write: Write<Update, Result>): WritableAtom<Value, Update, Result> & WithInitialValue<Value>
// primitive atomexport function atom<Value>( initialValue: Value): PrimitiveAtom<Value> & WithInitialValue<Value>
export function atom<Value, Update, Result extends void | Promise<void>>( read: Value | Read<Value>, write?: Write<Update, Result>) { const key = `atom${++keyCount}` const config = { toString: () => key, } as WritableAtom<Value, Update, Result> & { init?: Value } if (typeof read === 'function') { config.read = read as Read<Value> } else { config.init = read config.read = (get) => get(config) config.write = (get, set, update) => set(config, typeof update === 'function' ? update(get(config)) : update) } if (write) { config.write = write } return config}
jotai

Version Info

Tagged at
2 years ago