1 /** PURE_IMPORTS_START tslib,_Subject,_scheduler_async,_Subscriber,_util_isNumeric,_util_isScheduler PURE_IMPORTS_END */
2 import * as tslib_1 from "tslib";
3 import { Subject } from '../Subject';
4 import { async } from '../scheduler/async';
5 import { Subscriber } from '../Subscriber';
6 import { isNumeric } from '../util/isNumeric';
7 import { isScheduler } from '../util/isScheduler';
8 export function windowTime(windowTimeSpan) {
10 var windowCreationInterval = null;
11 var maxWindowSize = Number.POSITIVE_INFINITY;
12 if (isScheduler(arguments[3])) {
13 scheduler = arguments[3];
15 if (isScheduler(arguments[2])) {
16 scheduler = arguments[2];
18 else if (isNumeric(arguments[2])) {
19 maxWindowSize = Number(arguments[2]);
21 if (isScheduler(arguments[1])) {
22 scheduler = arguments[1];
24 else if (isNumeric(arguments[1])) {
25 windowCreationInterval = Number(arguments[1]);
27 return function windowTimeOperatorFunction(source) {
28 return source.lift(new WindowTimeOperator(windowTimeSpan, windowCreationInterval, maxWindowSize, scheduler));
31 var WindowTimeOperator = /*@__PURE__*/ (function () {
32 function WindowTimeOperator(windowTimeSpan, windowCreationInterval, maxWindowSize, scheduler) {
33 this.windowTimeSpan = windowTimeSpan;
34 this.windowCreationInterval = windowCreationInterval;
35 this.maxWindowSize = maxWindowSize;
36 this.scheduler = scheduler;
38 WindowTimeOperator.prototype.call = function (subscriber, source) {
39 return source.subscribe(new WindowTimeSubscriber(subscriber, this.windowTimeSpan, this.windowCreationInterval, this.maxWindowSize, this.scheduler));
41 return WindowTimeOperator;
43 var CountedSubject = /*@__PURE__*/ (function (_super) {
44 tslib_1.__extends(CountedSubject, _super);
45 function CountedSubject() {
46 var _this = _super !== null && _super.apply(this, arguments) || this;
47 _this._numberOfNextedValues = 0;
50 CountedSubject.prototype.next = function (value) {
51 this._numberOfNextedValues++;
52 _super.prototype.next.call(this, value);
54 Object.defineProperty(CountedSubject.prototype, "numberOfNextedValues", {
56 return this._numberOfNextedValues;
61 return CountedSubject;
63 var WindowTimeSubscriber = /*@__PURE__*/ (function (_super) {
64 tslib_1.__extends(WindowTimeSubscriber, _super);
65 function WindowTimeSubscriber(destination, windowTimeSpan, windowCreationInterval, maxWindowSize, scheduler) {
66 var _this = _super.call(this, destination) || this;
67 _this.destination = destination;
68 _this.windowTimeSpan = windowTimeSpan;
69 _this.windowCreationInterval = windowCreationInterval;
70 _this.maxWindowSize = maxWindowSize;
71 _this.scheduler = scheduler;
73 var window = _this.openWindow();
74 if (windowCreationInterval !== null && windowCreationInterval >= 0) {
75 var closeState = { subscriber: _this, window: window, context: null };
76 var creationState = { windowTimeSpan: windowTimeSpan, windowCreationInterval: windowCreationInterval, subscriber: _this, scheduler: scheduler };
77 _this.add(scheduler.schedule(dispatchWindowClose, windowTimeSpan, closeState));
78 _this.add(scheduler.schedule(dispatchWindowCreation, windowCreationInterval, creationState));
81 var timeSpanOnlyState = { subscriber: _this, window: window, windowTimeSpan: windowTimeSpan };
82 _this.add(scheduler.schedule(dispatchWindowTimeSpanOnly, windowTimeSpan, timeSpanOnlyState));
86 WindowTimeSubscriber.prototype._next = function (value) {
87 var windows = this.windows;
88 var len = windows.length;
89 for (var i = 0; i < len; i++) {
90 var window_1 = windows[i];
91 if (!window_1.closed) {
93 if (window_1.numberOfNextedValues >= this.maxWindowSize) {
94 this.closeWindow(window_1);
99 WindowTimeSubscriber.prototype._error = function (err) {
100 var windows = this.windows;
101 while (windows.length > 0) {
102 windows.shift().error(err);
104 this.destination.error(err);
106 WindowTimeSubscriber.prototype._complete = function () {
107 var windows = this.windows;
108 while (windows.length > 0) {
109 var window_2 = windows.shift();
110 if (!window_2.closed) {
114 this.destination.complete();
116 WindowTimeSubscriber.prototype.openWindow = function () {
117 var window = new CountedSubject();
118 this.windows.push(window);
119 var destination = this.destination;
120 destination.next(window);
123 WindowTimeSubscriber.prototype.closeWindow = function (window) {
125 var windows = this.windows;
126 windows.splice(windows.indexOf(window), 1);
128 return WindowTimeSubscriber;
130 function dispatchWindowTimeSpanOnly(state) {
131 var subscriber = state.subscriber, windowTimeSpan = state.windowTimeSpan, window = state.window;
133 subscriber.closeWindow(window);
135 state.window = subscriber.openWindow();
136 this.schedule(state, windowTimeSpan);
138 function dispatchWindowCreation(state) {
139 var windowTimeSpan = state.windowTimeSpan, subscriber = state.subscriber, scheduler = state.scheduler, windowCreationInterval = state.windowCreationInterval;
140 var window = subscriber.openWindow();
142 var context = { action: action, subscription: null };
143 var timeSpanState = { subscriber: subscriber, window: window, context: context };
144 context.subscription = scheduler.schedule(dispatchWindowClose, windowTimeSpan, timeSpanState);
145 action.add(context.subscription);
146 action.schedule(state, windowCreationInterval);
148 function dispatchWindowClose(state) {
149 var subscriber = state.subscriber, window = state.window, context = state.context;
150 if (context && context.action && context.subscription) {
151 context.action.remove(context.subscription);
153 subscriber.closeWindow(window);
155 //# sourceMappingURL=windowTime.js.map