Skip to content

Commit ba6a9ce

Browse files
committed
feat(sampleTime): add higher-order lettable version of sampleTime
1 parent 8c73e6e commit ba6a9ce

File tree

3 files changed

+97
-50
lines changed

3 files changed

+97
-50
lines changed

src/operator/sampleTime.ts

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import { Observable } from '../Observable';
2-
import { Operator } from '../Operator';
3-
import { Subscriber } from '../Subscriber';
42
import { IScheduler } from '../Scheduler';
5-
import { Action } from '../scheduler/Action';
63
import { async } from '../scheduler/async';
7-
import { TeardownLogic } from '../Subscription';
4+
import { sampleTime as higherOrder } from '../operators/sampleTime';
85

96
/**
107
* Emits the most recently emitted value from the source Observable within
@@ -43,50 +40,5 @@ import { TeardownLogic } from '../Subscription';
4340
* @owner Observable
4441
*/
4542
export function sampleTime<T>(this: Observable<T>, period: number, scheduler: IScheduler = async): Observable<T> {
46-
return this.lift(new SampleTimeOperator(period, scheduler));
47-
}
48-
49-
class SampleTimeOperator<T> implements Operator<T, T> {
50-
constructor(private period: number,
51-
private scheduler: IScheduler) {
52-
}
53-
54-
call(subscriber: Subscriber<T>, source: any): TeardownLogic {
55-
return source.subscribe(new SampleTimeSubscriber(subscriber, this.period, this.scheduler));
56-
}
57-
}
58-
59-
/**
60-
* We need this JSDoc comment for affecting ESDoc.
61-
* @ignore
62-
* @extends {Ignored}
63-
*/
64-
class SampleTimeSubscriber<T> extends Subscriber<T> {
65-
lastValue: T;
66-
hasValue: boolean = false;
67-
68-
constructor(destination: Subscriber<T>,
69-
private period: number,
70-
private scheduler: IScheduler) {
71-
super(destination);
72-
this.add(scheduler.schedule(dispatchNotification, period, { subscriber: this, period }));
73-
}
74-
75-
protected _next(value: T) {
76-
this.lastValue = value;
77-
this.hasValue = true;
78-
}
79-
80-
notifyNext() {
81-
if (this.hasValue) {
82-
this.hasValue = false;
83-
this.destination.next(this.lastValue);
84-
}
85-
}
86-
}
87-
88-
function dispatchNotification<T>(this: Action<any>, state: any) {
89-
let { subscriber, period } = state;
90-
subscriber.notifyNext();
91-
this.schedule(state, period);
43+
return higherOrder(period, scheduler)(this);
9244
}

src/operators/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export { retry } from './retry';
5959
export { retryWhen } from './retryWhen';
6060
export { refCount } from './refCount';
6161
export { sample } from './sample';
62+
export { sampleTime } from './sampleTime';
6263
export { scan } from './scan';
6364
export { subscribeOn } from './subscribeOn';
6465
export { switchAll } from './switchAll';

src/operators/sampleTime.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { Observable } from '../Observable';
2+
import { Operator } from '../Operator';
3+
import { Subscriber } from '../Subscriber';
4+
import { IScheduler } from '../Scheduler';
5+
import { Action } from '../scheduler/Action';
6+
import { async } from '../scheduler/async';
7+
import { TeardownLogic } from '../Subscription';
8+
9+
import { MonoTypeOperatorFunction } from '../interfaces';
10+
11+
/**
12+
* Emits the most recently emitted value from the source Observable within
13+
* periodic time intervals.
14+
*
15+
* <span class="informal">Samples the source Observable at periodic time
16+
* intervals, emitting what it samples.</span>
17+
*
18+
* <img src="./img/sampleTime.png" width="100%">
19+
*
20+
* `sampleTime` periodically looks at the source Observable and emits whichever
21+
* value it has most recently emitted since the previous sampling, unless the
22+
* source has not emitted anything since the previous sampling. The sampling
23+
* happens periodically in time every `period` milliseconds (or the time unit
24+
* defined by the optional `scheduler` argument). The sampling starts as soon as
25+
* the output Observable is subscribed.
26+
*
27+
* @example <caption>Every second, emit the most recent click at most once</caption>
28+
* var clicks = Rx.Observable.fromEvent(document, 'click');
29+
* var result = clicks.sampleTime(1000);
30+
* result.subscribe(x => console.log(x));
31+
*
32+
* @see {@link auditTime}
33+
* @see {@link debounceTime}
34+
* @see {@link delay}
35+
* @see {@link sample}
36+
* @see {@link throttleTime}
37+
*
38+
* @param {number} period The sampling period expressed in milliseconds or the
39+
* time unit determined internally by the optional `scheduler`.
40+
* @param {Scheduler} [scheduler=async] The {@link IScheduler} to use for
41+
* managing the timers that handle the sampling.
42+
* @return {Observable<T>} An Observable that emits the results of sampling the
43+
* values emitted by the source Observable at the specified time interval.
44+
* @method sampleTime
45+
* @owner Observable
46+
*/
47+
export function sampleTime<T>(period: number, scheduler: IScheduler = async): MonoTypeOperatorFunction<T> {
48+
return (source: Observable<T>) => source.lift(new SampleTimeOperator(period, scheduler));
49+
}
50+
51+
class SampleTimeOperator<T> implements Operator<T, T> {
52+
constructor(private period: number,
53+
private scheduler: IScheduler) {
54+
}
55+
56+
call(subscriber: Subscriber<T>, source: any): TeardownLogic {
57+
return source.subscribe(new SampleTimeSubscriber(subscriber, this.period, this.scheduler));
58+
}
59+
}
60+
61+
/**
62+
* We need this JSDoc comment for affecting ESDoc.
63+
* @ignore
64+
* @extends {Ignored}
65+
*/
66+
class SampleTimeSubscriber<T> extends Subscriber<T> {
67+
lastValue: T;
68+
hasValue: boolean = false;
69+
70+
constructor(destination: Subscriber<T>,
71+
private period: number,
72+
private scheduler: IScheduler) {
73+
super(destination);
74+
this.add(scheduler.schedule(dispatchNotification, period, { subscriber: this, period }));
75+
}
76+
77+
protected _next(value: T) {
78+
this.lastValue = value;
79+
this.hasValue = true;
80+
}
81+
82+
notifyNext() {
83+
if (this.hasValue) {
84+
this.hasValue = false;
85+
this.destination.next(this.lastValue);
86+
}
87+
}
88+
}
89+
90+
function dispatchNotification<T>(this: Action<any>, state: any) {
91+
let { subscriber, period } = state;
92+
subscriber.notifyNext();
93+
this.schedule(state, period);
94+
}

0 commit comments

Comments
 (0)