-
-
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(operator): implement take operator with TakeMachine
- Loading branch information
Andre Medeiros
committed
Feb 24, 2016
1 parent
a74f160
commit 6e1d0db
Showing
3 changed files
with
57 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import {Observer} from '../Observer'; | ||
import {Machine} from '../Machine'; | ||
import {Stream} from '../Stream'; | ||
|
||
export class TakeMachine<T> implements Machine<T> { | ||
public proxy: Observer<T>; | ||
public taken: number; | ||
|
||
constructor(public max: number, | ||
public inStream: Stream<T>) { | ||
this.taken = 0; | ||
} | ||
|
||
start(outStream: Observer<T>): void { | ||
this.proxy = { | ||
next: (t: T) => { | ||
if (this.taken++ < this.max) { | ||
outStream.next(t); | ||
} else { | ||
outStream.complete(); | ||
this.stop(); | ||
} | ||
}, | ||
error: outStream.error, | ||
complete: outStream.complete, | ||
}; | ||
this.inStream.subscribe(this.proxy); | ||
} | ||
|
||
stop(): void { | ||
this.inStream.unsubscribe(this.proxy); | ||
} | ||
} |
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