biobulkbende.org/node_modules/rxjs/_esm5/operators/timeout.js

145 lines
6.6 KiB
JavaScript
Raw Normal View History

2020-09-16 12:23:28 +00:00
/** PURE_IMPORTS_START .._scheduler_async,.._util_isDate,.._Subscriber,.._util_TimeoutError PURE_IMPORTS_END */
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 __());
};
import { async } from '../scheduler/async';
import { isDate } from '../util/isDate';
import { Subscriber } from '../Subscriber';
import { TimeoutError } from '../util/TimeoutError';
/**
*
* Errors if Observable does not emit a value in given time span.
*
* <span class="informal">Timeouts on Observable that doesn't emit values fast enough.</span>
*
* <img src="./img/timeout.png" width="100%">
*
* `timeout` operator accepts as an argument either a number or a Date.
*
* If number was provided, it returns an Observable that behaves like a source
* Observable, unless there is a period of time where there is no value emitted.
* So if you provide `100` as argument and first value comes after 50ms from
* the moment of subscription, this value will be simply re-emitted by the resulting
* Observable. If however after that 100ms passes without a second value being emitted,
* stream will end with an error and source Observable will be unsubscribed.
* These checks are performed throughout whole lifecycle of Observable - from the moment
* it was subscribed to, until it completes or errors itself. Thus every value must be
* emitted within specified period since previous value.
*
* If provided argument was Date, returned Observable behaves differently. It throws
* if Observable did not complete before provided Date. This means that periods between
* emission of particular values do not matter in this case. If Observable did not complete
* before provided Date, source Observable will be unsubscribed. Other than that, resulting
* stream behaves just as source Observable.
*
* `timeout` accepts also a Scheduler as a second parameter. It is used to schedule moment (or moments)
* when returned Observable will check if source stream emitted value or completed.
*
* @example <caption>Check if ticks are emitted within certain timespan</caption>
* const seconds = Rx.Observable.interval(1000);
*
* seconds.timeout(1100) // Let's use bigger timespan to be safe,
* // since `interval` might fire a bit later then scheduled.
* .subscribe(
* value => console.log(value), // Will emit numbers just as regular `interval` would.
* err => console.log(err) // Will never be called.
* );
*
* seconds.timeout(900).subscribe(
* value => console.log(value), // Will never be called.
* err => console.log(err) // Will emit error before even first value is emitted,
* // since it did not arrive within 900ms period.
* );
*
* @example <caption>Use Date to check if Observable completed</caption>
* const seconds = Rx.Observable.interval(1000);
*
* seconds.timeout(new Date("December 17, 2020 03:24:00"))
* .subscribe(
* value => console.log(value), // Will emit values as regular `interval` would
* // until December 17, 2020 at 03:24:00.
* err => console.log(err) // On December 17, 2020 at 03:24:00 it will emit an error,
* // since Observable did not complete by then.
* );
*
* @see {@link timeoutWith}
*
* @param {number|Date} due Number specifying period within which Observable must emit values
* or Date specifying before when Observable should complete
* @param {Scheduler} [scheduler] Scheduler controlling when timeout checks occur.
* @return {Observable<T>} Observable that mirrors behaviour of source, unless timeout checks fail.
* @method timeout
* @owner Observable
*/
export function timeout(due, scheduler) {
if (scheduler === void 0) {
scheduler = async;
}
var absoluteTimeout = isDate(due);
var waitFor = absoluteTimeout ? (+due - scheduler.now()) : Math.abs(due);
return function (source) { return source.lift(new TimeoutOperator(waitFor, absoluteTimeout, scheduler, new TimeoutError())); };
}
var TimeoutOperator = /*@__PURE__*/ (/*@__PURE__*/ 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 = /*@__PURE__*/ (/*@__PURE__*/ 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.action = null;
this.scheduleTimeout();
}
TimeoutSubscriber.dispatchTimeout = function (subscriber) {
subscriber.error(subscriber.errorInstance);
};
TimeoutSubscriber.prototype.scheduleTimeout = function () {
var action = this.action;
if (action) {
// Recycle the action if we've already scheduled one. All the production
// Scheduler Actions mutate their state/delay time and return themeselves.
// VirtualActions are immutable, so they create and return a clone. In this
// case, we need to set the action reference to the most recent VirtualAction,
// to ensure that's the one we clone from next time.
this.action = action.schedule(this, this.waitFor);
}
else {
this.add(this.action = this.scheduler.schedule(TimeoutSubscriber.dispatchTimeout, this.waitFor, this));
}
};
TimeoutSubscriber.prototype._next = function (value) {
if (!this.absoluteTimeout) {
this.scheduleTimeout();
}
_super.prototype._next.call(this, value);
};
/** @deprecated internal use only */ TimeoutSubscriber.prototype._unsubscribe = function () {
this.action = null;
this.scheduler = null;
this.errorInstance = null;
};
return TimeoutSubscriber;
}(Subscriber));
//# sourceMappingURL=timeout.js.map