1 | /** PURE_IMPORTS_START tslib,_scheduler_async,_util_isDate,_Subscriber,_Notification PURE_IMPORTS_END */
|
---|
2 | import * as tslib_1 from "tslib";
|
---|
3 | import { async } from '../scheduler/async';
|
---|
4 | import { isDate } from '../util/isDate';
|
---|
5 | import { Subscriber } from '../Subscriber';
|
---|
6 | import { Notification } from '../Notification';
|
---|
7 | export function delay(delay, scheduler) {
|
---|
8 | if (scheduler === void 0) {
|
---|
9 | scheduler = async;
|
---|
10 | }
|
---|
11 | var absoluteDelay = isDate(delay);
|
---|
12 | var delayFor = absoluteDelay ? (+delay - scheduler.now()) : Math.abs(delay);
|
---|
13 | return function (source) { return source.lift(new DelayOperator(delayFor, scheduler)); };
|
---|
14 | }
|
---|
15 | var DelayOperator = /*@__PURE__*/ (function () {
|
---|
16 | function DelayOperator(delay, scheduler) {
|
---|
17 | this.delay = delay;
|
---|
18 | this.scheduler = scheduler;
|
---|
19 | }
|
---|
20 | DelayOperator.prototype.call = function (subscriber, source) {
|
---|
21 | return source.subscribe(new DelaySubscriber(subscriber, this.delay, this.scheduler));
|
---|
22 | };
|
---|
23 | return DelayOperator;
|
---|
24 | }());
|
---|
25 | var DelaySubscriber = /*@__PURE__*/ (function (_super) {
|
---|
26 | tslib_1.__extends(DelaySubscriber, _super);
|
---|
27 | function DelaySubscriber(destination, delay, scheduler) {
|
---|
28 | var _this = _super.call(this, destination) || this;
|
---|
29 | _this.delay = delay;
|
---|
30 | _this.scheduler = scheduler;
|
---|
31 | _this.queue = [];
|
---|
32 | _this.active = false;
|
---|
33 | _this.errored = false;
|
---|
34 | return _this;
|
---|
35 | }
|
---|
36 | DelaySubscriber.dispatch = function (state) {
|
---|
37 | var source = state.source;
|
---|
38 | var queue = source.queue;
|
---|
39 | var scheduler = state.scheduler;
|
---|
40 | var destination = state.destination;
|
---|
41 | while (queue.length > 0 && (queue[0].time - scheduler.now()) <= 0) {
|
---|
42 | queue.shift().notification.observe(destination);
|
---|
43 | }
|
---|
44 | if (queue.length > 0) {
|
---|
45 | var delay_1 = Math.max(0, queue[0].time - scheduler.now());
|
---|
46 | this.schedule(state, delay_1);
|
---|
47 | }
|
---|
48 | else {
|
---|
49 | this.unsubscribe();
|
---|
50 | source.active = false;
|
---|
51 | }
|
---|
52 | };
|
---|
53 | DelaySubscriber.prototype._schedule = function (scheduler) {
|
---|
54 | this.active = true;
|
---|
55 | var destination = this.destination;
|
---|
56 | destination.add(scheduler.schedule(DelaySubscriber.dispatch, this.delay, {
|
---|
57 | source: this, destination: this.destination, scheduler: scheduler
|
---|
58 | }));
|
---|
59 | };
|
---|
60 | DelaySubscriber.prototype.scheduleNotification = function (notification) {
|
---|
61 | if (this.errored === true) {
|
---|
62 | return;
|
---|
63 | }
|
---|
64 | var scheduler = this.scheduler;
|
---|
65 | var message = new DelayMessage(scheduler.now() + this.delay, notification);
|
---|
66 | this.queue.push(message);
|
---|
67 | if (this.active === false) {
|
---|
68 | this._schedule(scheduler);
|
---|
69 | }
|
---|
70 | };
|
---|
71 | DelaySubscriber.prototype._next = function (value) {
|
---|
72 | this.scheduleNotification(Notification.createNext(value));
|
---|
73 | };
|
---|
74 | DelaySubscriber.prototype._error = function (err) {
|
---|
75 | this.errored = true;
|
---|
76 | this.queue = [];
|
---|
77 | this.destination.error(err);
|
---|
78 | this.unsubscribe();
|
---|
79 | };
|
---|
80 | DelaySubscriber.prototype._complete = function () {
|
---|
81 | this.scheduleNotification(Notification.createComplete());
|
---|
82 | this.unsubscribe();
|
---|
83 | };
|
---|
84 | return DelaySubscriber;
|
---|
85 | }(Subscriber));
|
---|
86 | var DelayMessage = /*@__PURE__*/ (function () {
|
---|
87 | function DelayMessage(time, notification) {
|
---|
88 | this.time = time;
|
---|
89 | this.notification = notification;
|
---|
90 | }
|
---|
91 | return DelayMessage;
|
---|
92 | }());
|
---|
93 | //# sourceMappingURL=delay.js.map
|
---|