@reactivex/rxjs
Version:
Reactive Extensions for modern JavaScript
55 lines • 1.76 kB
JavaScript
import { OuterSubscriber } from '../OuterSubscriber';
import { subscribeToResult } from '../util/subscribeToResult';
/**
* Returns an Observable that, when the specified sampler Observable emits an item or completes, it then emits the most
* recently emitted item (if any) emitted by the source Observable since the previous emission from the sampler
* Observable.
*
* <img src="./img/sample.png" width="100%">
*
* @param {Observable} sampler - the Observable to use for sampling the source Observable.
* @return {Observable<T>} an Observable that emits the results of sampling the items emitted by this Observable
* whenever the sampler Observable emits an item or completes.
* @method sample
* @owner Observable
*/
export function sample(notifier) {
return this.lift(new SampleOperator(notifier));
}
class SampleOperator {
constructor(notifier) {
this.notifier = notifier;
}
call(subscriber, source) {
return source._subscribe(new SampleSubscriber(subscriber, this.notifier));
}
}
/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
class SampleSubscriber extends OuterSubscriber {
constructor(destination, notifier) {
super(destination);
this.hasValue = false;
this.add(subscribeToResult(this, notifier));
}
_next(value) {
this.value = value;
this.hasValue = true;
}
notifyNext(outerValue, innerValue, outerIndex, innerIndex, innerSub) {
this.emitValue();
}
notifyComplete() {
this.emitValue();
}
emitValue() {
if (this.hasValue) {
this.hasValue = false;
this.destination.next(this.value);
}
}
}
//# sourceMappingURL=sample.js.map