source: trip-planner-front/node_modules/rxjs/src/internal/operators/timeout.ts@ e29cc2e

Last change on this file since e29cc2e was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

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