Skip to content

Commit

Permalink
Changes can activate on non-replaced placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
paldepind committed Aug 20, 2019
1 parent 69d7b2b commit fe4a377
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,19 +254,29 @@ export function shiftFrom<A>(s: Stream<Stream<A>>): Behavior<Stream<A>> {

class ChangesStream<A> extends Stream<A> implements BListener {
last: A;
initialized: boolean;
constructor(
readonly parent: Behavior<A>,
readonly comparator: (v: A, u: A) => boolean
) {
super();
this.parents = cons(parent);
this.initialized = false;
}
activate(t: Time): void {
super.activate(t);
this.last = at(this.parent, t);
// The parent may be an unreplaced placeholder and in that case
// we can't read its current value.
if (this.parent.state === State.Push) {
this.last = this.parent.last;
this.initialized = true;
}
}
pushB(t: number): void {
if (!this.comparator(this.last, this.parent.last)) {
if (!this.initialized) {
this.initialized = true;
this.last = this.parent.last;
} else if (!this.comparator(this.last, this.parent.last)) {
this.pushSToChildren(t, this.parent.last);
this.last = this.parent.last;
}
Expand Down
9 changes: 9 additions & 0 deletions test/placeholder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,15 @@ describe("placeholder", () => {
assert.deepEqual(results, [0, 2000, 5000]);
restore();
});
it("is possible to invoke changes on a placeholder", () => {
const p = H.placeholder<number>();
const b = H.changes(p);
const cb = subscribeSpy(b);
const sink = sinkBehavior(0);
p.replaceWith(sink);
sink.push(1);
assert.deepEqual(cb.args, [[1]]);
});
});
describe("stream", () => {
it("is stream", () => {
Expand Down

0 comments on commit fe4a377

Please sign in to comment.