-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(timestamp): add higher-order lettable version of timestamp
- Loading branch information
Showing
4 changed files
with
26 additions
and
31 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 |
---|---|---|
@@ -1,41 +1,14 @@ | ||
import { Operator } from '../Operator'; | ||
import { Observable } from '../Observable'; | ||
import { Subscriber } from '../Subscriber'; | ||
import { IScheduler } from '../Scheduler'; | ||
import { async } from '../scheduler/async'; | ||
|
||
import { timestamp as higherOrder } from '../operators'; | ||
import { Timestamp } from '../operators/timestamp'; | ||
/** | ||
* @param scheduler | ||
* @return {Observable<Timestamp<any>>|WebSocketSubject<T>|Observable<T>} | ||
* @method timestamp | ||
* @owner Observable | ||
*/ | ||
export function timestamp<T>(this: Observable<T>, scheduler: IScheduler = async): Observable<Timestamp<T>> { | ||
return this.lift(new TimestampOperator(scheduler)); | ||
} | ||
|
||
export class Timestamp<T> { | ||
constructor(public value: T, public timestamp: number) { | ||
} | ||
}; | ||
|
||
class TimestampOperator<T> implements Operator<T, Timestamp<T>> { | ||
constructor(private scheduler: IScheduler) { | ||
} | ||
|
||
call(observer: Subscriber<Timestamp<T>>, source: any): any { | ||
return source.subscribe(new TimestampSubscriber(observer, this.scheduler)); | ||
} | ||
} | ||
|
||
class TimestampSubscriber<T> extends Subscriber<T> { | ||
constructor(destination: Subscriber<Timestamp<T>>, private scheduler: IScheduler) { | ||
super(destination); | ||
} | ||
|
||
protected _next(value: T): void { | ||
const now = this.scheduler.now(); | ||
|
||
this.destination.next(new Timestamp(value, now)); | ||
} | ||
return higherOrder(scheduler)(this); | ||
} |
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,21 @@ | ||
|
||
import { IScheduler } from '../Scheduler'; | ||
import { async } from '../scheduler/async'; | ||
import { OperatorFunction } from '../interfaces'; | ||
import { map } from './map'; | ||
|
||
/** | ||
* @param scheduler | ||
* @return {Observable<Timestamp<any>>|WebSocketSubject<T>|Observable<T>} | ||
* @method timestamp | ||
* @owner Observable | ||
*/ | ||
export function timestamp<T>(scheduler: IScheduler = async): OperatorFunction<T, Timestamp<T>> { | ||
return map((value: T) => new Timestamp(value, scheduler.now())); | ||
// return (source: Observable<T>) => source.lift(new TimestampOperator(scheduler)); | ||
} | ||
|
||
export class Timestamp<T> { | ||
constructor(public value: T, public timestamp: number) { | ||
} | ||
}; |