UNPKG

@reactivex/rxjs

Version:

Reactive Extensions for modern JavaScript

103 lines 3.78 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); } return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var innerSubscribe_1 = require("../innerSubscribe"); /** * Emits the most recently emitted value from the source Observable whenever * another Observable, the `notifier`, emits. * * <span class="informal">It's like {@link sampleTime}, but samples whenever * the `notifier` Observable emits something.</span> * * ![](sample.png) * * Whenever the `notifier` Observable emits a value or completes, `sample` * looks at the source Observable and emits whichever value it has most recently * emitted since the previous sampling, unless the source has not emitted * anything since the previous sampling. The `notifier` is subscribed to as soon * as the output Observable is subscribed. * * ## Example * On every click, sample the most recent "seconds" timer * ```ts * import { fromEvent, interval } from 'rxjs'; * import { sample } from 'rxjs/operators'; * * const seconds = interval(1000); * const clicks = fromEvent(document, 'click'); * const result = seconds.pipe(sample(clicks)); * result.subscribe(x => console.log(x)); * ``` * * @see {@link audit} * @see {@link debounce} * @see {@link sampleTime} * @see {@link throttle} * * @param {Observable<any>} notifier The Observable to use for sampling the * source Observable. * @return {Observable<T>} An Observable that emits the results of sampling the * values emitted by the source Observable whenever the notifier Observable * emits value or completes. * @method sample * @owner Observable */ function sample(notifier) { return function (source) { return source.lift(new SampleOperator(notifier)); }; } exports.sample = sample; var SampleOperator = /** @class */ (function () { function SampleOperator(notifier) { this.notifier = notifier; } SampleOperator.prototype.call = function (subscriber, source) { var sampleSubscriber = new SampleSubscriber(subscriber); var subscription = source.subscribe(sampleSubscriber); subscription.add(innerSubscribe_1.innerSubscribe(this.notifier, new innerSubscribe_1.SimpleInnerSubscriber(sampleSubscriber))); return subscription; }; return SampleOperator; }()); /** * We need this JSDoc comment for affecting ESDoc. * @ignore * @extends {Ignored} */ var SampleSubscriber = /** @class */ (function (_super) { __extends(SampleSubscriber, _super); function SampleSubscriber() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.hasValue = false; return _this; } SampleSubscriber.prototype._next = function (value) { this.value = value; this.hasValue = true; }; SampleSubscriber.prototype.notifyNext = function () { this.emitValue(); }; SampleSubscriber.prototype.notifyComplete = function () { this.emitValue(); }; SampleSubscriber.prototype.emitValue = function () { if (this.hasValue) { this.hasValue = false; this.destination.next(this.value); } }; return SampleSubscriber; }(innerSubscribe_1.SimpleOuterSubscriber)); //# sourceMappingURL=sample.js.map