diff --git a/src/internal/operators/timeInterval.ts b/src/internal/operators/timeInterval.ts index 8c2205e383..8e63aa790b 100644 --- a/src/internal/operators/timeInterval.ts +++ b/src/internal/operators/timeInterval.ts @@ -1,48 +1,24 @@ -import { Operator } from '../Operator'; + import { Observable } from '../Observable'; -import { Subscriber } from '../Subscriber'; import { async } from '../scheduler/async'; -import { OperatorFunction, SchedulerLike, TimeInterval as TimeIntervalInterface } from '../types'; +import { SchedulerLike, OperatorFunction } from '../types'; +import { scan } from './scan'; +import { defer } from '../observable/defer'; +import { map } from './map'; export function timeInterval(scheduler: SchedulerLike = async): OperatorFunction> { - return (source: Observable) => source.lift(new TimeIntervalOperator(scheduler)); -} - -export class TimeInterval implements TimeIntervalInterface { - constructor(public value: T, public interval: number) { - - } -} - -class TimeIntervalOperator implements Operator> { - constructor(private scheduler: SchedulerLike) { - - } - - call(observer: Subscriber>, source: any): any { - return source.subscribe(new TimeIntervalSubscriber(observer, this.scheduler)); - } + return (source: Observable) => defer(() => { + return source.pipe( + // HACK: the typings seem off with scan + scan( + ({ current }, value) => ({ value, current: scheduler.now(), last: current }), + { current: scheduler.now(), value: undefined, last: undefined } + ) as any, + map>(({ current, last, value }) => new TimeInterval(value, current - last)), + ); + }); } -/** - * We need this JSDoc comment for affecting ESDoc. - * @ignore - * @extends {Ignored} - */ -class TimeIntervalSubscriber extends Subscriber { - private lastTime: number = 0; - - constructor(destination: Subscriber>, private scheduler: SchedulerLike) { - super(destination); - - this.lastTime = scheduler.now(); - } - - protected _next(value: T) { - let now = this.scheduler.now(); - let span = now - this.lastTime; - this.lastTime = now; - - this.destination.next(new TimeInterval(value, span)); - } +export class TimeInterval { + constructor(public value: T, public interval: number) {} }