-
-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(endWhen): implement operator endWhen(), add tests
- Loading branch information
Andre Medeiros
committed
Mar 30, 2016
1 parent
65ddbd4
commit 23099ef
Showing
5 changed files
with
118 additions
and
1 deletion.
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,70 @@ | ||
import {InternalListener} from '../InternalListener'; | ||
import {Operator} from '../Operator'; | ||
import {Stream} from '../Stream'; | ||
import {emptyListener} from '../utils/emptyListener'; | ||
import {empty} from '../utils/empty'; | ||
|
||
export class Proxy<T> implements InternalListener<T> { | ||
constructor(private out: Stream<T>, | ||
private prod: EndWhenOperator<T>) { | ||
} | ||
|
||
_n(t: T) { | ||
this.out._n(t); | ||
} | ||
|
||
_e(err: any) { | ||
this.out._e(err); | ||
} | ||
|
||
_c() { | ||
this.prod.end(); | ||
} | ||
} | ||
|
||
export class OtherListener<T> implements InternalListener<any> { | ||
constructor(private out: Stream<T>, | ||
private prod: EndWhenOperator<T>) { | ||
} | ||
|
||
_n(t: T) { | ||
this.prod.end(); | ||
} | ||
|
||
_e(err: any) { | ||
this.out._e(err); | ||
} | ||
|
||
_c() { | ||
this.prod.end(); | ||
} | ||
} | ||
|
||
export class EndWhenOperator<T> implements Operator<T, T> { | ||
private proxy: InternalListener<T> = emptyListener; | ||
private oli: InternalListener<any> = emptyListener; // oli = other listener | ||
private out: Stream<T> = <Stream<T>> empty; | ||
|
||
constructor(private o: Stream<any>, // o = other | ||
public ins: Stream<T>) { | ||
} | ||
|
||
_start(out: Stream<T>): void { | ||
this.out = out; | ||
this.o._add(this.oli = new OtherListener(out, this)); | ||
this.ins._add(this.proxy = new Proxy(out, this)); | ||
} | ||
|
||
_stop(): void { | ||
this.ins._remove(this.proxy); | ||
} | ||
|
||
end(): void { | ||
this.o._remove(this.oli); | ||
this.ins._remove(this.proxy); | ||
this.out._c(); | ||
this.proxy = null; | ||
this.out = null; | ||
this.oli = null; | ||
} | ||
} |
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,41 @@ | ||
import xs from '../../src/index'; | ||
import delay from '../../src/extra/delay'; | ||
import * as assert from 'assert'; | ||
|
||
describe('Stream.prototype.endWhen', () => { | ||
it('should complete the stream when another stream emits next', (done) => { | ||
const source = xs.interval(50); | ||
const other = xs.interval(220).take(1); | ||
const stream = source.endWhen(other); | ||
const expected = [0, 1, 2, 3]; | ||
|
||
stream.addListener({ | ||
next: (x: number) => { | ||
assert.equal(x, expected.shift()); | ||
}, | ||
error: (err: any) => done(err), | ||
complete: () => { | ||
assert.equal(expected.length, 0); | ||
done(); | ||
}, | ||
}); | ||
}); | ||
|
||
it('should complete the stream when another stream emits complete', (done) => { | ||
const source = xs.interval(50); | ||
const other = xs.empty().compose(delay<any>(220)); | ||
const stream = source.endWhen(other); | ||
const expected = [0, 1, 2, 3]; | ||
|
||
stream.addListener({ | ||
next: (x: number) => { | ||
assert.equal(x, expected.shift()); | ||
}, | ||
error: (err: any) => done(err), | ||
complete: () => { | ||
assert.equal(expected.length, 0); | ||
done(); | ||
}, | ||
}); | ||
}); | ||
}); |
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