Skip to content

Commit

Permalink
lift only switches to push if all parents are push
Browse files Browse the repository at this point in the history
  • Loading branch information
paldepind committed Sep 15, 2019
1 parent ea5cb1e commit 1cad54b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/behavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,18 @@ export class LiftBehavior<A extends any[], R> extends Behavior<R> {
update(_t: number): R {
return this.f(...(this.bs.map((b) => b.last) as any));
}
changeStateDown(_: State): void {
let state = State.Done;
for (const p of this.parents) {
state = Math.max(state, p.state);
}
if (this.state !== state) {
this.state = state;
for (const child of this.children) {
child.changeStateDown(state);
}
}
}
}

class FlatMapBehavior<A, B> extends Behavior<B> {
Expand Down
25 changes: 25 additions & 0 deletions test/behavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,31 @@ describe("behavior", () => {
p.newValue(4);
assert.deepEqual(cb.args, [[16], [24]]);
});
it("only switched to push if all parents are push", () => {
// In this test the lifted behavior `b3` starts out in the pull state
// since both of its parents are in pull. Then one of the parents
// switches to push. But, since one parent is still in pull the lifted
// behavior should remain in pull.
let n = 1;
const b1 = H.fromFunction(() => n);
const fut = H.sinkFuture<H.Behavior<number>>();
const b2 = H.switchTo(fromFunction(() => 2), fut);
const b3 = H.lift((n, m) => n + m, b1, b2);
observe(
() => {},
() => {
return () => {
throw new Error("Should not be called.");
};
},
b3
);
expect(H.at(b3)).toEqual(3);
fut.resolve(H.sinkBehavior(5));
expect(H.at(b3)).toEqual(6);
n = 2;
expect(H.at(b3)).toEqual(7);
});
});
});
describe("flatMap", () => {
Expand Down

0 comments on commit 1cad54b

Please sign in to comment.