windowTime.js
7.31 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
"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 async_1 = require('../scheduler/async');
var Subscriber_1 = require('../Subscriber');
/**
* Branch out the source Observable values as a nested Observable periodically
* in time.
*
* <span class="informal">It's like {@link bufferTime}, but emits a nested
* Observable instead of an array.</span>
*
* <img src="./img/windowTime.png" width="100%">
*
* Returns an Observable that emits windows of items it collects from the source
* Observable. The output Observable starts a new window periodically, as
* determined by the `windowCreationInterval` argument. It emits each window
* after a fixed timespan, specified by the `windowTimeSpan` argument. When the
* source Observable completes or encounters an error, the output Observable
* emits the current window and propagates the notification from the source
* Observable. If `windowCreationInterval` is not provided, the output
* Observable starts a new window when the previous window of duration
* `windowTimeSpan` completes.
*
* @example <caption>In every window of 1 second each, emit at most 2 click events</caption>
* var clicks = Rx.Observable.fromEvent(document, 'click');
* var result = clicks.windowTime(1000)
* .map(win => win.take(2)) // each window has at most 2 emissions
* .mergeAll(); // flatten the Observable-of-Observables
* result.subscribe(x => console.log(x));
*
* @example <caption>Every 5 seconds start a window 1 second long, and emit at most 2 click events per window</caption>
* var clicks = Rx.Observable.fromEvent(document, 'click');
* var result = clicks.windowTime(1000, 5000)
* .map(win => win.take(2)) // each window has at most 2 emissions
* .mergeAll(); // flatten the Observable-of-Observables
* result.subscribe(x => console.log(x));
*
* @see {@link window}
* @see {@link windowCount}
* @see {@link windowToggle}
* @see {@link windowWhen}
* @see {@link bufferTime}
*
* @param {number} windowTimeSpan The amount of time to fill each window.
* @param {number} [windowCreationInterval] The interval at which to start new
* windows.
* @param {Scheduler} [scheduler=async] The scheduler on which to schedule the
* intervals that determine window boundaries.
* @return {Observable<Observable<T>>} An observable of windows, which in turn
* are Observables.
* @method windowTime
* @owner Observable
*/
function windowTime(windowTimeSpan, windowCreationInterval, scheduler) {
if (windowCreationInterval === void 0) { windowCreationInterval = null; }
if (scheduler === void 0) { scheduler = async_1.async; }
return this.lift(new WindowTimeOperator(windowTimeSpan, windowCreationInterval, scheduler));
}
exports.windowTime = windowTime;
var WindowTimeOperator = (function () {
function WindowTimeOperator(windowTimeSpan, windowCreationInterval, scheduler) {
this.windowTimeSpan = windowTimeSpan;
this.windowCreationInterval = windowCreationInterval;
this.scheduler = scheduler;
}
WindowTimeOperator.prototype.call = function (subscriber, source) {
return source.subscribe(new WindowTimeSubscriber(subscriber, this.windowTimeSpan, this.windowCreationInterval, this.scheduler));
};
return WindowTimeOperator;
}());
/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
var WindowTimeSubscriber = (function (_super) {
__extends(WindowTimeSubscriber, _super);
function WindowTimeSubscriber(destination, windowTimeSpan, windowCreationInterval, scheduler) {
_super.call(this, destination);
this.destination = destination;
this.windowTimeSpan = windowTimeSpan;
this.windowCreationInterval = windowCreationInterval;
this.scheduler = scheduler;
this.windows = [];
if (windowCreationInterval !== null && windowCreationInterval >= 0) {
var window_1 = this.openWindow();
var closeState = { subscriber: this, window: window_1, context: null };
var creationState = { windowTimeSpan: windowTimeSpan, windowCreationInterval: windowCreationInterval, subscriber: this, scheduler: scheduler };
this.add(scheduler.schedule(dispatchWindowClose, windowTimeSpan, closeState));
this.add(scheduler.schedule(dispatchWindowCreation, windowCreationInterval, creationState));
}
else {
var window_2 = this.openWindow();
var timeSpanOnlyState = { subscriber: this, window: window_2, windowTimeSpan: windowTimeSpan };
this.add(scheduler.schedule(dispatchWindowTimeSpanOnly, windowTimeSpan, timeSpanOnlyState));
}
}
WindowTimeSubscriber.prototype._next = function (value) {
var windows = this.windows;
var len = windows.length;
for (var i = 0; i < len; i++) {
var window_3 = windows[i];
if (!window_3.closed) {
window_3.next(value);
}
}
};
WindowTimeSubscriber.prototype._error = function (err) {
var windows = this.windows;
while (windows.length > 0) {
windows.shift().error(err);
}
this.destination.error(err);
};
WindowTimeSubscriber.prototype._complete = function () {
var windows = this.windows;
while (windows.length > 0) {
var window_4 = windows.shift();
if (!window_4.closed) {
window_4.complete();
}
}
this.destination.complete();
};
WindowTimeSubscriber.prototype.openWindow = function () {
var window = new Subject_1.Subject();
this.windows.push(window);
var destination = this.destination;
destination.next(window);
return window;
};
WindowTimeSubscriber.prototype.closeWindow = function (window) {
window.complete();
var windows = this.windows;
windows.splice(windows.indexOf(window), 1);
};
return WindowTimeSubscriber;
}(Subscriber_1.Subscriber));
function dispatchWindowTimeSpanOnly(state) {
var subscriber = state.subscriber, windowTimeSpan = state.windowTimeSpan, window = state.window;
if (window) {
window.complete();
}
state.window = subscriber.openWindow();
this.schedule(state, windowTimeSpan);
}
function dispatchWindowCreation(state) {
var windowTimeSpan = state.windowTimeSpan, subscriber = state.subscriber, scheduler = state.scheduler, windowCreationInterval = state.windowCreationInterval;
var window = subscriber.openWindow();
var action = this;
var context = { action: action, subscription: null };
var timeSpanState = { subscriber: subscriber, window: window, context: context };
context.subscription = scheduler.schedule(dispatchWindowClose, windowTimeSpan, timeSpanState);
action.add(context.subscription);
action.schedule(state, windowCreationInterval);
}
function dispatchWindowClose(arg) {
var subscriber = arg.subscriber, window = arg.window, context = arg.context;
if (context && context.action && context.subscription) {
context.action.remove(context.subscription);
}
subscriber.closeWindow(window);
}
//# sourceMappingURL=windowTime.js.map