Skip to content

Commit

Permalink
fix(vanilla): make restore atoms dev only (v2 API) (#1648)
Browse files Browse the repository at this point in the history
* change useHydrateAtoms capability

* make restore atoms dev only

* empty commit
  • Loading branch information
dai-shi committed Dec 20, 2022
1 parent 956c283 commit 8ccaa49
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 26 deletions.
1 change: 1 addition & 0 deletions docs/guides/migrating-to-v2-api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,4 @@ const allAtom = atom((get) => Promise.all([get(fooAtom), get(barAtom)]))
## Some other changes
- `atomWithStorage` util's `delayInit` is removed as being default.
- `useHydrateAtoms` can only accept writable atoms.
4 changes: 2 additions & 2 deletions src/react/devtools/useGotoAtomsSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export function useGotoAtomsSnapshot(options?: Options) {
const store = useStore(options)
return useCallback(
(snapshot: AtomsSnapshot) => {
if (store.dev_subscribe_state) {
store.res(snapshot.values)
if (store.dev_restore_atoms) {
store.dev_restore_atoms(snapshot.values)
}
},
[store]
Expand Down
16 changes: 6 additions & 10 deletions src/react/utils/useHydrateAtoms.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
import { useStore } from 'jotai/react'
import type { Atom } from 'jotai/vanilla'
import type { WritableAtom } from 'jotai/vanilla'

type Store = ReturnType<typeof useStore>
type Options = Parameters<typeof useStore>[0]
type AnyWritableAtom = WritableAtom<unknown, any[], any>

const hydratedMap: WeakMap<Store, WeakSet<Atom<unknown>>> = new WeakMap()
const hydratedMap: WeakMap<Store, WeakSet<AnyWritableAtom>> = new WeakMap()

export function useHydrateAtoms(
values: Iterable<readonly [Atom<unknown>, unknown]>,
values: Iterable<readonly [AnyWritableAtom, unknown]>,
options?: Options
) {
const store = useStore(options)

const hydratedSet = getHydratedSet(store)
const tuplesToRestore: (readonly [Atom<unknown>, unknown])[] = []
for (const tuple of values) {
const atom = tuple[0]
for (const [atom, value] of values) {
if (!hydratedSet.has(atom)) {
hydratedSet.add(atom)
tuplesToRestore.push(tuple)
store.set(atom, value)
}
}
if (tuplesToRestore.length) {
store.res(tuplesToRestore)
}
}

const getHydratedSet = (store: Store) => {
Expand Down
23 changes: 9 additions & 14 deletions src/vanilla/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,24 +550,11 @@ export const createStore = () => {
}
}

const restoreAtoms = (
values: Iterable<readonly [AnyAtom, AnyValue]>
): void => {
for (const [atom, value] of values) {
if (hasInitialValue(atom)) {
setAtomValue(atom, value)
recomputeDependents(atom)
}
}
flushPending()
}

if (__DEV__) {
return {
get: readAtom,
set: writeAtom,
sub: subscribeAtom,
res: restoreAtoms,
// store dev methods (these are tentative and subject to change)
dev_subscribe_state: (l: StateListener) => {
stateListeners.add(l)
Expand All @@ -578,13 +565,21 @@ export const createStore = () => {
dev_get_mounted_atoms: () => mountedAtoms.values(),
dev_get_atom_state: (a: AnyAtom) => atomStateMap.get(a),
dev_get_mounted: (a: AnyAtom) => mountedMap.get(a),
dev_restore_atoms: (values: Iterable<readonly [AnyAtom, AnyValue]>) => {
for (const [atom, value] of values) {
if (hasInitialValue(atom)) {
setAtomValue(atom, value)
recomputeDependents(atom)
}
}
flushPending()
},
}
}
return {
get: readAtom,
set: writeAtom,
sub: subscribeAtom,
res: restoreAtoms,
}
}

Expand Down

1 comment on commit 8ccaa49

@vercel
Copy link

@vercel vercel bot commented on 8ccaa49 Dec 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.