"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 Subscription_1 = require("../Subscription"); var subscribeToResult_1 = require("../util/subscribeToResult"); var OuterSubscriber_1 = require("../OuterSubscriber"); function bufferToggle(openings, closingSelector) { return function bufferToggleOperatorFunction(source) { return source.lift(new BufferToggleOperator(openings, closingSelector)); }; } exports.bufferToggle = bufferToggle; var BufferToggleOperator = (function () { function BufferToggleOperator(openings, closingSelector) { this.openings = openings; this.closingSelector = closingSelector; } BufferToggleOperator.prototype.call = function (subscriber, source) { return source.subscribe(new BufferToggleSubscriber(subscriber, this.openings, this.closingSelector)); }; return BufferToggleOperator; }()); var BufferToggleSubscriber = (function (_super) { __extends(BufferToggleSubscriber, _super); function BufferToggleSubscriber(destination, openings, closingSelector) { var _this = _super.call(this, destination) || this; _this.openings = openings; _this.closingSelector = closingSelector; _this.contexts = []; _this.add(subscribeToResult_1.subscribeToResult(_this, openings)); return _this; } BufferToggleSubscriber.prototype._next = function (value) { var contexts = this.contexts; var len = contexts.length; for (var i = 0; i < len; i++) { contexts[i].buffer.push(value); } }; BufferToggleSubscriber.prototype._error = function (err) { var contexts = this.contexts; while (contexts.length > 0) { var context_1 = contexts.shift(); context_1.subscription.unsubscribe(); context_1.buffer = null; context_1.subscription = null; } this.contexts = null; _super.prototype._error.call(this, err); }; BufferToggleSubscriber.prototype._complete = function () { var contexts = this.contexts; while (contexts.length > 0) { var context_2 = contexts.shift(); this.destination.next(context_2.buffer); context_2.subscription.unsubscribe(); context_2.buffer = null; context_2.subscription = null; } this.contexts = null; _super.prototype._complete.call(this); }; BufferToggleSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) { outerValue ? this.closeBuffer(outerValue) : this.openBuffer(innerValue); }; BufferToggleSubscriber.prototype.notifyComplete = function (innerSub) { this.closeBuffer(innerSub.context); }; BufferToggleSubscriber.prototype.openBuffer = function (value) { try { var closingSelector = this.closingSelector; var closingNotifier = closingSelector.call(this, value); if (closingNotifier) { this.trySubscribe(closingNotifier); } } catch (err) { this._error(err); } }; BufferToggleSubscriber.prototype.closeBuffer = function (context) { var contexts = this.contexts; if (contexts && context) { var buffer = context.buffer, subscription = context.subscription; this.destination.next(buffer); contexts.splice(contexts.indexOf(context), 1); this.remove(subscription); subscription.unsubscribe(); } }; BufferToggleSubscriber.prototype.trySubscribe = function (closingNotifier) { var contexts = this.contexts; var buffer = []; var subscription = new Subscription_1.Subscription(); var context = { buffer: buffer, subscription: subscription }; contexts.push(context); var innerSubscription = subscribeToResult_1.subscribeToResult(this, closingNotifier, context); if (!innerSubscription || innerSubscription.closed) { this.closeBuffer(context); } else { innerSubscription.context = context; this.add(innerSubscription); subscription.add(innerSubscription); } }; return BufferToggleSubscriber; }(OuterSubscriber_1.OuterSubscriber)); //# sourceMappingURL=bufferToggle.js.map