-
-
Notifications
You must be signed in to change notification settings - Fork 641
/
Copy pathatomWithObservable.ts
159 lines (143 loc) · 4.02 KB
/
atomWithObservable.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { atom } from 'jotai'
import type { Atom, Getter, WritableAtom } from 'jotai'
type Timeout = ReturnType<typeof setTimeout>
type AnyError = unknown
declare global {
interface SymbolConstructor {
readonly observable: symbol
}
}
type Subscription = {
unsubscribe: () => void
}
type Observer<T> = {
next: (value: T) => void
error: (error: AnyError) => void
complete: () => void
}
type ObservableLike<T> = {
[Symbol.observable]?: () => ObservableLike<T> | undefined
} & (
| {
subscribe(observer: Partial<Observer<T>>): Subscription
}
| {
subscribe(observer: Partial<Observer<T>>): Subscription
// Overload function to make typing happy
subscribe(next: (value: T) => void): Subscription
}
)
type SubjectLike<T> = ObservableLike<T> & Observer<T>
type Options<Data> = {
initialValue?: Data | (() => Data)
unstable_timeout?: number
}
export function atomWithObservable<Data>(
getObservable: (get: Getter) => SubjectLike<Data>,
options?: Options<Data>
): WritableAtom<Data, Data>
export function atomWithObservable<Data>(
getObservable: (get: Getter) => ObservableLike<Data>,
options?: Options<Data>
): Atom<Data>
export function atomWithObservable<Data>(
getObservable: (get: Getter) => ObservableLike<Data> | SubjectLike<Data>,
options?: Options<Data>
) {
const observableResultAtom = atom((get) => {
let observable = getObservable(get)
const itself = observable[Symbol.observable]?.()
if (itself) {
observable = itself
}
type Result = { d: Data } | { e: AnyError }
let resolve: ((result: Result) => void) | undefined
const makePending = () =>
new Promise<Result>((r) => {
resolve = r
})
const initialResult: Result | Promise<Result> =
options && 'initialValue' in options
? {
d:
typeof options.initialValue === 'function'
? (options.initialValue as () => Data)()
: (options.initialValue as Data),
}
: makePending()
let setResult: ((result: Result) => void) | undefined
let lastResult: Result | undefined
const listener = (result: Result) => {
lastResult = result
resolve?.(result)
setResult?.(result)
}
let subscription: Subscription | undefined
let timer: Timeout | undefined
const isNotMounted = () => !setResult
const start = () => {
if (subscription) {
clearTimeout(timer)
subscription.unsubscribe()
}
subscription = observable.subscribe({
next: (d) => listener({ d }),
error: (e) => listener({ e }),
complete: () => {},
})
if (isNotMounted() && options?.unstable_timeout) {
timer = setTimeout(() => {
if (subscription) {
subscription.unsubscribe()
subscription = undefined
}
}, options.unstable_timeout)
}
}
start()
const resultAtom = atom(lastResult || initialResult)
resultAtom.onMount = (update) => {
setResult = update
if (lastResult) {
update(lastResult)
}
if (subscription) {
clearTimeout(timer)
} else {
start()
}
return () => {
setResult = undefined
if (subscription) {
subscription.unsubscribe()
subscription = undefined
}
}
}
return [resultAtom, observable, makePending, start, isNotMounted] as const
})
const observableAtom = atom(
(get) => {
const [resultAtom] = get(observableResultAtom)
const result = get(resultAtom)
if ('e' in result) {
throw result.e
}
return result.d
},
(get, set, data: Data) => {
const [resultAtom, observable, makePending, start, isNotMounted] =
get(observableResultAtom)
if ('next' in observable) {
if (isNotMounted()) {
set(resultAtom, makePending())
start()
}
observable.next(data)
} else {
throw new Error('observable is not subject')
}
}
)
return observableAtom
}