2 var __extends = (this && this.__extends) || (function () {
3 var extendStatics = function (d, b) {
4 extendStatics = Object.setPrototypeOf ||
5 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7 return extendStatics(d, b);
9 return function (d, b) {
11 function __() { this.constructor = d; }
12 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15 Object.defineProperty(exports, "__esModule", { value: true });
16 var Subscription_1 = require("../Subscription");
17 var subscribeToResult_1 = require("../util/subscribeToResult");
18 var OuterSubscriber_1 = require("../OuterSubscriber");
19 function bufferToggle(openings, closingSelector) {
20 return function bufferToggleOperatorFunction(source) {
21 return source.lift(new BufferToggleOperator(openings, closingSelector));
24 exports.bufferToggle = bufferToggle;
25 var BufferToggleOperator = (function () {
26 function BufferToggleOperator(openings, closingSelector) {
27 this.openings = openings;
28 this.closingSelector = closingSelector;
30 BufferToggleOperator.prototype.call = function (subscriber, source) {
31 return source.subscribe(new BufferToggleSubscriber(subscriber, this.openings, this.closingSelector));
33 return BufferToggleOperator;
35 var BufferToggleSubscriber = (function (_super) {
36 __extends(BufferToggleSubscriber, _super);
37 function BufferToggleSubscriber(destination, openings, closingSelector) {
38 var _this = _super.call(this, destination) || this;
39 _this.openings = openings;
40 _this.closingSelector = closingSelector;
42 _this.add(subscribeToResult_1.subscribeToResult(_this, openings));
45 BufferToggleSubscriber.prototype._next = function (value) {
46 var contexts = this.contexts;
47 var len = contexts.length;
48 for (var i = 0; i < len; i++) {
49 contexts[i].buffer.push(value);
52 BufferToggleSubscriber.prototype._error = function (err) {
53 var contexts = this.contexts;
54 while (contexts.length > 0) {
55 var context_1 = contexts.shift();
56 context_1.subscription.unsubscribe();
57 context_1.buffer = null;
58 context_1.subscription = null;
61 _super.prototype._error.call(this, err);
63 BufferToggleSubscriber.prototype._complete = function () {
64 var contexts = this.contexts;
65 while (contexts.length > 0) {
66 var context_2 = contexts.shift();
67 this.destination.next(context_2.buffer);
68 context_2.subscription.unsubscribe();
69 context_2.buffer = null;
70 context_2.subscription = null;
73 _super.prototype._complete.call(this);
75 BufferToggleSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
76 outerValue ? this.closeBuffer(outerValue) : this.openBuffer(innerValue);
78 BufferToggleSubscriber.prototype.notifyComplete = function (innerSub) {
79 this.closeBuffer(innerSub.context);
81 BufferToggleSubscriber.prototype.openBuffer = function (value) {
83 var closingSelector = this.closingSelector;
84 var closingNotifier = closingSelector.call(this, value);
85 if (closingNotifier) {
86 this.trySubscribe(closingNotifier);
93 BufferToggleSubscriber.prototype.closeBuffer = function (context) {
94 var contexts = this.contexts;
95 if (contexts && context) {
96 var buffer = context.buffer, subscription = context.subscription;
97 this.destination.next(buffer);
98 contexts.splice(contexts.indexOf(context), 1);
99 this.remove(subscription);
100 subscription.unsubscribe();
103 BufferToggleSubscriber.prototype.trySubscribe = function (closingNotifier) {
104 var contexts = this.contexts;
106 var subscription = new Subscription_1.Subscription();
107 var context = { buffer: buffer, subscription: subscription };
108 contexts.push(context);
109 var innerSubscription = subscribeToResult_1.subscribeToResult(this, closingNotifier, context);
110 if (!innerSubscription || innerSubscription.closed) {
111 this.closeBuffer(context);
114 innerSubscription.context = context;
115 this.add(innerSubscription);
116 subscription.add(innerSubscription);
119 return BufferToggleSubscriber;
120 }(OuterSubscriber_1.OuterSubscriber));
121 //# sourceMappingURL=bufferToggle.js.map