windowToggle.js
7 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"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 Subject_1 = require('../Subject');
var Subscription_1 = require('../Subscription');
var tryCatch_1 = require('../util/tryCatch');
var errorObject_1 = require('../util/errorObject');
var OuterSubscriber_1 = require('../OuterSubscriber');
var subscribeToResult_1 = require('../util/subscribeToResult');
/**
* Branch out the source Observable values as a nested Observable starting from
* an emission from `openings` and ending when the output of `closingSelector`
* emits.
*
* <span class="informal">It's like {@link bufferToggle}, but emits a nested
* Observable instead of an array.</span>
*
* <img src="./img/windowToggle.png" width="100%">
*
* Returns an Observable that emits windows of items it collects from the source
* Observable. The output Observable emits windows that contain those items
* emitted by the source Observable between the time when the `openings`
* Observable emits an item and when the Observable returned by
* `closingSelector` emits an item.
*
* @example <caption>Every other second, emit the click events from the next 500ms</caption>
* var clicks = Rx.Observable.fromEvent(document, 'click');
* var openings = Rx.Observable.interval(1000);
* var result = clicks.windowToggle(openings, i =>
* i % 2 ? Rx.Observable.interval(500) : Rx.Observable.empty()
* ).mergeAll();
* result.subscribe(x => console.log(x));
*
* @see {@link window}
* @see {@link windowCount}
* @see {@link windowTime}
* @see {@link windowWhen}
* @see {@link bufferToggle}
*
* @param {Observable<O>} openings An observable of notifications to start new
* windows.
* @param {function(value: O): Observable} closingSelector A function that takes
* the value emitted by the `openings` observable and returns an Observable,
* which, when it emits (either `next` or `complete`), signals that the
* associated window should complete.
* @return {Observable<Observable<T>>} An observable of windows, which in turn
* are Observables.
* @method windowToggle
* @owner Observable
*/
function windowToggle(openings, closingSelector) {
return this.lift(new WindowToggleOperator(openings, closingSelector));
}
exports.windowToggle = windowToggle;
var WindowToggleOperator = (function () {
function WindowToggleOperator(openings, closingSelector) {
this.openings = openings;
this.closingSelector = closingSelector;
}
WindowToggleOperator.prototype.call = function (subscriber, source) {
return source.subscribe(new WindowToggleSubscriber(subscriber, this.openings, this.closingSelector));
};
return WindowToggleOperator;
}());
/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
var WindowToggleSubscriber = (function (_super) {
__extends(WindowToggleSubscriber, _super);
function WindowToggleSubscriber(destination, openings, closingSelector) {
_super.call(this, destination);
this.openings = openings;
this.closingSelector = closingSelector;
this.contexts = [];
this.add(this.openSubscription = subscribeToResult_1.subscribeToResult(this, openings, openings));
}
WindowToggleSubscriber.prototype._next = function (value) {
var contexts = this.contexts;
if (contexts) {
var len = contexts.length;
for (var i = 0; i < len; i++) {
contexts[i].window.next(value);
}
}
};
WindowToggleSubscriber.prototype._error = function (err) {
var contexts = this.contexts;
this.contexts = null;
if (contexts) {
var len = contexts.length;
var index = -1;
while (++index < len) {
var context = contexts[index];
context.window.error(err);
context.subscription.unsubscribe();
}
}
_super.prototype._error.call(this, err);
};
WindowToggleSubscriber.prototype._complete = function () {
var contexts = this.contexts;
this.contexts = null;
if (contexts) {
var len = contexts.length;
var index = -1;
while (++index < len) {
var context = contexts[index];
context.window.complete();
context.subscription.unsubscribe();
}
}
_super.prototype._complete.call(this);
};
WindowToggleSubscriber.prototype._unsubscribe = function () {
var contexts = this.contexts;
this.contexts = null;
if (contexts) {
var len = contexts.length;
var index = -1;
while (++index < len) {
var context = contexts[index];
context.window.unsubscribe();
context.subscription.unsubscribe();
}
}
};
WindowToggleSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
if (outerValue === this.openings) {
var closingSelector = this.closingSelector;
var closingNotifier = tryCatch_1.tryCatch(closingSelector)(innerValue);
if (closingNotifier === errorObject_1.errorObject) {
return this.error(errorObject_1.errorObject.e);
}
else {
var window_1 = new Subject_1.Subject();
var subscription = new Subscription_1.Subscription();
var context = { window: window_1, subscription: subscription };
this.contexts.push(context);
var innerSubscription = subscribeToResult_1.subscribeToResult(this, closingNotifier, context);
if (innerSubscription.closed) {
this.closeWindow(this.contexts.length - 1);
}
else {
innerSubscription.context = context;
subscription.add(innerSubscription);
}
this.destination.next(window_1);
}
}
else {
this.closeWindow(this.contexts.indexOf(outerValue));
}
};
WindowToggleSubscriber.prototype.notifyError = function (err) {
this.error(err);
};
WindowToggleSubscriber.prototype.notifyComplete = function (inner) {
if (inner !== this.openSubscription) {
this.closeWindow(this.contexts.indexOf(inner.context));
}
};
WindowToggleSubscriber.prototype.closeWindow = function (index) {
if (index === -1) {
return;
}
var contexts = this.contexts;
var context = contexts[index];
var window = context.window, subscription = context.subscription;
contexts.splice(index, 1);
window.complete();
subscription.unsubscribe();
};
return WindowToggleSubscriber;
}(OuterSubscriber_1.OuterSubscriber));
//# sourceMappingURL=windowToggle.js.map