UNPKG

@reactivex/rxjs

Version:

Reactive Extensions for modern JavaScript

197 lines 7.63 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 Subject_1 = require("../Subject"); var Subscription_1 = require("../Subscription"); var OuterSubscriber_1 = require("../OuterSubscriber"); var subscribeToResult_1 = require("../util/subscribeToResult"); /** * Branch out the source Observable values as a nested Observable starting from * an emission from `openings` and ending when the output of `closingSelector` * emits. * * <span class="informal">It's like {@link bufferToggle}, but emits a nested * Observable instead of an array.</span> * * ![](windowToggle.png) * * Returns an Observable that emits windows of items it collects from the source * Observable. The output Observable emits windows that contain those items * emitted by the source Observable between the time when the `openings` * Observable emits an item and when the Observable returned by * `closingSelector` emits an item. * * ## Example * Every other second, emit the click events from the next 500ms * ```ts * import { fromEvent, interval, EMPTY } from 'rxjs'; * import { windowToggle, mergeAll } from 'rxjs/operators'; * * const clicks = fromEvent(document, 'click'); * const openings = interval(1000); * const result = clicks.pipe( * windowToggle(openings, i => i % 2 ? interval(500) : EMPTY), * mergeAll() * ); * result.subscribe(x => console.log(x)); * ``` * * @see {@link window} * @see {@link windowCount} * @see {@link windowTime} * @see {@link windowWhen} * @see {@link bufferToggle} * * @param {Observable<O>} openings An observable of notifications to start new * windows. * @param {function(value: O): Observable} closingSelector A function that takes * the value emitted by the `openings` observable and returns an Observable, * which, when it emits (either `next` or `complete`), signals that the * associated window should complete. * @return {Observable<Observable<T>>} An observable of windows, which in turn * are Observables. * @method windowToggle * @owner Observable */ function windowToggle(openings, closingSelector) { return function (source) { return source.lift(new WindowToggleOperator(openings, closingSelector)); }; } exports.windowToggle = windowToggle; var WindowToggleOperator = /** @class */ (function () { function WindowToggleOperator(openings, closingSelector) { this.openings = openings; this.closingSelector = closingSelector; } WindowToggleOperator.prototype.call = function (subscriber, source) { return source.subscribe(new WindowToggleSubscriber(subscriber, this.openings, this.closingSelector)); }; return WindowToggleOperator; }()); /** * We need this JSDoc comment for affecting ESDoc. * @ignore * @extends {Ignored} */ var WindowToggleSubscriber = /** @class */ (function (_super) { __extends(WindowToggleSubscriber, _super); function WindowToggleSubscriber(destination, openings, closingSelector) { var _this = _super.call(this, destination) || this; _this.openings = openings; _this.closingSelector = closingSelector; _this.contexts = []; _this.add(_this.openSubscription = subscribeToResult_1.subscribeToResult(_this, openings, openings)); return _this; } WindowToggleSubscriber.prototype._next = function (value) { var contexts = this.contexts; if (contexts) { var len = contexts.length; for (var i = 0; i < len; i++) { contexts[i].window.next(value); } } }; WindowToggleSubscriber.prototype._error = function (err) { var contexts = this.contexts; this.contexts = null; if (contexts) { var len = contexts.length; var index = -1; while (++index < len) { var context_1 = contexts[index]; context_1.window.error(err); context_1.subscription.unsubscribe(); } } _super.prototype._error.call(this, err); }; WindowToggleSubscriber.prototype._complete = function () { var contexts = this.contexts; this.contexts = null; if (contexts) { var len = contexts.length; var index = -1; while (++index < len) { var context_2 = contexts[index]; context_2.window.complete(); context_2.subscription.unsubscribe(); } } _super.prototype._complete.call(this); }; /** @deprecated This is an internal implementation detail, do not use. */ WindowToggleSubscriber.prototype._unsubscribe = function () { var contexts = this.contexts; this.contexts = null; if (contexts) { var len = contexts.length; var index = -1; while (++index < len) { var context_3 = contexts[index]; context_3.window.unsubscribe(); context_3.subscription.unsubscribe(); } } }; WindowToggleSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) { if (outerValue === this.openings) { var closingNotifier = void 0; try { var closingSelector = this.closingSelector; closingNotifier = closingSelector(innerValue); } catch (e) { return this.error(e); } var window_1 = new Subject_1.Subject(); var subscription = new Subscription_1.Subscription(); var context_4 = { window: window_1, subscription: subscription }; this.contexts.push(context_4); var innerSubscription = subscribeToResult_1.subscribeToResult(this, closingNotifier, context_4); if (innerSubscription.closed) { this.closeWindow(this.contexts.length - 1); } else { innerSubscription.context = context_4; subscription.add(innerSubscription); } this.destination.next(window_1); } else { this.closeWindow(this.contexts.indexOf(outerValue)); } }; WindowToggleSubscriber.prototype.notifyError = function (err) { this.error(err); }; WindowToggleSubscriber.prototype.notifyComplete = function (inner) { if (inner !== this.openSubscription) { this.closeWindow(this.contexts.indexOf(inner.context)); } }; WindowToggleSubscriber.prototype.closeWindow = function (index) { if (index === -1) { return; } var contexts = this.contexts; var context = contexts[index]; var window = context.window, subscription = context.subscription; contexts.splice(index, 1); window.complete(); subscription.unsubscribe(); }; return WindowToggleSubscriber; }(OuterSubscriber_1.OuterSubscriber)); //# sourceMappingURL=windowToggle.js.map