-
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.
Merge pull request #3177 from benlesh/smaller-timeInterval
refactor(timeInterval): implement in terms of scan
- Loading branch information
Showing
1 changed file
with
17 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<T>(scheduler: SchedulerLike = async): OperatorFunction<T, TimeInterval<T>> { | ||
return (source: Observable<T>) => source.lift(new TimeIntervalOperator(scheduler)); | ||
} | ||
|
||
export class TimeInterval<T> implements TimeIntervalInterface<T> { | ||
constructor(public value: T, public interval: number) { | ||
|
||
} | ||
} | ||
|
||
class TimeIntervalOperator<T> implements Operator<T, TimeInterval<T>> { | ||
constructor(private scheduler: SchedulerLike) { | ||
|
||
} | ||
|
||
call(observer: Subscriber<TimeInterval<T>>, source: any): any { | ||
return source.subscribe(new TimeIntervalSubscriber(observer, this.scheduler)); | ||
} | ||
return (source: Observable<T>) => 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<any, TimeInterval<T>>(({ current, last, value }) => new TimeInterval(value, current - last)), | ||
); | ||
}); | ||
} | ||
|
||
/** | ||
* We need this JSDoc comment for affecting ESDoc. | ||
* @ignore | ||
* @extends {Ignored} | ||
*/ | ||
class TimeIntervalSubscriber<T> extends Subscriber<T> { | ||
private lastTime: number = 0; | ||
|
||
constructor(destination: Subscriber<TimeInterval<T>>, 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<T> { | ||
constructor(public value: T, public interval: number) {} | ||
} |