@libre/atom
Advanced tools
Comparing version 1.2.1 to 1.2.2
@@ -59,7 +59,13 @@ let nextAtomUid = 0; | ||
addChangeHandler(countAtom, "log", (state) => { console.log(state) }) | ||
addChangeHandler(countAtom, "log", ({current, previous}) => { | ||
console.log(previous, current) | ||
}) | ||
swap(countAtom, (state) => ({ count: state.count + 1 })) | ||
// stdout logs: { count: 1 } | ||
// stdout logs: | ||
// { count: 0 } | ||
// { count: 1 } | ||
``` | ||
@@ -73,2 +79,26 @@ */ | ||
* when the state of `atom` changes. | ||
* | ||
* * @example | ||
```js | ||
import {Atom, addChangeHandler, removeChangeHandler, swap} from '@libre/atom' | ||
const countAtom = Atom.of({ count: 0 }) | ||
addChangeHandler(countAtom, "log", ({current, previous}) => { | ||
console.log(previous, current) | ||
}) | ||
swap(countAtom, (state) => ({ count: state.count + 1 })) | ||
// stdout logs: | ||
// { count: 0 } | ||
// { count: 1 } | ||
removeChangeHandler(atom, "log") | ||
swap(countAtom, (state) => ({ count: state.count + 1 })) | ||
// nothing is logged | ||
``` | ||
*/ | ||
@@ -75,0 +105,0 @@ function removeChangeHandler(atom, key) { |
@@ -14,7 +14,13 @@ import { Atom } from "./atom"; | ||
addChangeHandler(countAtom, "log", (state) => { console.log(state) }) | ||
addChangeHandler(countAtom, "log", ({current, previous}) => { | ||
console.log(previous, current) | ||
}) | ||
swap(countAtom, (state) => ({ count: state.count + 1 })) | ||
// stdout logs: { count: 1 } | ||
// stdout logs: | ||
// { count: 0 } | ||
// { count: 1 } | ||
``` | ||
@@ -29,3 +35,27 @@ */ | ||
* when the state of `atom` changes. | ||
* | ||
* * @example | ||
```js | ||
import {Atom, addChangeHandler, removeChangeHandler, swap} from '@libre/atom' | ||
const countAtom = Atom.of({ count: 0 }) | ||
addChangeHandler(countAtom, "log", ({current, previous}) => { | ||
console.log(previous, current) | ||
}) | ||
swap(countAtom, (state) => ({ count: state.count + 1 })) | ||
// stdout logs: | ||
// { count: 0 } | ||
// { count: 1 } | ||
removeChangeHandler(atom, "log") | ||
swap(countAtom, (state) => ({ count: state.count + 1 })) | ||
// nothing is logged | ||
``` | ||
*/ | ||
export declare function removeChangeHandler<S>(atom: Atom<S>, key: string): void; |
import { Atom } from "./atom"; | ||
import { AtomConstructorOptions, DeepImmutable } from "./internal-types"; | ||
import { AtomConstructorOptions } from "./internal-types"; | ||
/** @ignore */ | ||
export declare function _useNextAtomId(): number; | ||
/** @ignore */ | ||
export declare function _getState<S>(atom: Atom<S>): DeepImmutable<S>; | ||
export declare function _getState<S>(atom: Atom<S>): S; | ||
/** @ignore */ | ||
@@ -8,0 +8,0 @@ export declare function _setState<S>(atom: Atom<S>, state: S): void; |
import { Atom } from "./atom"; | ||
import { DeepImmutable } from "./internal-types"; | ||
/** | ||
@@ -22,2 +21,2 @@ * Swaps `atom`'s state with the value returned from applying `updateFn` to `atom`'s | ||
*/ | ||
export declare function swap<S>(atom: Atom<S>, updateFn: (state: DeepImmutable<S>) => S): void; | ||
export declare function swap<S>(atom: Atom<S>, updateFn: (state: S) => S): void; |
@@ -92,3 +92,3 @@ { | ||
"unpkg": "https://unpkg.com/@libre/atom", | ||
"version": "1.2.1" | ||
"version": "1.2.2" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
62516
558