-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbufferToggle.js
More file actions
145 lines (145 loc) · 5.73 KB
/
Copy pathbufferToggle.js
File metadata and controls
145 lines (145 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var Subscriber_1 = require('../Subscriber');
var Subscription_1 = require('../Subscription');
var tryCatch_1 = require('../util/tryCatch');
var errorObject_1 = require('../util/errorObject');
/**
* Buffers values from the source by opening the buffer via signals from an
* Observable provided to `openings`, and closing and sending the buffers when
* an Observable returned by the `closingSelector` emits.
*
* <img src="./img/bufferToggle.png" width="100%">
*
* @param {Observable<O>} openings An observable of notifications to start new
* buffers.
* @param {Function} closingSelector a function that takes the value emitted by
* the `openings` observable and returns an Observable, which, when it emits,
* signals that the associated buffer should be emitted and cleared.
* @returns {Observable<T[]>} an observable of arrays of buffered values.
*/
function bufferToggle(openings, closingSelector) {
return this.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) {
return new BufferToggleSubscriber(subscriber, this.openings, this.closingSelector);
};
return BufferToggleOperator;
}());
var BufferToggleSubscriber = (function (_super) {
__extends(BufferToggleSubscriber, _super);
function BufferToggleSubscriber(destination, openings, closingSelector) {
_super.call(this, destination);
this.openings = openings;
this.closingSelector = closingSelector;
this.contexts = [];
this.add(this.openings.subscribe(new BufferToggleOpeningsSubscriber(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 = contexts.shift();
context.subscription.unsubscribe();
context.buffer = null;
context.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 = contexts.shift();
this.destination.next(context.buffer);
context.subscription.unsubscribe();
context.buffer = null;
context.subscription = null;
}
this.contexts = null;
_super.prototype._complete.call(this);
};
BufferToggleSubscriber.prototype.openBuffer = function (value) {
var closingSelector = this.closingSelector;
var contexts = this.contexts;
var closingNotifier = tryCatch_1.tryCatch(closingSelector)(value);
if (closingNotifier === errorObject_1.errorObject) {
this._error(errorObject_1.errorObject.e);
}
else {
var context = {
buffer: [],
subscription: new Subscription_1.Subscription()
};
contexts.push(context);
var subscriber = new BufferToggleClosingsSubscriber(this, context);
var subscription = closingNotifier.subscribe(subscriber);
context.subscription.add(subscription);
this.add(subscription);
}
};
BufferToggleSubscriber.prototype.closeBuffer = function (context) {
var contexts = this.contexts;
if (contexts === null) {
return;
}
var buffer = context.buffer, subscription = context.subscription;
this.destination.next(buffer);
contexts.splice(contexts.indexOf(context), 1);
this.remove(subscription);
subscription.unsubscribe();
};
return BufferToggleSubscriber;
}(Subscriber_1.Subscriber));
var BufferToggleOpeningsSubscriber = (function (_super) {
__extends(BufferToggleOpeningsSubscriber, _super);
function BufferToggleOpeningsSubscriber(parent) {
_super.call(this, null);
this.parent = parent;
}
BufferToggleOpeningsSubscriber.prototype._next = function (value) {
this.parent.openBuffer(value);
};
BufferToggleOpeningsSubscriber.prototype._error = function (err) {
this.parent.error(err);
};
BufferToggleOpeningsSubscriber.prototype._complete = function () {
// noop
};
return BufferToggleOpeningsSubscriber;
}(Subscriber_1.Subscriber));
var BufferToggleClosingsSubscriber = (function (_super) {
__extends(BufferToggleClosingsSubscriber, _super);
function BufferToggleClosingsSubscriber(parent, context) {
_super.call(this, null);
this.parent = parent;
this.context = context;
}
BufferToggleClosingsSubscriber.prototype._next = function () {
this.parent.closeBuffer(this.context);
};
BufferToggleClosingsSubscriber.prototype._error = function (err) {
this.parent.error(err);
};
BufferToggleClosingsSubscriber.prototype._complete = function () {
this.parent.closeBuffer(this.context);
};
return BufferToggleClosingsSubscriber;
}(Subscriber_1.Subscriber));
//# sourceMappingURL=bufferToggle.js.map