-
-
Notifications
You must be signed in to change notification settings - Fork 567
/
atom.ts
126 lines (107 loc) 路 3.05 KB
/
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
124
125
126
type Getter = <Value>(atom: Atom<Value>) => Value
type Setter = <Value, Args extends unknown[], Result>(
atom: WritableAtom<Value, Args, Result>,
...args: Args
) => Result
type SetAtom<Args extends unknown[], Result> = <A extends Args>(
...args: A
) => Result
/**
* setSelf is for internal use only and subject to change without notice.
*/
type Read<Value, SetSelf = never> = (
get: Getter,
options: { readonly signal: AbortSignal; readonly setSelf: SetSelf },
) => Value
type Write<Args extends unknown[], Result> = (
get: Getter,
set: Setter,
...args: Args
) => Result
// This is an internal type and not part of public API.
// Do not depend on it as it can change without notice.
type WithInitialValue<Value> = {
init: Value
}
type OnUnmount = () => void
type OnMount<Args extends unknown[], Result> = <
S extends SetAtom<Args, Result>,
>(
setAtom: S,
) => OnUnmount | void
export interface Atom<Value> {
toString: () => string
read: Read<Value>
unstable_is?(a: Atom<unknown>): boolean
debugLabel?: string
/**
* To ONLY be used by Jotai libraries to mark atoms as private. Subject to change.
* @private
*/
debugPrivate?: boolean
}
export interface WritableAtom<Value, Args extends unknown[], Result>
extends Atom<Value> {
read: Read<Value, SetAtom<Args, Result>>
write: Write<Args, Result>
onMount?: OnMount<Args, Result>
}
type SetStateAction<Value> = Value | ((prev: Value) => Value)
export type PrimitiveAtom<Value> = WritableAtom<
Value,
[SetStateAction<Value>],
void
>
let keyCount = 0 // global key count for all atoms
// writable derived atom
export function atom<Value, Args extends unknown[], Result>(
read: Read<Value, SetAtom<Args, Result>>,
write: Write<Args, Result>,
): WritableAtom<Value, Args, Result>
// read-only derived atom
export function atom<Value>(read: Read<Value>): Atom<Value>
// write-only derived atom
export function atom<Value, Args extends unknown[], Result>(
initialValue: Value,
write: Write<Args, Result>,
): WritableAtom<Value, Args, Result> & WithInitialValue<Value>
// primitive atom
export function atom<Value>(
initialValue: Value,
): PrimitiveAtom<Value> & WithInitialValue<Value>
export function atom<Value, Args extends unknown[], Result>(
read: Value | Read<Value, SetAtom<Args, Result>>,
write?: Write<Args, Result>,
) {
const key = `atom${++keyCount}`
const config = {
toString: () => key,
} as WritableAtom<Value, Args, Result> & { init?: Value }
if (typeof read === 'function') {
config.read = read as Read<Value, SetAtom<Args, Result>>
} else {
config.init = read
config.read = defaultRead
config.write = defaultWrite as unknown as Write<Args, Result>
}
if (write) {
config.write = write
}
return config
}
function defaultRead<Value>(this: Atom<Value>, get: Getter) {
return get(this)
}
function defaultWrite<Value>(
this: PrimitiveAtom<Value>,
get: Getter,
set: Setter,
arg: SetStateAction<Value>,
) {
return set(
this,
typeof arg === 'function'
? (arg as (prev: Value) => Value)(get(this))
: arg,
)
}