-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from udecode/feat/extend
Add APIs to support working with custom and derived atoms
- Loading branch information
Showing
9 changed files
with
768 additions
and
161 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
'jotai-x': minor | ||
--- | ||
|
||
- Atoms other than `atom` can now be passed in the `initialState` argument to `createAtomStore`. Primitive values use `atom` by default | ||
- Added an `extend` option to `createAtomStore` that lets you add derived atoms to the store | ||
- New accessors on `UseStoreApi` | ||
- `useMyStore().store()` returns the `JotaiStore` for the current context, or undefined if no store exists | ||
- `useMyStore().{get,set,use}.atom(someAtom)` accesses `someAtom` through the store | ||
- Types: remove exports for some internal types | ||
- `GetRecord` | ||
- `SetRecord` | ||
- `UseRecord` |
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,32 @@ | ||
import { atom } from 'jotai'; | ||
|
||
import type { WritableAtom } from 'jotai/vanilla'; | ||
|
||
type WrapFn<T> = T extends (...args: infer _A) => infer _R ? { __fn: T } : T; | ||
|
||
const wrapFn = <T>(fnOrValue: T): WrapFn<T> => | ||
(typeof fnOrValue === 'function' ? { __fn: fnOrValue } : fnOrValue) as any; | ||
|
||
type UnwrapFn<T> = T extends { __fn: infer U } ? U : T; | ||
|
||
const unwrapFn = <T>(wrappedFnOrValue: T): UnwrapFn<T> => | ||
(wrappedFnOrValue && | ||
typeof wrappedFnOrValue === 'object' && | ||
'__fn' in wrappedFnOrValue | ||
? wrappedFnOrValue.__fn | ||
: wrappedFnOrValue) as any; | ||
|
||
/** | ||
* Jotai atoms don't allow functions as values by default. This function is a | ||
* drop-in replacement for `atom` that wraps functions in an object while | ||
* leaving non-functions unchanged. The wrapper object should be completely | ||
* invisible to consumers of the atom. | ||
*/ | ||
export const atomWithFn = <T>(initialValue: T): WritableAtom<T, [T], void> => { | ||
const baseAtom = atom(wrapFn(initialValue)); | ||
|
||
return atom( | ||
(get) => unwrapFn(get(baseAtom)) as T, | ||
(_get, set, value) => set(baseAtom, wrapFn(value)) | ||
); | ||
}; |
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
Oops, something went wrong.