timeout.js
3.7 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
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var async_1 = require('../scheduler/async');
var isDate_1 = require('../util/isDate');
var Subscriber_1 = require('../Subscriber');
var TimeoutError_1 = require('../util/TimeoutError');
/**
* @param {number} due
* @param {Scheduler} [scheduler]
* @return {Observable<R>|WebSocketSubject<T>|Observable<T>}
* @method timeout
* @owner Observable
*/
function timeout(due, scheduler) {
if (scheduler === void 0) { scheduler = async_1.async; }
var absoluteTimeout = isDate_1.isDate(due);
var waitFor = absoluteTimeout ? (+due - scheduler.now()) : Math.abs(due);
return this.lift(new TimeoutOperator(waitFor, absoluteTimeout, scheduler, new TimeoutError_1.TimeoutError()));
}
exports.timeout = timeout;
var TimeoutOperator = (function () {
function TimeoutOperator(waitFor, absoluteTimeout, scheduler, errorInstance) {
this.waitFor = waitFor;
this.absoluteTimeout = absoluteTimeout;
this.scheduler = scheduler;
this.errorInstance = errorInstance;
}
TimeoutOperator.prototype.call = function (subscriber, source) {
return source.subscribe(new TimeoutSubscriber(subscriber, this.absoluteTimeout, this.waitFor, this.scheduler, this.errorInstance));
};
return TimeoutOperator;
}());
/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
var TimeoutSubscriber = (function (_super) {
__extends(TimeoutSubscriber, _super);
function TimeoutSubscriber(destination, absoluteTimeout, waitFor, scheduler, errorInstance) {
_super.call(this, destination);
this.absoluteTimeout = absoluteTimeout;
this.waitFor = waitFor;
this.scheduler = scheduler;
this.errorInstance = errorInstance;
this.index = 0;
this._previousIndex = 0;
this._hasCompleted = false;
this.scheduleTimeout();
}
Object.defineProperty(TimeoutSubscriber.prototype, "previousIndex", {
get: function () {
return this._previousIndex;
},
enumerable: true,
configurable: true
});
Object.defineProperty(TimeoutSubscriber.prototype, "hasCompleted", {
get: function () {
return this._hasCompleted;
},
enumerable: true,
configurable: true
});
TimeoutSubscriber.dispatchTimeout = function (state) {
var source = state.subscriber;
var currentIndex = state.index;
if (!source.hasCompleted && source.previousIndex === currentIndex) {
source.notifyTimeout();
}
};
TimeoutSubscriber.prototype.scheduleTimeout = function () {
var currentIndex = this.index;
this.scheduler.schedule(TimeoutSubscriber.dispatchTimeout, this.waitFor, { subscriber: this, index: currentIndex });
this.index++;
this._previousIndex = currentIndex;
};
TimeoutSubscriber.prototype._next = function (value) {
this.destination.next(value);
if (!this.absoluteTimeout) {
this.scheduleTimeout();
}
};
TimeoutSubscriber.prototype._error = function (err) {
this.destination.error(err);
this._hasCompleted = true;
};
TimeoutSubscriber.prototype._complete = function () {
this.destination.complete();
this._hasCompleted = true;
};
TimeoutSubscriber.prototype.notifyTimeout = function () {
this.error(this.errorInstance);
};
return TimeoutSubscriber;
}(Subscriber_1.Subscriber));
//# sourceMappingURL=timeout.js.map