deno.land / x / jotai@v1.8.4 / src / devtools / useGotoAtomsSnapshot.ts
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758import { 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'}
Version Info