deno.land / x / jotai@v1.8.4 / src / devtools / useGotoAtomsSnapshot.ts

useGotoAtomsSnapshot.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
import { useCallback, useContext } from 'react'import { SECRET_INTERNAL_getScopeContext as getScopeContext } from 'jotai'import type { Atom } from '../core/atom'import { DEV_SUBSCRIBE_STATE, RESTORE_ATOMS } from '../core/store'
type Scope = NonNullable<Parameters<typeof getScopeContext>[0]>type AnyAtomValue = unknowntype AnyAtom = Atom<AnyAtomValue>type AtomsValues = Map<AnyAtom, AnyAtomValue> // immutabletype AtomsDependents = Map<AnyAtom, Set<AnyAtom>> // immutabletype AtomsSnapshot = Readonly<{ values: AtomsValues dependents: AtomsDependents}>
/** * @deprecated use object snapshot instead */type DeprecatedIterableSnapshot = Iterable<readonly [AnyAtom, AnyAtomValue]>
export function useGotoAtomsSnapshot(scope?: Scope) { const ScopeContext = getScopeContext(scope) const { s: store, w: versionedWrite } = useContext(ScopeContext)
return useCallback( (snapshot: AtomsSnapshot | DeprecatedIterableSnapshot) => { if (!store[DEV_SUBSCRIBE_STATE]) return
const restoreAtoms = ( values: Iterable<readonly [AnyAtom, AnyAtomValue]> ) => { if (versionedWrite) { versionedWrite((version) => { store[RESTORE_ATOMS](values, version) }) } else { store[RESTORE_ATOMS](values) } } if (isIterable(snapshot)) { if (__DEV__) { console.warn( 'snapshot as iterable is deprecated. use an object instead.' ) } restoreAtoms(snapshot) return } restoreAtoms(snapshot.values) }, [store, versionedWrite] )}
const isIterable = (item: any): item is Iterable<any> => { return typeof item[Symbol.iterator] === 'function'}
jotai

Version Info

Tagged at
2 years ago