-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: split functions into separate modules
- Loading branch information
1 parent
39bf743
commit 56ffae2
Showing
14 changed files
with
234 additions
and
215 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,25 @@ | ||
import { Atom } from "./atom"; | ||
import { _getState } from "./internal-state"; | ||
import { DeepImmutable } from "./internal-types"; | ||
import { _throwIfNotAtom } from "./utils"; | ||
|
||
/** | ||
* Dereferences (i.e. "*reads*") the current state of an [[Atom]]. The dereferenced value | ||
* should ___not___ be mutated. | ||
* | ||
* @param <S> the type of `atom`'s inner state | ||
* | ||
* @example | ||
```js | ||
import {Atom, deref} from '@libre/atom' | ||
const stateAtom = Atom.of({ count: 0 }) | ||
deref(stateAtom) // => { count: 0 } | ||
``` | ||
*/ | ||
export function deref<S>(atom: Atom<S>): DeepImmutable<S> { | ||
_throwIfNotAtom(atom); | ||
return _getState(atom); | ||
} |
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,27 @@ | ||
import { Atom } from "./atom"; | ||
import { _getValidator } from "./internal-state"; | ||
import { AtomConstructorOptions } from "./internal-types"; | ||
|
||
import { _throwIfNotAtom } from "./utils"; | ||
|
||
/** | ||
* Gets `atom`'s validator function | ||
* | ||
* @param <S> the type of `atom`'s inner state | ||
* | ||
* @example | ||
```js | ||
import {Atom, deref, getValidator, swap} from '@libre/atom' | ||
const atom = Atom.of({ count: 0 }, { validator: (state) => isEven(state.count) }) | ||
const validator = getValidator(atom) | ||
validator({ count: 3 }) // => false | ||
validator({ count: 2 }) // => true | ||
``` | ||
*/ | ||
|
||
export function getValidator<S>(atom: Atom<S>): NonNullable<AtomConstructorOptions<any>["validator"]> { | ||
_throwIfNotAtom(atom); | ||
return _getValidator(atom); | ||
} |
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,2 +1,7 @@ | ||
export { Atom, deref, swap, set, setValidator, getValidator } from "./atom"; | ||
export { Atom } from "./atom"; | ||
export { AtomState } from "./internal-types"; | ||
export { deref } from "./deref"; | ||
export { getValidator } from "./getValidator"; | ||
export { set } from "./set"; | ||
export { setValidator } from "./setValidator"; | ||
export { swap } from "./swap"; |
Oops, something went wrong.