-
-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rstream): add fromView(), update fromAtom() docs, update re-exports
- Loading branch information
1 parent
01a751e
commit 41bb385
Showing
3 changed files
with
67 additions
and
7 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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { ReadonlyAtom, ViewTransform } from "@thi.ng/atom/api"; | ||
import { View } from "@thi.ng/atom/view"; | ||
import { Stream } from "../stream"; | ||
import { Path } from "@thi.ng/paths"; | ||
import { Predicate2 } from "@thi.ng/api/api"; | ||
|
||
/** | ||
* Similar to `fromAtom()`, but creates an eager derived view for a | ||
* nested value in atom / cursor and yields stream of its value changes. | ||
* Views are readonly versions of Cursors and more lightweight. The view | ||
* checks for value changes with given `equiv` predicate | ||
* (`@thi.ng/api/equiv` by default). If the predicate returns a falsy | ||
* result, the new value is emitted on the stream. The first value | ||
* emitted is always the (possibly transformed) current value at the | ||
* stream's start time (i.e. when the first subscriber attaches). | ||
* | ||
* If the optional `tx` is given, the raw value is first passed to this | ||
* transformer function and its result emitted on the stream. | ||
* | ||
* When the stream is cancelled the view is destroyed as well. | ||
* | ||
* See: | ||
* - fromAtom() | ||
* - @thi.ng/atom | ||
* | ||
* ``` | ||
* db = new Atom({a: 1, b: {c: 2}}); | ||
* | ||
* fromView(db, "b.c", (x) => x != null ? x : "n/a").subscribe(trace("view:")) | ||
* // view: 2 | ||
* | ||
* db.swapIn("b.c", (x: number) => x + 1); | ||
* // view: 3 | ||
* | ||
* db.reset({a: 10}); | ||
* // view: n/a | ||
* ``` | ||
* | ||
* @param atom | ||
* @param path | ||
* @param tx | ||
*/ | ||
export function fromView<T>(atom: ReadonlyAtom<any>, path: Path, tx?: ViewTransform<T>, equiv?: Predicate2<any>): Stream<T> { | ||
return new Stream<T>((stream) => { | ||
let isActive = true; | ||
const view = new View<T>( | ||
atom, | ||
path, | ||
tx ? | ||
(x) => isActive && (x = tx(x), stream.next(x), x) : | ||
(x) => isActive && (stream.next(x), x), | ||
false, | ||
equiv | ||
); | ||
return () => (isActive = false, view.release()); | ||
}); | ||
} |
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