-
-
Notifications
You must be signed in to change notification settings - Fork 139
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 last() operator with LastMachine
- Loading branch information
Andre Medeiros
committed
Feb 26, 2016
1 parent
ad210fc
commit 747e255
Showing
3 changed files
with
60 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,36 @@ | ||
import {Observer} from '../Observer'; | ||
import {Machine} from '../Machine'; | ||
import {Stream} from '../Stream'; | ||
import {emptyObserver} from '../utils/emptyObserver'; | ||
|
||
export class LastMachine<T> implements Machine<T> { | ||
public proxy: Observer<T> = emptyObserver; | ||
public has: boolean = false; | ||
public val: T; | ||
|
||
constructor(public inStream: Stream<T>) { | ||
} | ||
|
||
start(outStream: Stream<T>): void { | ||
this.proxy = { | ||
next: (t: T) => { | ||
this.has = true; | ||
this.val = t; | ||
}, | ||
error: (err) => outStream.error(err), | ||
complete: () => { | ||
if (this.has) { | ||
outStream.next(this.val); | ||
outStream.complete(); | ||
} else { | ||
outStream.error('TODO show error about empty stream has no last value'); | ||
} | ||
}, | ||
}; | ||
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