bufferTime.js
6.85 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
"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 async_1 = require("../scheduler/async");
var Subscriber_1 = require("../Subscriber");
var isScheduler_1 = require("../util/isScheduler");
function bufferTime(bufferTimeSpan) {
var length = arguments.length;
var scheduler = async_1.async;
if (isScheduler_1.isScheduler(arguments[arguments.length - 1])) {
scheduler = arguments[arguments.length - 1];
length--;
}
var bufferCreationInterval = null;
if (length >= 2) {
bufferCreationInterval = arguments[1];
}
var maxBufferSize = Number.POSITIVE_INFINITY;
if (length >= 3) {
maxBufferSize = arguments[2];
}
return function bufferTimeOperatorFunction(source) {
return source.lift(new BufferTimeOperator(bufferTimeSpan, bufferCreationInterval, maxBufferSize, scheduler));
};
}
exports.bufferTime = bufferTime;
var BufferTimeOperator = (function () {
function BufferTimeOperator(bufferTimeSpan, bufferCreationInterval, maxBufferSize, scheduler) {
this.bufferTimeSpan = bufferTimeSpan;
this.bufferCreationInterval = bufferCreationInterval;
this.maxBufferSize = maxBufferSize;
this.scheduler = scheduler;
}
BufferTimeOperator.prototype.call = function (subscriber, source) {
return source.subscribe(new BufferTimeSubscriber(subscriber, this.bufferTimeSpan, this.bufferCreationInterval, this.maxBufferSize, this.scheduler));
};
return BufferTimeOperator;
}());
var Context = (function () {
function Context() {
this.buffer = [];
}
return Context;
}());
var BufferTimeSubscriber = (function (_super) {
__extends(BufferTimeSubscriber, _super);
function BufferTimeSubscriber(destination, bufferTimeSpan, bufferCreationInterval, maxBufferSize, scheduler) {
var _this = _super.call(this, destination) || this;
_this.bufferTimeSpan = bufferTimeSpan;
_this.bufferCreationInterval = bufferCreationInterval;
_this.maxBufferSize = maxBufferSize;
_this.scheduler = scheduler;
_this.contexts = [];
var context = _this.openContext();
_this.timespanOnly = bufferCreationInterval == null || bufferCreationInterval < 0;
if (_this.timespanOnly) {
var timeSpanOnlyState = { subscriber: _this, context: context, bufferTimeSpan: bufferTimeSpan };
_this.add(context.closeAction = scheduler.schedule(dispatchBufferTimeSpanOnly, bufferTimeSpan, timeSpanOnlyState));
}
else {
var closeState = { subscriber: _this, context: context };
var creationState = { bufferTimeSpan: bufferTimeSpan, bufferCreationInterval: bufferCreationInterval, subscriber: _this, scheduler: scheduler };
_this.add(context.closeAction = scheduler.schedule(dispatchBufferClose, bufferTimeSpan, closeState));
_this.add(scheduler.schedule(dispatchBufferCreation, bufferCreationInterval, creationState));
}
return _this;
}
BufferTimeSubscriber.prototype._next = function (value) {
var contexts = this.contexts;
var len = contexts.length;
var filledBufferContext;
for (var i = 0; i < len; i++) {
var context_1 = contexts[i];
var buffer = context_1.buffer;
buffer.push(value);
if (buffer.length == this.maxBufferSize) {
filledBufferContext = context_1;
}
}
if (filledBufferContext) {
this.onBufferFull(filledBufferContext);
}
};
BufferTimeSubscriber.prototype._error = function (err) {
this.contexts.length = 0;
_super.prototype._error.call(this, err);
};
BufferTimeSubscriber.prototype._complete = function () {
var _a = this, contexts = _a.contexts, destination = _a.destination;
while (contexts.length > 0) {
var context_2 = contexts.shift();
destination.next(context_2.buffer);
}
_super.prototype._complete.call(this);
};
BufferTimeSubscriber.prototype._unsubscribe = function () {
this.contexts = null;
};
BufferTimeSubscriber.prototype.onBufferFull = function (context) {
this.closeContext(context);
var closeAction = context.closeAction;
closeAction.unsubscribe();
this.remove(closeAction);
if (!this.closed && this.timespanOnly) {
context = this.openContext();
var bufferTimeSpan = this.bufferTimeSpan;
var timeSpanOnlyState = { subscriber: this, context: context, bufferTimeSpan: bufferTimeSpan };
this.add(context.closeAction = this.scheduler.schedule(dispatchBufferTimeSpanOnly, bufferTimeSpan, timeSpanOnlyState));
}
};
BufferTimeSubscriber.prototype.openContext = function () {
var context = new Context();
this.contexts.push(context);
return context;
};
BufferTimeSubscriber.prototype.closeContext = function (context) {
this.destination.next(context.buffer);
var contexts = this.contexts;
var spliceIndex = contexts ? contexts.indexOf(context) : -1;
if (spliceIndex >= 0) {
contexts.splice(contexts.indexOf(context), 1);
}
};
return BufferTimeSubscriber;
}(Subscriber_1.Subscriber));
function dispatchBufferTimeSpanOnly(state) {
var subscriber = state.subscriber;
var prevContext = state.context;
if (prevContext) {
subscriber.closeContext(prevContext);
}
if (!subscriber.closed) {
state.context = subscriber.openContext();
state.context.closeAction = this.schedule(state, state.bufferTimeSpan);
}
}
function dispatchBufferCreation(state) {
var bufferCreationInterval = state.bufferCreationInterval, bufferTimeSpan = state.bufferTimeSpan, subscriber = state.subscriber, scheduler = state.scheduler;
var context = subscriber.openContext();
var action = this;
if (!subscriber.closed) {
subscriber.add(context.closeAction = scheduler.schedule(dispatchBufferClose, bufferTimeSpan, { subscriber: subscriber, context: context }));
action.schedule(state, bufferCreationInterval);
}
}
function dispatchBufferClose(arg) {
var subscriber = arg.subscriber, context = arg.context;
subscriber.closeContext(context);
}
//# sourceMappingURL=bufferTime.js.map