[6a3a178] | 1 | import { MonoTypeOperatorFunction, SchedulerLike } from '../types';
|
---|
| 2 | /**
|
---|
| 3 | *
|
---|
| 4 | * Errors if Observable does not emit a value in given time span.
|
---|
| 5 | *
|
---|
| 6 | * <span class="informal">Timeouts on Observable that doesn't emit values fast enough.</span>
|
---|
| 7 | *
|
---|
| 8 | * ![](timeout.png)
|
---|
| 9 | *
|
---|
| 10 | * `timeout` operator accepts as an argument either a number or a Date.
|
---|
| 11 | *
|
---|
| 12 | * If number was provided, it returns an Observable that behaves like a source
|
---|
| 13 | * Observable, unless there is a period of time where there is no value emitted.
|
---|
| 14 | * So if you provide `100` as argument and first value comes after 50ms from
|
---|
| 15 | * the moment of subscription, this value will be simply re-emitted by the resulting
|
---|
| 16 | * Observable. If however after that 100ms passes without a second value being emitted,
|
---|
| 17 | * stream will end with an error and source Observable will be unsubscribed.
|
---|
| 18 | * These checks are performed throughout whole lifecycle of Observable - from the moment
|
---|
| 19 | * it was subscribed to, until it completes or errors itself. Thus every value must be
|
---|
| 20 | * emitted within specified period since previous value.
|
---|
| 21 | *
|
---|
| 22 | * If provided argument was Date, returned Observable behaves differently. It throws
|
---|
| 23 | * if Observable did not complete before provided Date. This means that periods between
|
---|
| 24 | * emission of particular values do not matter in this case. If Observable did not complete
|
---|
| 25 | * before provided Date, source Observable will be unsubscribed. Other than that, resulting
|
---|
| 26 | * stream behaves just as source Observable.
|
---|
| 27 | *
|
---|
| 28 | * `timeout` accepts also a Scheduler as a second parameter. It is used to schedule moment (or moments)
|
---|
| 29 | * when returned Observable will check if source stream emitted value or completed.
|
---|
| 30 | *
|
---|
| 31 | * ## Examples
|
---|
| 32 | * Check if ticks are emitted within certain timespan
|
---|
| 33 | * ```ts
|
---|
| 34 | * import { interval } from 'rxjs';
|
---|
| 35 | * import { timeout } from 'rxjs/operators';
|
---|
| 36 | *
|
---|
| 37 | * const seconds = interval(1000);
|
---|
| 38 | *
|
---|
| 39 | * seconds.pipe(timeout(1100)) // Let's use bigger timespan to be safe,
|
---|
| 40 | * // since `interval` might fire a bit later then scheduled.
|
---|
| 41 | * .subscribe(
|
---|
| 42 | * value => console.log(value), // Will emit numbers just as regular `interval` would.
|
---|
| 43 | * err => console.log(err), // Will never be called.
|
---|
| 44 | * );
|
---|
| 45 | *
|
---|
| 46 | * seconds.pipe(timeout(900))
|
---|
| 47 | * .subscribe(
|
---|
| 48 | * value => console.log(value), // Will never be called.
|
---|
| 49 | * err => console.log(err), // Will emit error before even first value is emitted,
|
---|
| 50 | * // since it did not arrive within 900ms period.
|
---|
| 51 | * );
|
---|
| 52 | * ```
|
---|
| 53 | *
|
---|
| 54 | * Use Date to check if Observable completed
|
---|
| 55 | * ```ts
|
---|
| 56 | * import { interval } from 'rxjs';
|
---|
| 57 | * import { timeout } from 'rxjs/operators';
|
---|
| 58 | *
|
---|
| 59 | * const seconds = interval(1000);
|
---|
| 60 | *
|
---|
| 61 | * seconds.pipe(
|
---|
| 62 | * timeout(new Date("December 17, 2020 03:24:00")),
|
---|
| 63 | * )
|
---|
| 64 | * .subscribe(
|
---|
| 65 | * value => console.log(value), // Will emit values as regular `interval` would
|
---|
| 66 | * // until December 17, 2020 at 03:24:00.
|
---|
| 67 | * err => console.log(err) // On December 17, 2020 at 03:24:00 it will emit an error,
|
---|
| 68 | * // since Observable did not complete by then.
|
---|
| 69 | * );
|
---|
| 70 | * ```
|
---|
| 71 | * @see {@link timeoutWith}
|
---|
| 72 | *
|
---|
| 73 | * @param {number|Date} due Number specifying period within which Observable must emit values
|
---|
| 74 | * or Date specifying before when Observable should complete
|
---|
| 75 | * @param {SchedulerLike} [scheduler] Scheduler controlling when timeout checks occur.
|
---|
| 76 | * @return {Observable<T>} Observable that mirrors behaviour of source, unless timeout checks fail.
|
---|
| 77 | * @method timeout
|
---|
| 78 | * @owner Observable
|
---|
| 79 | */
|
---|
| 80 | export declare function timeout<T>(due: number | Date, scheduler?: SchedulerLike): MonoTypeOperatorFunction<T>;
|
---|