94 lines
3.4 KiB
JavaScript
94 lines
3.4 KiB
JavaScript
|
/** PURE_IMPORTS_START tslib,_scheduler_async,_util_isDate,_Subscriber,_Notification PURE_IMPORTS_END */
|
||
|
import * as tslib_1 from "tslib";
|
||
|
import { async } from '../scheduler/async';
|
||
|
import { isDate } from '../util/isDate';
|
||
|
import { Subscriber } from '../Subscriber';
|
||
|
import { Notification } from '../Notification';
|
||
|
export function delay(delay, scheduler) {
|
||
|
if (scheduler === void 0) {
|
||
|
scheduler = async;
|
||
|
}
|
||
|
var absoluteDelay = isDate(delay);
|
||
|
var delayFor = absoluteDelay ? (+delay - scheduler.now()) : Math.abs(delay);
|
||
|
return function (source) { return source.lift(new DelayOperator(delayFor, scheduler)); };
|
||
|
}
|
||
|
var DelayOperator = /*@__PURE__*/ (function () {
|
||
|
function DelayOperator(delay, scheduler) {
|
||
|
this.delay = delay;
|
||
|
this.scheduler = scheduler;
|
||
|
}
|
||
|
DelayOperator.prototype.call = function (subscriber, source) {
|
||
|
return source.subscribe(new DelaySubscriber(subscriber, this.delay, this.scheduler));
|
||
|
};
|
||
|
return DelayOperator;
|
||
|
}());
|
||
|
var DelaySubscriber = /*@__PURE__*/ (function (_super) {
|
||
|
tslib_1.__extends(DelaySubscriber, _super);
|
||
|
function DelaySubscriber(destination, delay, scheduler) {
|
||
|
var _this = _super.call(this, destination) || this;
|
||
|
_this.delay = delay;
|
||
|
_this.scheduler = scheduler;
|
||
|
_this.queue = [];
|
||
|
_this.active = false;
|
||
|
_this.errored = false;
|
||
|
return _this;
|
||
|
}
|
||
|
DelaySubscriber.dispatch = function (state) {
|
||
|
var source = state.source;
|
||
|
var queue = source.queue;
|
||
|
var scheduler = state.scheduler;
|
||
|
var destination = state.destination;
|
||
|
while (queue.length > 0 && (queue[0].time - scheduler.now()) <= 0) {
|
||
|
queue.shift().notification.observe(destination);
|
||
|
}
|
||
|
if (queue.length > 0) {
|
||
|
var delay_1 = Math.max(0, queue[0].time - scheduler.now());
|
||
|
this.schedule(state, delay_1);
|
||
|
}
|
||
|
else {
|
||
|
this.unsubscribe();
|
||
|
source.active = false;
|
||
|
}
|
||
|
};
|
||
|
DelaySubscriber.prototype._schedule = function (scheduler) {
|
||
|
this.active = true;
|
||
|
var destination = this.destination;
|
||
|
destination.add(scheduler.schedule(DelaySubscriber.dispatch, this.delay, {
|
||
|
source: this, destination: this.destination, scheduler: scheduler
|
||
|
}));
|
||
|
};
|
||
|
DelaySubscriber.prototype.scheduleNotification = function (notification) {
|
||
|
if (this.errored === true) {
|
||
|
return;
|
||
|
}
|
||
|
var scheduler = this.scheduler;
|
||
|
var message = new DelayMessage(scheduler.now() + this.delay, notification);
|
||
|
this.queue.push(message);
|
||
|
if (this.active === false) {
|
||
|
this._schedule(scheduler);
|
||
|
}
|
||
|
};
|
||
|
DelaySubscriber.prototype._next = function (value) {
|
||
|
this.scheduleNotification(Notification.createNext(value));
|
||
|
};
|
||
|
DelaySubscriber.prototype._error = function (err) {
|
||
|
this.errored = true;
|
||
|
this.queue = [];
|
||
|
this.destination.error(err);
|
||
|
this.unsubscribe();
|
||
|
};
|
||
|
DelaySubscriber.prototype._complete = function () {
|
||
|
this.scheduleNotification(Notification.createComplete());
|
||
|
this.unsubscribe();
|
||
|
};
|
||
|
return DelaySubscriber;
|
||
|
}(Subscriber));
|
||
|
var DelayMessage = /*@__PURE__*/ (function () {
|
||
|
function DelayMessage(time, notification) {
|
||
|
this.time = time;
|
||
|
this.notification = notification;
|
||
|
}
|
||
|
return DelayMessage;
|
||
|
}());
|
||
|
//# sourceMappingURL=delay.js.map
|