From a780bf223fa16ace9f6eff93f1c822105f495555 Mon Sep 17 00:00:00 2001 From: Ben Lesh Date: Wed, 12 Jul 2017 18:01:41 -0700 Subject: [PATCH] feat(timestamp): add higher-order lettable version of timestamp --- src/Rx.ts | 2 +- src/operator/timestamp.ts | 33 +++------------------------------ src/operators/index.ts | 1 + src/operators/timestamp.ts | 21 +++++++++++++++++++++ 4 files changed, 26 insertions(+), 31 deletions(-) create mode 100644 src/operators/timestamp.ts diff --git a/src/Rx.ts b/src/Rx.ts index 5ed10a520f..ff8e6ca712 100644 --- a/src/Rx.ts +++ b/src/Rx.ts @@ -158,7 +158,7 @@ export {ObjectUnsubscribedError} from './util/ObjectUnsubscribedError'; export {TimeoutError} from './util/TimeoutError'; export {UnsubscriptionError} from './util/UnsubscriptionError'; export {TimeInterval} from './operator/timeInterval'; -export {Timestamp} from './operator/timestamp'; +export {Timestamp} from './operators/timestamp'; export {TestScheduler} from './testing/TestScheduler'; export {VirtualTimeScheduler} from './scheduler/VirtualTimeScheduler'; export {AjaxRequest, AjaxResponse, AjaxError, AjaxTimeoutError} from './observable/dom/AjaxObservable'; diff --git a/src/operator/timestamp.ts b/src/operator/timestamp.ts index 29676af316..e40fbbbb1e 100644 --- a/src/operator/timestamp.ts +++ b/src/operator/timestamp.ts @@ -1,9 +1,8 @@ -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>|WebSocketSubject|Observable} @@ -11,31 +10,5 @@ import { async } from '../scheduler/async'; * @owner Observable */ export function timestamp(this: Observable, scheduler: IScheduler = async): Observable> { - return this.lift(new TimestampOperator(scheduler)); -} - -export class Timestamp { - constructor(public value: T, public timestamp: number) { - } -}; - -class TimestampOperator implements Operator> { - constructor(private scheduler: IScheduler) { - } - - call(observer: Subscriber>, source: any): any { - return source.subscribe(new TimestampSubscriber(observer, this.scheduler)); - } -} - -class TimestampSubscriber extends Subscriber { - constructor(destination: Subscriber>, 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); } diff --git a/src/operators/index.ts b/src/operators/index.ts index 55cff5b4c9..07388d0841 100644 --- a/src/operators/index.ts +++ b/src/operators/index.ts @@ -22,6 +22,7 @@ export { switchAll } from './switchAll'; export { switchMap } from './switchMap'; export { takeLast } from './takeLast'; export { tap } from './tap'; +export { timestamp } from './timestamp'; export { toArray } from './toArray'; export { window } from './window'; export { windowCount } from './windowCount'; diff --git a/src/operators/timestamp.ts b/src/operators/timestamp.ts new file mode 100644 index 0000000000..abb3121a6d --- /dev/null +++ b/src/operators/timestamp.ts @@ -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>|WebSocketSubject|Observable} + * @method timestamp + * @owner Observable + */ +export function timestamp(scheduler: IScheduler = async): OperatorFunction> { + return map((value: T) => new Timestamp(value, scheduler.now())); + // return (source: Observable) => source.lift(new TimestampOperator(scheduler)); +} + +export class Timestamp { + constructor(public value: T, public timestamp: number) { + } +};