windowTime.js
6.97 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 () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
}
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var Subject_1 = require("../Subject");
var async_1 = require("../scheduler/async");
var Subscriber_1 = require("../Subscriber");
var isNumeric_1 = require("../util/isNumeric");
var isScheduler_1 = require("../util/isScheduler");
function windowTime(windowTimeSpan) {
var scheduler = async_1.async;
var windowCreationInterval = null;
var maxWindowSize = Number.POSITIVE_INFINITY;
if (isScheduler_1.isScheduler(arguments[3])) {
scheduler = arguments[3];
}
if (isScheduler_1.isScheduler(arguments[2])) {
scheduler = arguments[2];
}
else if (isNumeric_1.isNumeric(arguments[2])) {
maxWindowSize = Number(arguments[2]);
}
if (isScheduler_1.isScheduler(arguments[1])) {
scheduler = arguments[1];
}
else if (isNumeric_1.isNumeric(arguments[1])) {
windowCreationInterval = Number(arguments[1]);
}
return function windowTimeOperatorFunction(source) {
return source.lift(new WindowTimeOperator(windowTimeSpan, windowCreationInterval, maxWindowSize, scheduler));
};
}
exports.windowTime = windowTime;
var WindowTimeOperator = (function () {
function WindowTimeOperator(windowTimeSpan, windowCreationInterval, maxWindowSize, scheduler) {
this.windowTimeSpan = windowTimeSpan;
this.windowCreationInterval = windowCreationInterval;
this.maxWindowSize = maxWindowSize;
this.scheduler = scheduler;
}
WindowTimeOperator.prototype.call = function (subscriber, source) {
return source.subscribe(new WindowTimeSubscriber(subscriber, this.windowTimeSpan, this.windowCreationInterval, this.maxWindowSize, this.scheduler));
};
return WindowTimeOperator;
}());
var CountedSubject = (function (_super) {
__extends(CountedSubject, _super);
function CountedSubject() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this._numberOfNextedValues = 0;
return _this;
}
CountedSubject.prototype.next = function (value) {
this._numberOfNextedValues++;
_super.prototype.next.call(this, value);
};
Object.defineProperty(CountedSubject.prototype, "numberOfNextedValues", {
get: function () {
return this._numberOfNextedValues;
},
enumerable: true,
configurable: true
});
return CountedSubject;
}(Subject_1.Subject));
var WindowTimeSubscriber = (function (_super) {
__extends(WindowTimeSubscriber, _super);
function WindowTimeSubscriber(destination, windowTimeSpan, windowCreationInterval, maxWindowSize, scheduler) {
var _this = _super.call(this, destination) || this;
_this.destination = destination;
_this.windowTimeSpan = windowTimeSpan;
_this.windowCreationInterval = windowCreationInterval;
_this.maxWindowSize = maxWindowSize;
_this.scheduler = scheduler;
_this.windows = [];
var window = _this.openWindow();
if (windowCreationInterval !== null && windowCreationInterval >= 0) {
var closeState = { subscriber: _this, window: window, 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 timeSpanOnlyState = { subscriber: _this, window: window, windowTimeSpan: windowTimeSpan };
_this.add(scheduler.schedule(dispatchWindowTimeSpanOnly, windowTimeSpan, timeSpanOnlyState));
}
return _this;
}
WindowTimeSubscriber.prototype._next = function (value) {
var windows = this.windows;
var len = windows.length;
for (var i = 0; i < len; i++) {
var window_1 = windows[i];
if (!window_1.closed) {
window_1.next(value);
if (window_1.numberOfNextedValues >= this.maxWindowSize) {
this.closeWindow(window_1);
}
}
}
};
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_2 = windows.shift();
if (!window_2.closed) {
window_2.complete();
}
}
this.destination.complete();
};
WindowTimeSubscriber.prototype.openWindow = function () {
var window = new CountedSubject();
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) {
subscriber.closeWindow(window);
}
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(state) {
var subscriber = state.subscriber, window = state.window, context = state.context;
if (context && context.action && context.subscription) {
context.action.remove(context.subscription);
}
subscriber.closeWindow(window);
}
//# sourceMappingURL=windowTime.js.map