1 | "use strict";
|
---|
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);
|
---|
8 | }
|
---|
9 | return function (d, b) {
|
---|
10 | extendStatics(d, b);
|
---|
11 | function __() { this.constructor = d; }
|
---|
12 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
---|
13 | };
|
---|
14 | })();
|
---|
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));
|
---|
22 | };
|
---|
23 | }
|
---|
24 | exports.bufferToggle = bufferToggle;
|
---|
25 | var BufferToggleOperator = (function () {
|
---|
26 | function BufferToggleOperator(openings, closingSelector) {
|
---|
27 | this.openings = openings;
|
---|
28 | this.closingSelector = closingSelector;
|
---|
29 | }
|
---|
30 | BufferToggleOperator.prototype.call = function (subscriber, source) {
|
---|
31 | return source.subscribe(new BufferToggleSubscriber(subscriber, this.openings, this.closingSelector));
|
---|
32 | };
|
---|
33 | return BufferToggleOperator;
|
---|
34 | }());
|
---|
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.closingSelector = closingSelector;
|
---|
40 | _this.contexts = [];
|
---|
41 | _this.add(subscribeToResult_1.subscribeToResult(_this, openings));
|
---|
42 | return _this;
|
---|
43 | }
|
---|
44 | BufferToggleSubscriber.prototype._next = function (value) {
|
---|
45 | var contexts = this.contexts;
|
---|
46 | var len = contexts.length;
|
---|
47 | for (var i = 0; i < len; i++) {
|
---|
48 | contexts[i].buffer.push(value);
|
---|
49 | }
|
---|
50 | };
|
---|
51 | BufferToggleSubscriber.prototype._error = function (err) {
|
---|
52 | var contexts = this.contexts;
|
---|
53 | while (contexts.length > 0) {
|
---|
54 | var context_1 = contexts.shift();
|
---|
55 | context_1.subscription.unsubscribe();
|
---|
56 | context_1.buffer = null;
|
---|
57 | context_1.subscription = null;
|
---|
58 | }
|
---|
59 | this.contexts = null;
|
---|
60 | _super.prototype._error.call(this, err);
|
---|
61 | };
|
---|
62 | BufferToggleSubscriber.prototype._complete = function () {
|
---|
63 | var contexts = this.contexts;
|
---|
64 | while (contexts.length > 0) {
|
---|
65 | var context_2 = contexts.shift();
|
---|
66 | this.destination.next(context_2.buffer);
|
---|
67 | context_2.subscription.unsubscribe();
|
---|
68 | context_2.buffer = null;
|
---|
69 | context_2.subscription = null;
|
---|
70 | }
|
---|
71 | this.contexts = null;
|
---|
72 | _super.prototype._complete.call(this);
|
---|
73 | };
|
---|
74 | BufferToggleSubscriber.prototype.notifyNext = function (outerValue, innerValue) {
|
---|
75 | outerValue ? this.closeBuffer(outerValue) : this.openBuffer(innerValue);
|
---|
76 | };
|
---|
77 | BufferToggleSubscriber.prototype.notifyComplete = function (innerSub) {
|
---|
78 | this.closeBuffer(innerSub.context);
|
---|
79 | };
|
---|
80 | BufferToggleSubscriber.prototype.openBuffer = function (value) {
|
---|
81 | try {
|
---|
82 | var closingSelector = this.closingSelector;
|
---|
83 | var closingNotifier = closingSelector.call(this, value);
|
---|
84 | if (closingNotifier) {
|
---|
85 | this.trySubscribe(closingNotifier);
|
---|
86 | }
|
---|
87 | }
|
---|
88 | catch (err) {
|
---|
89 | this._error(err);
|
---|
90 | }
|
---|
91 | };
|
---|
92 | BufferToggleSubscriber.prototype.closeBuffer = function (context) {
|
---|
93 | var contexts = this.contexts;
|
---|
94 | if (contexts && context) {
|
---|
95 | var buffer = context.buffer, subscription = context.subscription;
|
---|
96 | this.destination.next(buffer);
|
---|
97 | contexts.splice(contexts.indexOf(context), 1);
|
---|
98 | this.remove(subscription);
|
---|
99 | subscription.unsubscribe();
|
---|
100 | }
|
---|
101 | };
|
---|
102 | BufferToggleSubscriber.prototype.trySubscribe = function (closingNotifier) {
|
---|
103 | var contexts = this.contexts;
|
---|
104 | var buffer = [];
|
---|
105 | var subscription = new Subscription_1.Subscription();
|
---|
106 | var context = { buffer: buffer, subscription: subscription };
|
---|
107 | contexts.push(context);
|
---|
108 | var innerSubscription = subscribeToResult_1.subscribeToResult(this, closingNotifier, context);
|
---|
109 | if (!innerSubscription || innerSubscription.closed) {
|
---|
110 | this.closeBuffer(context);
|
---|
111 | }
|
---|
112 | else {
|
---|
113 | innerSubscription.context = context;
|
---|
114 | this.add(innerSubscription);
|
---|
115 | subscription.add(innerSubscription);
|
---|
116 | }
|
---|
117 | };
|
---|
118 | return BufferToggleSubscriber;
|
---|
119 | }(OuterSubscriber_1.OuterSubscriber));
|
---|
120 | //# sourceMappingURL=bufferToggle.js.map |
---|