-
-
Notifications
You must be signed in to change notification settings - Fork 643
/
Copy pathstore.ts
714 lines (664 loc) · 21 KB
/
store.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
import type { Atom, WritableAtom } from './atom.ts'
type AnyValue = unknown
type AnyError = unknown
type AnyAtom = Atom<AnyValue>
type AnyWritableAtom = WritableAtom<AnyValue, unknown[], unknown>
type OnUnmount = () => void
type Getter = Parameters<AnyAtom['read']>[0]
type Setter = Parameters<AnyWritableAtom['write']>[1]
const isSelfAtom = (atom: AnyAtom, a: AnyAtom): boolean =>
atom.unstable_is ? atom.unstable_is(a) : a === atom
const hasInitialValue = <T extends Atom<AnyValue>>(
atom: T,
): atom is T & (T extends Atom<infer Value> ? { init: Value } : never) =>
'init' in atom
const isActuallyWritableAtom = (atom: AnyAtom): atom is AnyWritableAtom =>
!!(atom as AnyWritableAtom).write
//
// Cancelable Promise
//
type CancelHandler = (nextValue: unknown) => void
type PromiseState = [cancelHandlers: Set<CancelHandler>, settled: boolean]
const cancelablePromiseMap = new WeakMap<PromiseLike<unknown>, PromiseState>()
const isPendingPromise = (value: unknown): value is PromiseLike<unknown> =>
isPromiseLike(value) && !cancelablePromiseMap.get(value)?.[1]
const cancelPromise = <T>(promise: PromiseLike<T>, nextValue: unknown) => {
const promiseState = cancelablePromiseMap.get(promise)
if (promiseState) {
promiseState[1] = true
promiseState[0].forEach((fn) => fn(nextValue))
} else if (import.meta.env?.MODE !== 'production') {
throw new Error('[Bug] cancelable promise not found')
}
}
const patchPromiseForCancelability = <T>(promise: PromiseLike<T>) => {
if (cancelablePromiseMap.has(promise)) {
// already patched
return
}
const promiseState: PromiseState = [new Set(), false]
cancelablePromiseMap.set(promise, promiseState)
const settle = () => {
promiseState[1] = true
}
promise.then(settle, settle)
;(promise as { onCancel?: (fn: CancelHandler) => void }).onCancel = (fn) => {
promiseState[0].add(fn)
}
}
const isPromiseLike = (
x: unknown,
): x is PromiseLike<unknown> & { onCancel?: (fn: CancelHandler) => void } =>
typeof (x as any)?.then === 'function'
/**
* State tracked for mounted atoms. An atom is considered "mounted" if it has a
* subscriber, or is a transitive dependency of another atom that has a
* subscriber.
*
* The mounted state of an atom is freed once it is no longer mounted.
*/
type Mounted = {
/** Set of listeners to notify when the atom value changes. */
readonly l: Set<() => void>
/** Set of mounted atoms that the atom depends on. */
readonly d: Set<AnyAtom>
/** Set of mounted atoms that depends on the atom. */
readonly t: Set<AnyAtom>
/** Function to run when the atom is unmounted. */
u?: OnUnmount
}
/**
* Mutable atom state,
* tracked for both mounted and unmounted atoms in a store.
*/
type AtomState<Value = AnyValue> = {
/**
* Map of atoms that the atom depends on.
* The map value is the epoch number of the dependency.
*/
readonly d: Map<AnyAtom, number>
/**
* Set of atoms with pending promise that depend on the atom.
*
* This may cause memory leaks, but it's for the capability to continue promises
*/
readonly p: Set<AnyAtom>
/** The epoch number of the atom. */
n: number
/** Object to store mounted state of the atom. */
m?: Mounted // only available if the atom is mounted
/** Atom value */
v?: Value
/** Atom error */
e?: AnyError
}
const isAtomStateInitialized = <Value>(atomState: AtomState<Value>) =>
'v' in atomState || 'e' in atomState
const returnAtomValue = <Value>(atomState: AtomState<Value>): Value => {
if ('e' in atomState) {
throw atomState.e
}
if (import.meta.env?.MODE !== 'production' && !('v' in atomState)) {
throw new Error('[Bug] atom state is not initialized')
}
return atomState.v!
}
const addPendingPromiseToDependency = (
atom: AnyAtom,
promise: PromiseLike<AnyValue>,
dependencyAtomState: AtomState,
) => {
if (!dependencyAtomState.p.has(atom)) {
dependencyAtomState.p.add(atom)
promise.then(
() => {
dependencyAtomState.p.delete(atom)
},
() => {
dependencyAtomState.p.delete(atom)
},
)
}
}
const addDependency = <Value>(
pending: Pending | undefined,
atom: Atom<Value>,
atomState: AtomState<Value>,
a: AnyAtom,
aState: AtomState,
) => {
if (import.meta.env?.MODE !== 'production' && a === atom) {
throw new Error('[Bug] atom cannot depend on itself')
}
atomState.d.set(a, aState.n)
if (isPendingPromise(atomState.v)) {
addPendingPromiseToDependency(atom, atomState.v, aState)
}
aState.m?.t.add(atom)
if (pending) {
addPendingDependent(pending, a, atom)
}
}
//
// Pending
//
type Pending = readonly [
dependents: Map<AnyAtom, Set<AnyAtom>>,
atomStates: Map<AnyAtom, AtomState>,
functions: Set<() => void>,
]
const createPending = (): Pending => [new Map(), new Map(), new Set()]
const addPendingAtom = (
pending: Pending,
atom: AnyAtom,
atomState: AtomState,
) => {
if (!pending[0].has(atom)) {
pending[0].set(atom, new Set())
}
pending[1].set(atom, atomState)
}
const addPendingDependent = (
pending: Pending,
atom: AnyAtom,
dependent: AnyAtom,
) => {
const dependents = pending[0].get(atom)
if (dependents) {
dependents.add(dependent)
}
}
const getPendingDependents = (pending: Pending, atom: AnyAtom) =>
pending[0].get(atom)
const addPendingFunction = (pending: Pending, fn: () => void) => {
pending[2].add(fn)
}
const flushPending = (pending: Pending) => {
while (pending[1].size || pending[2].size) {
pending[0].clear()
const atomStates = new Set(pending[1].values())
pending[1].clear()
const functions = new Set(pending[2])
pending[2].clear()
atomStates.forEach((atomState) => atomState.m?.l.forEach((l) => l()))
functions.forEach((fn) => fn())
}
}
type GetAtomState = <Value>(
atom: Atom<Value>,
originAtomState?: AtomState,
) => AtomState<Value>
// internal & unstable type
type StoreArgs = readonly [
getAtomState: GetAtomState,
// possible other arguments in the future
]
// for debugging purpose only
type DevStoreRev4 = {
dev4_get_internal_weak_map: () => {
get: (atom: AnyAtom) => AtomState | undefined
}
dev4_get_mounted_atoms: () => Set<AnyAtom>
dev4_restore_atoms: (values: Iterable<readonly [AnyAtom, AnyValue]>) => void
}
type PrdStore = {
get: <Value>(atom: Atom<Value>) => Value
set: <Value, Args extends unknown[], Result>(
atom: WritableAtom<Value, Args, Result>,
...args: Args
) => Result
sub: (atom: AnyAtom, listener: () => void) => () => void
unstable_derive: (fn: (...args: StoreArgs) => StoreArgs) => Store
}
type Store = PrdStore | (PrdStore & DevStoreRev4)
export type INTERNAL_DevStoreRev4 = DevStoreRev4
export type INTERNAL_PrdStore = PrdStore
const buildStore = (getAtomState: StoreArgs[0]): Store => {
// for debugging purpose only
let debugMountedAtoms: Set<AnyAtom>
if (import.meta.env?.MODE !== 'production') {
debugMountedAtoms = new Set()
}
const setAtomStateValueOrPromise = (
atom: AnyAtom,
atomState: AtomState,
valueOrPromise: unknown,
) => {
const hasPrevValue = 'v' in atomState
const prevValue = atomState.v
const pendingPromise = isPendingPromise(atomState.v) ? atomState.v : null
if (isPromiseLike(valueOrPromise)) {
patchPromiseForCancelability(valueOrPromise)
for (const a of atomState.d.keys()) {
addPendingPromiseToDependency(
atom,
valueOrPromise,
getAtomState(a, atomState),
)
}
atomState.v = valueOrPromise
delete atomState.e
} else {
atomState.v = valueOrPromise
delete atomState.e
}
if (!hasPrevValue || !Object.is(prevValue, atomState.v)) {
++atomState.n
if (pendingPromise) {
cancelPromise(pendingPromise, valueOrPromise)
}
}
}
const readAtomState = <Value>(
pending: Pending | undefined,
atom: Atom<Value>,
atomState: AtomState<Value>,
dirtyAtoms?: Set<AnyAtom>,
): AtomState<Value> => {
// See if we can skip recomputing this atom.
if (isAtomStateInitialized(atomState)) {
// If the atom is mounted, we can use cached atom state.
// because it should have been updated by dependencies.
// We can't use the cache if the atom is dirty.
if (atomState.m && !dirtyAtoms?.has(atom)) {
return atomState
}
// Otherwise, check if the dependencies have changed.
// If all dependencies haven't changed, we can use the cache.
if (
Array.from(atomState.d).every(
([a, n]) =>
// Recursively, read the atom state of the dependency, and
// check if the atom epoch number is unchanged
readAtomState(pending, a, getAtomState(a, atomState), dirtyAtoms)
.n === n,
)
) {
return atomState
}
}
// Compute a new state for this atom.
atomState.d.clear()
let isSync = true
const getter: Getter = <V>(a: Atom<V>) => {
if (isSelfAtom(atom, a)) {
const aState = getAtomState(a, atomState)
if (!isAtomStateInitialized(aState)) {
if (hasInitialValue(a)) {
setAtomStateValueOrPromise(a, aState, a.init)
} else {
// NOTE invalid derived atoms can reach here
throw new Error('no atom init')
}
}
return returnAtomValue(aState)
}
// a !== atom
const aState = readAtomState(
pending,
a,
getAtomState(a, atomState),
dirtyAtoms,
)
if (isSync) {
addDependency(pending, atom, atomState, a, aState)
} else {
const pending = createPending()
addDependency(pending, atom, atomState, a, aState)
mountDependencies(pending, atom, atomState)
flushPending(pending)
}
return returnAtomValue(aState)
}
let controller: AbortController | undefined
let setSelf: ((...args: unknown[]) => unknown) | undefined
const options = {
get signal() {
if (!controller) {
controller = new AbortController()
}
return controller.signal
},
get setSelf() {
if (
import.meta.env?.MODE !== 'production' &&
!isActuallyWritableAtom(atom)
) {
console.warn('setSelf function cannot be used with read-only atom')
}
if (!setSelf && isActuallyWritableAtom(atom)) {
setSelf = (...args) => {
if (import.meta.env?.MODE !== 'production' && isSync) {
console.warn('setSelf function cannot be called in sync')
}
if (!isSync) {
return writeAtom(atom, ...args)
}
}
}
return setSelf
},
}
try {
const valueOrPromise = atom.read(getter, options as never)
setAtomStateValueOrPromise(atom, atomState, valueOrPromise)
if (isPromiseLike(valueOrPromise)) {
valueOrPromise.onCancel?.(() => controller?.abort())
const complete = () => {
if (atomState.m) {
const pending = createPending()
mountDependencies(pending, atom, atomState)
flushPending(pending)
}
}
valueOrPromise.then(complete, complete)
}
return atomState
} catch (error) {
delete atomState.v
atomState.e = error
++atomState.n
return atomState
} finally {
isSync = false
}
}
const readAtom = <Value>(atom: Atom<Value>): Value =>
returnAtomValue(readAtomState(undefined, atom, getAtomState(atom)))
const getDependents = <Value>(
pending: Pending,
atom: Atom<Value>,
atomState: AtomState<Value>,
): Map<AnyAtom, AtomState> => {
const dependents = new Map<AnyAtom, AtomState>()
for (const a of atomState.m?.t || []) {
dependents.set(a, getAtomState(a, atomState))
}
for (const atomWithPendingPromise of atomState.p) {
dependents.set(
atomWithPendingPromise,
getAtomState(atomWithPendingPromise, atomState),
)
}
getPendingDependents(pending, atom)?.forEach((dependent) => {
dependents.set(dependent, getAtomState(dependent, atomState))
})
return dependents
}
const recomputeDependents = <Value>(
pending: Pending,
atom: Atom<Value>,
atomState: AtomState<Value>,
) => {
// This is a topological sort via depth-first search, slightly modified from
// what's described here for simplicity and performance reasons:
// https://en.wikipedia.org/wiki/Topological_sorting#Depth-first_search
// Step 1: traverse the dependency graph to build the topsorted atom list
// We don't bother to check for cycles, which simplifies the algorithm.
const topsortedAtoms: (readonly [
atom: AnyAtom,
atomState: AtomState,
epochNumber: number,
])[] = []
const markedAtoms = new Set<AnyAtom>()
const visit = (a: AnyAtom, aState: AtomState) => {
if (markedAtoms.has(a)) {
return
}
markedAtoms.add(a)
for (const [d, s] of getDependents(pending, a, aState)) {
if (a !== d) {
visit(d, s)
}
}
// The algorithm calls for pushing onto the front of the list. For
// performance, we will simply push onto the end, and then will iterate in
// reverse order later.
topsortedAtoms.push([a, aState, aState.n])
}
// Visit the root atom. This is the only atom in the dependency graph
// without incoming edges, which is one reason we can simplify the algorithm
visit(atom, atomState)
// Step 2: use the topsorted atom list to recompute all affected atoms
// Track what's changed, so that we can short circuit when possible
const changedAtoms = new Set<AnyAtom>([atom])
for (let i = topsortedAtoms.length - 1; i >= 0; --i) {
const [a, aState, prevEpochNumber] = topsortedAtoms[i]!
let hasChangedDeps = false
for (const dep of aState.d.keys()) {
if (dep !== a && changedAtoms.has(dep)) {
hasChangedDeps = true
break
}
}
if (hasChangedDeps) {
readAtomState(pending, a, aState, markedAtoms)
mountDependencies(pending, a, aState)
if (prevEpochNumber !== aState.n) {
addPendingAtom(pending, a, aState)
changedAtoms.add(a)
}
}
markedAtoms.delete(a)
}
}
const writeAtomState = <Value, Args extends unknown[], Result>(
pending: Pending,
atom: WritableAtom<Value, Args, Result>,
atomState: AtomState<Value>,
...args: Args
): Result => {
const getter: Getter = <V>(a: Atom<V>) =>
returnAtomValue(readAtomState(pending, a, getAtomState(a, atomState)))
const setter: Setter = <V, As extends unknown[], R>(
a: WritableAtom<V, As, R>,
...args: As
) => {
const aState = getAtomState(a, atomState)
let r: R | undefined
if (isSelfAtom(atom, a)) {
if (!hasInitialValue(a)) {
// NOTE technically possible but restricted as it may cause bugs
throw new Error('atom not writable')
}
const hasPrevValue = 'v' in aState
const prevValue = aState.v
const v = args[0] as V
setAtomStateValueOrPromise(a, aState, v)
mountDependencies(pending, a, aState)
if (!hasPrevValue || !Object.is(prevValue, aState.v)) {
addPendingAtom(pending, a, aState)
recomputeDependents(pending, a, aState)
}
} else {
r = writeAtomState(pending, a, aState, ...args) as R
}
flushPending(pending)
return r as R
}
const result = atom.write(getter, setter, ...args)
return result
}
const writeAtom = <Value, Args extends unknown[], Result>(
atom: WritableAtom<Value, Args, Result>,
...args: Args
): Result => {
const pending = createPending()
const result = writeAtomState(pending, atom, getAtomState(atom), ...args)
flushPending(pending)
return result
}
const mountDependencies = (
pending: Pending,
atom: AnyAtom,
atomState: AtomState,
) => {
if (atomState.m && !isPendingPromise(atomState.v)) {
for (const a of atomState.d.keys()) {
if (!atomState.m.d.has(a)) {
const aMounted = mountAtom(pending, a, getAtomState(a, atomState))
aMounted.t.add(atom)
atomState.m.d.add(a)
}
}
for (const a of atomState.m.d || []) {
if (!atomState.d.has(a)) {
atomState.m.d.delete(a)
const aMounted = unmountAtom(pending, a, getAtomState(a, atomState))
aMounted?.t.delete(atom)
}
}
}
}
const mountAtom = <Value>(
pending: Pending,
atom: Atom<Value>,
atomState: AtomState<Value>,
): Mounted => {
if (!atomState.m) {
// recompute atom state
readAtomState(pending, atom, atomState)
// mount dependencies first
for (const a of atomState.d.keys()) {
const aMounted = mountAtom(pending, a, getAtomState(a, atomState))
aMounted.t.add(atom)
}
// mount self
atomState.m = {
l: new Set(),
d: new Set(atomState.d.keys()),
t: new Set(),
}
if (import.meta.env?.MODE !== 'production') {
debugMountedAtoms.add(atom)
}
if (isActuallyWritableAtom(atom) && atom.onMount) {
const mounted = atomState.m
const { onMount } = atom
addPendingFunction(pending, () => {
const onUnmount = onMount((...args) =>
writeAtomState(pending, atom, atomState, ...args),
)
if (onUnmount) {
mounted.u = onUnmount
}
})
}
}
return atomState.m
}
const unmountAtom = <Value>(
pending: Pending,
atom: Atom<Value>,
atomState: AtomState<Value>,
): Mounted | undefined => {
if (
atomState.m &&
!atomState.m.l.size &&
!Array.from(atomState.m.t).some((a) =>
getAtomState(a, atomState).m?.d.has(atom),
)
) {
// unmount self
const onUnmount = atomState.m.u
if (onUnmount) {
addPendingFunction(pending, onUnmount)
}
delete atomState.m
if (import.meta.env?.MODE !== 'production') {
debugMountedAtoms.delete(atom)
}
// unmount dependencies
for (const a of atomState.d.keys()) {
const aMounted = unmountAtom(pending, a, getAtomState(a, atomState))
aMounted?.t.delete(atom)
}
return undefined
}
return atomState.m
}
const subscribeAtom = (atom: AnyAtom, listener: () => void) => {
const pending = createPending()
const atomState = getAtomState(atom)
const mounted = mountAtom(pending, atom, atomState)
flushPending(pending)
const listeners = mounted.l
listeners.add(listener)
return () => {
listeners.delete(listener)
const pending = createPending()
unmountAtom(pending, atom, atomState)
flushPending(pending)
}
}
const unstable_derive = (fn: (...args: StoreArgs) => StoreArgs) =>
buildStore(...fn(getAtomState))
const store: Store = {
get: readAtom,
set: writeAtom,
sub: subscribeAtom,
unstable_derive,
}
if (import.meta.env?.MODE !== 'production') {
const devStore: DevStoreRev4 = {
// store dev methods (these are tentative and subject to change without notice)
dev4_get_internal_weak_map: () => ({
get: (atom) => {
const atomState = getAtomState(atom)
if (atomState.n === 0) {
// for backward compatibility
return undefined
}
return atomState
},
}),
dev4_get_mounted_atoms: () => debugMountedAtoms,
dev4_restore_atoms: (values) => {
const pending = createPending()
for (const [atom, value] of values) {
if (hasInitialValue(atom)) {
const atomState = getAtomState(atom)
const hasPrevValue = 'v' in atomState
const prevValue = atomState.v
setAtomStateValueOrPromise(atom, atomState, value)
mountDependencies(pending, atom, atomState)
if (!hasPrevValue || !Object.is(prevValue, atomState.v)) {
addPendingAtom(pending, atom, atomState)
recomputeDependents(pending, atom, atomState)
}
}
}
flushPending(pending)
},
}
Object.assign(store, devStore)
}
return store
}
export const createStore = (): Store => {
const atomStateMap = new WeakMap()
const getAtomState = <Value>(atom: Atom<Value>) => {
let atomState = atomStateMap.get(atom) as AtomState<Value> | undefined
if (!atomState) {
atomState = { d: new Map(), p: new Set(), n: 0 }
atomStateMap.set(atom, atomState)
}
return atomState
}
return buildStore(getAtomState)
}
let defaultStore: Store | undefined
export const getDefaultStore = (): Store => {
if (!defaultStore) {
defaultStore = createStore()
if (import.meta.env?.MODE !== 'production') {
;(globalThis as any).__JOTAI_DEFAULT_STORE__ ||= defaultStore
if ((globalThis as any).__JOTAI_DEFAULT_STORE__ !== defaultStore) {
console.warn(
'Detected multiple Jotai instances. It may cause unexpected behavior with the default store. https://github.com/pmndrs/jotai/discussions/2044',
)
}
}
}
return defaultStore
}