Skip to content

Commit

Permalink
feat(rstream): re-implement bisect() using PubSub, update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed May 20, 2018
1 parent cbc600e commit 846aaf9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 23 deletions.
48 changes: 36 additions & 12 deletions packages/rstream/src/subs/bisect.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
import { Predicate } from "@thi.ng/api/api";

import { ISubscriber } from "../api";
import { Subscription } from "../subscription";
import { PubSub } from "../pubsub";

export function bisect<T>(pred: Predicate<T>, a?: ISubscriber<T>, b?: ISubscriber<T>) {
return new Subscription<T, T>({
next(x) {
const sub = pred(x) ? a : b;
sub.next && sub.next(x);
},
done() {
a.done && a.done();
b.done && b.done();
}
});
/**
* Returns a new `PubSub` instance using given predicate `pred` as
* boolean topic function and `a` & `b` as subscribers for truthy (`a`)
* and falsey `b` values.
*
* ```
* rs.fromIterable([1,2,3,4]).subscribe(
* rs.bisect(
* (x) => !(x & 1),
* { next: (x) => console.log("even", x) },
* { next: (x) => console.log("odd", x) }
* )
* );
* ```
*
* If `a` or `b` need to be subscribed to directly, then `a` / `b` MUST
* be converted into a `Subscription` instance (if not already) and a
* reference kept prior to calling `bisect()`.
*
* ```
* const even = new rs.Subscription({next: (x) => console.log("even", x) });
* const odd = new rs.Subscription({next: (x) => console.log("odd", x) });
*
* rs.fromIterable([1,2,3,4]).subscribe(rs.bisect((x) => !(x & 1), even, odd));
* ```
*
* @param pred
* @param a
* @param b
*/
export function bisect<T>(pred: Predicate<T>, a?: ISubscriber<T>, b?: ISubscriber<T>): PubSub<T, T> {
const sub = new PubSub<T, T>({ topic: pred });
sub.subscribeTopic(true, a);
sub.subscribeTopic(false, b);
return sub;
}
23 changes: 12 additions & 11 deletions packages/rstream/test/bisect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ describe("bisect", () => {
it("raw subscribers", (done) => {
const odds = [], evens = [];
src.subscribe(
rs.bisect((x) => !!(x & 1),
rs.bisect<number>((x) => !!(x & 1),
{ next(x) { odds.push(x) } },
{ next(x) { evens.push(x) } }
)
).subscribe({
);
src.subscribe({
done() {
assert.deepEqual(odds, [1, 3]);
assert.deepEqual(evens, [2, 4]);
Expand All @@ -37,14 +38,14 @@ describe("bisect", () => {
tx.map<number, number>(x => x * 100)
);
let doneCount = 0;
src.subscribe(rs.bisect((x) => !!(x & 1), subo, sube))
.subscribe({
done() {
assert.deepEqual(odds, [10, 30]);
assert.deepEqual(evens, [200, 400]);
assert.equal(doneCount, 2);
done();
}
});
src.subscribe(rs.bisect((x) => !!(x & 1), subo, sube));
src.subscribe({
done() {
assert.deepEqual(odds, [10, 30]);
assert.deepEqual(evens, [200, 400]);
assert.equal(doneCount, 2);
done();
}
});
});
});

0 comments on commit 846aaf9

Please sign in to comment.