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.closingSelector = closingSelector;
27 _this.add(subscribeToResult(_this, openings));
30 BufferToggleSubscriber.prototype._next = function (value) {
31 var contexts = this.contexts;
32 var len = contexts.length;
33 for (var i = 0; i < len; i++) {
34 contexts[i].buffer.push(value);
37 BufferToggleSubscriber.prototype._error = function (err) {
38 var contexts = this.contexts;
39 while (contexts.length > 0) {
40 var context_1 = contexts.shift();
41 context_1.subscription.unsubscribe();
42 context_1.buffer = null;
43 context_1.subscription = null;
46 _super.prototype._error.call(this, err);
48 BufferToggleSubscriber.prototype._complete = function () {
49 var contexts = this.contexts;
50 while (contexts.length > 0) {
51 var context_2 = contexts.shift();
52 this.destination.next(context_2.buffer);
53 context_2.subscription.unsubscribe();
54 context_2.buffer = null;
55 context_2.subscription = null;
58 _super.prototype._complete.call(this);
60 BufferToggleSubscriber.prototype.notifyNext = function (outerValue, innerValue) {
61 outerValue ? this.closeBuffer(outerValue) : this.openBuffer(innerValue);
63 BufferToggleSubscriber.prototype.notifyComplete = function (innerSub) {
64 this.closeBuffer(innerSub.context);
66 BufferToggleSubscriber.prototype.openBuffer = function (value) {
68 var closingSelector = this.closingSelector;
69 var closingNotifier = closingSelector.call(this, value);
70 if (closingNotifier) {
71 this.trySubscribe(closingNotifier);
78 BufferToggleSubscriber.prototype.closeBuffer = function (context) {
79 var contexts = this.contexts;
80 if (contexts && context) {
81 var buffer = context.buffer, subscription = context.subscription;
82 this.destination.next(buffer);
83 contexts.splice(contexts.indexOf(context), 1);
84 this.remove(subscription);
85 subscription.unsubscribe();
88 BufferToggleSubscriber.prototype.trySubscribe = function (closingNotifier) {
89 var contexts = this.contexts;
91 var subscription = new Subscription();
92 var context = { buffer: buffer, subscription: subscription };
93 contexts.push(context);
94 var innerSubscription = subscribeToResult(this, closingNotifier, context);
95 if (!innerSubscription || innerSubscription.closed) {
96 this.closeBuffer(context);
99 innerSubscription.context = context;
100 this.add(innerSubscription);
101 subscription.add(innerSubscription);
104 return BufferToggleSubscriber;
106 //# sourceMappingURL=bufferToggle.js.map