Skip to content

Commit

Permalink
feat(timeInterval): add higher-order lettable version of timeInterval
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonaden committed Sep 8, 2017
1 parent 34a592d commit fcad034
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 42 deletions.
45 changes: 3 additions & 42 deletions src/operator/timeInterval.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Operator } from '../Operator';
import { Observable } from '../Observable';
import { Subscriber } from '../Subscriber';
import { IScheduler } from '../Scheduler';
import { async } from '../scheduler/async';
import { timeInterval as higherOrder, TimeInterval } from '../operators/timeInterval';
export {TimeInterval};

/**
* @param scheduler
Expand All @@ -11,44 +11,5 @@ import { async } from '../scheduler/async';
* @owner Observable
*/
export function timeInterval<T>(this: Observable<T>, scheduler: IScheduler = async): Observable<TimeInterval<T>> {
return this.lift(new TimeIntervalOperator(scheduler));
}

export class TimeInterval<T> {
constructor(public value: T, public interval: number) {

}
};

class TimeIntervalOperator<T> implements Operator<T, TimeInterval<T>> {
constructor(private scheduler: IScheduler) {

}

call(observer: Subscriber<TimeInterval<T>>, source: any): any {
return source.subscribe(new TimeIntervalSubscriber(observer, this.scheduler));
}
}

/**
* 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: IScheduler) {
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));
}
return higherOrder(scheduler)(this);
}
1 change: 1 addition & 0 deletions src/operators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export { takeLast } from './takeLast';
export { tap } from './tap';
export { throttle } from './throttle';
export { throttleTime } from './throttleTime';
export { timeInterval } from './timeInterval';
export { timestamp } from './timestamp';
export { toArray } from './toArray';
export { window } from './window';
Expand Down
49 changes: 49 additions & 0 deletions src/operators/timeInterval.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Operator } from '../Operator';
import { Observable } from '../Observable';
import { Subscriber } from '../Subscriber';
import { IScheduler } from '../Scheduler';
import { async } from '../scheduler/async';
import { OperatorFunction } from '../interfaces';

export function timeInterval<T>(scheduler: IScheduler = async): OperatorFunction<T, TimeInterval<T>> {
return (source: Observable<T>) => source.lift(new TimeIntervalOperator(scheduler));
}

export class TimeInterval<T> {
constructor(public value: T, public interval: number) {

}
};

class TimeIntervalOperator<T> implements Operator<T, TimeInterval<T>> {
constructor(private scheduler: IScheduler) {

}

call(observer: Subscriber<TimeInterval<T>>, source: any): any {
return source.subscribe(new TimeIntervalSubscriber(observer, this.scheduler));
}
}

/**
* 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: IScheduler) {
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));
}
}

0 comments on commit fcad034

Please sign in to comment.