-
-
Notifications
You must be signed in to change notification settings - Fork 642
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* wip * useHydrateAtoms wip * Add first test * More test cases * Simplify implementation * Update size-snapshot * Revert to original implementation * Optimize hook and add tests * Fix types * Fix types * Use symbol property for hydrated check * Allow hydration on atom once per store * Add scope test * Use new useAtom API for scope * Update size-snapshot * Fix isDevStore * Fix typo - use weakset instead of set
- Loading branch information
Mathis Møller
authored
Aug 10, 2021
1 parent
59d2613
commit 95cda07
Showing
8 changed files
with
339 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { useContext } from 'react' | ||
import { SECRET_INTERNAL_getStoreContext as getStoreContext } from 'jotai' | ||
import type { Atom, Scope } from '../core/atom' | ||
import type { Store } from '../core/contexts' | ||
|
||
const hydratedMap: WeakMap<Store, WeakSet<Atom<unknown>>> = new WeakMap() | ||
|
||
export function useHydrateAtoms( | ||
values: Iterable<readonly [Atom<unknown>, unknown]>, | ||
scope?: Scope | ||
) { | ||
const StoreContext = getStoreContext(scope) | ||
const store = useContext(StoreContext) | ||
const restoreAtoms = store[3] | ||
|
||
const hydratedSet = getHydratedSet(store) | ||
const tuplesToRestore = [] | ||
for (const tuple of values) { | ||
const atom = tuple[0] | ||
if (!hydratedSet.has(atom)) { | ||
hydratedSet.add(atom) | ||
tuplesToRestore.push(tuple) | ||
} | ||
} | ||
if (tuplesToRestore.length) { | ||
restoreAtoms(tuplesToRestore) | ||
} | ||
} | ||
|
||
function getHydratedSet(store: Store) { | ||
let hydratedSet = hydratedMap.get(store) | ||
if (!hydratedSet) { | ||
hydratedSet = new WeakSet() | ||
hydratedMap.set(store, hydratedSet) | ||
} | ||
return hydratedSet | ||
} |
Oops, something went wrong.
95cda07
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: