-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
44 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
import type { Atom } from './types.js'; | ||
|
||
import { store } from './store.js'; | ||
import { unsafe_store } from './store.js'; | ||
|
||
/** | ||
* Sets the state of the passed atom. | ||
*/ | ||
export const hydrateAtom = <T>(atom: Readonly<Atom<T>>, value: Readonly<T>) => { | ||
store.set(atom.key, value); | ||
export const hydrateAtom = <T>(atom: Atom<T>, value: T) => { | ||
unsafe_store.set(atom.key, value); | ||
}; | ||
|
||
/** | ||
* Resets a state of passed atom. | ||
*/ | ||
export const resetAtom = <T>(atom: Readonly<Atom<T>>) => { | ||
store.set(atom.key, atom.def); | ||
export const resetAtom = <T>(atom: Atom<T>) => { | ||
unsafe_store.set(atom.key, atom.def); | ||
}; | ||
|
||
/** | ||
* Resets states of passed atoms. | ||
*/ | ||
export const resetAtoms = (atoms: ReadonlyArray<Readonly<Atom<any>>>) => { | ||
atoms.forEach(resetAtom); | ||
export const resetAtoms = (atoms: ReadonlyArray<Atom<any>>) => { | ||
atoms.some(resetAtom); | ||
}; |
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 |
---|---|---|
@@ -1,18 +1,32 @@ | ||
export type Store = Map<string, unknown>; | ||
export type AtomKey = string | number | symbol; | ||
|
||
export type AtomUpdater<T> = (state: T) => T; | ||
export type AtomValOrUpdater<T> = T | AtomUpdater<T>; | ||
export type AtomStore = Map<AtomKey, any>; | ||
export type AtomStoreUpdate = Record<AtomKey, any>; | ||
|
||
export type Selector<T, S> = (partial: T) => S; | ||
export type AtomUpdater<Value, Update = Value> = (value: Value) => Update; | ||
export type AtomValueUpdate<Value, Update = Value> = Update | AtomUpdater<Value, Update>; | ||
|
||
export type AtomGetter<T> = () => T; | ||
export type AtomSetter<T> = (value: AtomValOrUpdater<T>) => T; | ||
export type AtomSubscribe<T> = (set: (value: T) => void) => () => void; | ||
export type AtomSelector<Value, Selected> = (value: Value) => Selected; | ||
|
||
export type Atom<T> = { | ||
key: string; | ||
def: T; | ||
get: AtomGetter<T>; | ||
set: AtomSetter<T>; | ||
sub: AtomSubscribe<T>; | ||
export type AtomGetter<Value> = () => Value; | ||
export type AtomSetter<Value, Update = Value> = (update: AtomValueUpdate<Value, Update>) => Value; | ||
|
||
export type AtomSubscribe<Value> = (next: (value: Value) => void) => () => void; | ||
|
||
export type Atom<Value, Update = Value> = { | ||
readonly key: AtomKey; | ||
readonly def: Value; | ||
|
||
get: AtomGetter<Value>; | ||
set: AtomSetter<Value, Update>; | ||
sub: AtomSubscribe<Value>; | ||
}; | ||
|
||
export type DynamicAtomGetter = <Value, Update = Value>( | ||
atom: Atom<Value, Update> | ||
) => Value; | ||
|
||
export type DynamicAtomSetter = <Value, Update = Value>( | ||
atom: Atom<Value, Update>, | ||
update: AtomValueUpdate<Value, Update> | ||
) => Value; |