1 /** PURE_IMPORTS_START tslib,_Subscription,_util_subscribeToResult,_OuterSubscriber PURE_IMPORTS_END */
2 import * as tslib_1 from "tslib";
3 import { Subscription } from '../Subscription';
4 import { subscribeToResult } from '../util/subscribeToResult';
5 import { OuterSubscriber } from '../OuterSubscriber';
6 export function bufferToggle(openings, closingSelector) {
7 return function bufferToggleOperatorFunction(source) {
8 return source.lift(new BufferToggleOperator(openings, closingSelector));
11 var BufferToggleOperator = /*@__PURE__*/ (function () {
12 function BufferToggleOperator(openings, closingSelector) {
13 this.openings = openings;
14 this.closingSelector = closingSelector;
16 BufferToggleOperator.prototype.call = function (subscriber, source) {
17 return source.subscribe(new BufferToggleSubscriber(subscriber, this.openings, this.closingSelector));
19 return BufferToggleOperator;
21 var BufferToggleSubscriber = /*@__PURE__*/ (function (_super) {
22 tslib_1.__extends(BufferToggleSubscriber, _super);
23 function BufferToggleSubscriber(destination, openings, closingSelector) {
24 var _this = _super.call(this, destination) || this;
25 _this.openings = openings;
26 _this.closingSelector = closingSelector;
28 _this.add(subscribeToResult(_this, openings));
31 BufferToggleSubscriber.prototype._next = function (value) {
32 var contexts = this.contexts;
33 var len = contexts.length;
34 for (var i = 0; i < len; i++) {
35 contexts[i].buffer.push(value);
38 BufferToggleSubscriber.prototype._error = function (err) {
39 var contexts = this.contexts;
40 while (contexts.length > 0) {
41 var context_1 = contexts.shift();
42 context_1.subscription.unsubscribe();
43 context_1.buffer = null;
44 context_1.subscription = null;
47 _super.prototype._error.call(this, err);
49 BufferToggleSubscriber.prototype._complete = function () {
50 var contexts = this.contexts;
51 while (contexts.length > 0) {
52 var context_2 = contexts.shift();
53 this.destination.next(context_2.buffer);
54 context_2.subscription.unsubscribe();
55 context_2.buffer = null;
56 context_2.subscription = null;
59 _super.prototype._complete.call(this);
61 BufferToggleSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
62 outerValue ? this.closeBuffer(outerValue) : this.openBuffer(innerValue);
64 BufferToggleSubscriber.prototype.notifyComplete = function (innerSub) {
65 this.closeBuffer(innerSub.context);
67 BufferToggleSubscriber.prototype.openBuffer = function (value) {
69 var closingSelector = this.closingSelector;
70 var closingNotifier = closingSelector.call(this, value);
71 if (closingNotifier) {
72 this.trySubscribe(closingNotifier);
79 BufferToggleSubscriber.prototype.closeBuffer = function (context) {
80 var contexts = this.contexts;
81 if (contexts && context) {
82 var buffer = context.buffer, subscription = context.subscription;
83 this.destination.next(buffer);
84 contexts.splice(contexts.indexOf(context), 1);
85 this.remove(subscription);
86 subscription.unsubscribe();
89 BufferToggleSubscriber.prototype.trySubscribe = function (closingNotifier) {
90 var contexts = this.contexts;
92 var subscription = new Subscription();
93 var context = { buffer: buffer, subscription: subscription };
94 contexts.push(context);
95 var innerSubscription = subscribeToResult(this, closingNotifier, context);
96 if (!innerSubscription || innerSubscription.closed) {
97 this.closeBuffer(context);
100 innerSubscription.context = context;
101 this.add(innerSubscription);
102 subscription.add(innerSubscription);
105 return BufferToggleSubscriber;
107 //# sourceMappingURL=bufferToggle.js.map