Skip to content

Commit

Permalink
fix(imitate): fix issue #66 with imitate()
Browse files Browse the repository at this point in the history
Fix imitate() so that existing listeners of a mimic stream are transferred to the imitation target
once imitate() is called.

Closes issue #66.
  • Loading branch information
staltz committed Jul 5, 2016
1 parent d87d229 commit 7aa3a04
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1718,6 +1718,10 @@ export class Stream<T> implements InternalListener<T> {
'https://github.com/staltz/xstream#faq');
}
this._target = target;
for (let ils = this._ils, N = ils.length, i = 0; i < N; i++) {
target._add(ils[i]);
}
this._ils = [];
}

/**
Expand Down
18 changes: 18 additions & 0 deletions tests/operator/imitate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,22 @@ describe('Stream.prototype.imitate', () => {
});
done();
});

it('should transfer existing listeners to imitation target', (done) => {
const mimic = xs.create<number>();
const expected = [0, 1, 2];

mimic.addListener({
next: (x: number) => {
assert.equal(x, expected.shift());
},
error: (err: any) => done(err),
complete: () => {
assert.equal(expected.length, 0);
done();
},
});

mimic.imitate(xs.periodic(50).take(3));
});
});

0 comments on commit 7aa3a04

Please sign in to comment.