-
-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/rstream-refactor' into develop
* feature/rstream-refactor: (23 commits) docs(rstream): update readme, add dataflow example docs(rstream): update readme feat(rstream): add IDeref impl for Subscription feat(rstream): add merge()/sync() ctor wrappers feat(rstream): add transduce(), update re-exports test(rstream): add StreamSync & Subscription tests refactor(rstream): simplify unsubscribe() logic docs(rstream): add Cache deprecation docstring feat(rstream): Subscription stores last value and passes to new subs refactor(rstream): update & StreamMerge/SyncOpts, minor fixes StreamSync feat(rstream): fix #6 update StreamMerge to support transduced input streams test(rstream): add/update sidechain* tests feat(rstream): update Sidechain*.next(), add unsubscribe() minor(rstream): fromPromise() feat(rstream): add fromView(), update fromAtom() docs, update re-exports feat(rstream): update Subscription.unsubscribe() fix(rstream): bisect() add downstream impl checks, add tests test(rstream): remove obsolete fromPromise error test case refactor(rstream): simplify Subscription, update all impls refactor(rstream): simplify StreamMerge source handling ...
- Loading branch information
Showing
21 changed files
with
817 additions
and
110 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
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
Oops, something went wrong.