UNPKG

11.1 kBTypeScriptView Raw
1import { MonoTypeOperatorFunction, SchedulerLike, OperatorFunction, ObservableInput, ObservedValueOf } from '../types';
2export interface TimeoutConfig<T, O extends ObservableInput<unknown> = ObservableInput<T>, M = unknown> {
3 /**
4 * The time allowed between values from the source before timeout is triggered.
5 */
6 each?: number;
7 /**
8 * The relative time as a `number` in milliseconds, or a specific time as a `Date` object,
9 * by which the first value must arrive from the source before timeout is triggered.
10 */
11 first?: number | Date;
12 /**
13 * The scheduler to use with time-related operations within this operator. Defaults to {@link asyncScheduler}
14 */
15 scheduler?: SchedulerLike;
16 /**
17 * A factory used to create observable to switch to when timeout occurs. Provides
18 * some information about the source observable's emissions and what delay or
19 * exact time triggered the timeout.
20 */
21 with?: (info: TimeoutInfo<T, M>) => O;
22 /**
23 * Optional additional metadata you can provide to code that handles
24 * the timeout, will be provided through the {@link TimeoutError}.
25 * This can be used to help identify the source of a timeout or pass along
26 * other information related to the timeout.
27 */
28 meta?: M;
29}
30export interface TimeoutInfo<T, M = unknown> {
31 /** Optional metadata that was provided to the timeout configuration. */
32 readonly meta: M;
33 /** The number of messages seen before the timeout */
34 readonly seen: number;
35 /** The last message seen */
36 readonly lastValue: T | null;
37}
38/**
39 * An error emitted when a timeout occurs.
40 */
41export interface TimeoutError<T = unknown, M = unknown> extends Error {
42 /**
43 * The information provided to the error by the timeout
44 * operation that created the error. Will be `null` if
45 * used directly in non-RxJS code with an empty constructor.
46 * (Note that using this constructor directly is not recommended,
47 * you should create your own errors)
48 */
49 info: TimeoutInfo<T, M> | null;
50}
51export interface TimeoutErrorCtor {
52 /**
53 * @deprecated Internal implementation detail. Do not construct error instances.
54 * Cannot be tagged as internal: https://github.com/ReactiveX/rxjs/issues/6269
55 */
56 new <T = unknown, M = unknown>(info?: TimeoutInfo<T, M>): TimeoutError<T, M>;
57}
58/**
59 * An error thrown by the {@link operators/timeout} operator.
60 *
61 * Provided so users can use as a type and do quality comparisons.
62 * We recommend you do not subclass this or create instances of this class directly.
63 * If you have need of a error representing a timeout, you should
64 * create your own error class and use that.
65 *
66 * @see {@link operators/timeout}
67 *
68 * @class TimeoutError
69 */
70export declare const TimeoutError: TimeoutErrorCtor;
71/**
72 * If `with` is provided, this will return an observable that will switch to a different observable if the source
73 * does not push values within the specified time parameters.
74 *
75 * <span class="informal">The most flexible option for creating a timeout behavior.</span>
76 *
77 * The first thing to know about the configuration is if you do not provide a `with` property to the configuration,
78 * when timeout conditions are met, this operator will emit a {@link TimeoutError}. Otherwise, it will use the factory
79 * function provided by `with`, and switch your subscription to the result of that. Timeout conditions are provided by
80 * the settings in `first` and `each`.
81 *
82 * The `first` property can be either a `Date` for a specific time, a `number` for a time period relative to the
83 * point of subscription, or it can be skipped. This property is to check timeout conditions for the arrival of
84 * the first value from the source _only_. The timings of all subsequent values from the source will be checked
85 * against the time period provided by `each`, if it was provided.
86 *
87 * The `each` property can be either a `number` or skipped. If a value for `each` is provided, it represents the amount of
88 * time the resulting observable will wait between the arrival of values from the source before timing out. Note that if
89 * `first` is _not_ provided, the value from `each` will be used to check timeout conditions for the arrival of the first
90 * value and all subsequent values. If `first` _is_ provided, `each` will only be use to check all values after the first.
91 *
92 * ### Example
93 *
94 * Emit a custom error if there is too much time between values
95 *
96 * ```ts
97 * import { interval, throwError } from 'rxjs';
98 * import { timeout } from 'rxjs/operators';
99 *
100 * class CustomTimeoutError extends Error {
101 * constructor() {
102 * super('It was too slow');
103 * this.name = 'CustomTimeoutError';
104 * }
105 * }
106 *
107 * const slow$ = interval(900);
108 *
109 * slow$.pipe(
110 * timeout({
111 * each: 1000,
112 * with: () => throwError(new CustomTimeoutError())
113 * })
114 * )
115 * .subscribe({
116 * error: console.error
117 * })
118 * ```
119 *
120 * ### Example
121 *
122 * Switch to a faster observable if your source is slow.
123 *
124 * ```ts
125 * import { interval, throwError } from 'rxjs';
126 * import { timeout } from 'rxjs/operators';
127 *
128 * const slow$ = interval(900);
129 * const fast$ = interval(500);
130 *
131 * slow$.pipe(
132 * timeout({
133 * each: 1000,
134 * with: () => fast$,
135 * })
136 * )
137 * .subscribe(console.log)
138 * ```
139 * @param config The configuration for the timeout.
140 */
141export declare function timeout<T, O extends ObservableInput<unknown>, M = unknown>(config: TimeoutConfig<T, O, M> & {
142 with: (info: TimeoutInfo<T, M>) => O;
143}): OperatorFunction<T, T | ObservedValueOf<O>>;
144/**
145 * Returns an observable that will error or switch to a different observable if the source does not push values
146 * within the specified time parameters.
147 *
148 * <span class="informal">The most flexible option for creating a timeout behavior.</span>
149 *
150 * The first thing to know about the configuration is if you do not provide a `with` property to the configuration,
151 * when timeout conditions are met, this operator will emit a {@link TimeoutError}. Otherwise, it will use the factory
152 * function provided by `with`, and switch your subscription to the result of that. Timeout conditions are provided by
153 * the settings in `first` and `each`.
154 *
155 * The `first` property can be either a `Date` for a specific time, a `number` for a time period relative to the
156 * point of subscription, or it can be skipped. This property is to check timeout conditions for the arrival of
157 * the first value from the source _only_. The timings of all subsequent values from the source will be checked
158 * against the time period provided by `each`, if it was provided.
159 *
160 * The `each` property can be either a `number` or skipped. If a value for `each` is provided, it represents the amount of
161 * time the resulting observable will wait between the arrival of values from the source before timing out. Note that if
162 * `first` is _not_ provided, the value from `each` will be used to check timeout conditions for the arrival of the first
163 * value and all subsequent values. If `first` _is_ provided, `each` will only be use to check all values after the first.
164 *
165 * ### Handling TimeoutErrors
166 *
167 * If no `with` property was provided, subscriptions to the resulting observable may emit an error of {@link TimeoutError}.
168 * The timeout error provides useful information you can examine when you're handling the error. The most common way to handle
169 * the error would be with {@link catchError}, although you could use {@link tap} or just the error handler in your `subscribe` call
170 * directly, if your error handling is only a side effect (such as notifying the user, or logging).
171 *
172 * In this case, you would check the error for `instanceof TimeoutError` to validate that the error was indeed from `timeout`, and
173 * not from some other source. If it's not from `timeout`, you should probably rethrow it if you're in a `catchError`.
174 *
175 *
176 * ### Example
177 *
178 * Emit a {@link TimeoutError} if the first value, and _only_ the first value, does not arrive within 5 seconds
179 *
180 * ```ts
181 * import { interval } from 'rxjs';
182 * import { timeout } from 'rxjs/operators';
183 *
184 * // A random interval that lasts between 0 and 10 seconds per tick
185 * const source$ = interval(Math.round(Math.random() * 10000));
186 *
187 * source$.pipe(
188 * timeout({ first: 5000 })
189 * )
190 * .subscribe(console.log);
191 * ```
192 *
193 * ### Example
194 *
195 * Emit a {@link TimeoutError} if the source waits longer than 5 seconds between any two values or the first value
196 * and subscription.
197 *
198 * ```ts
199 * import { timer } from 'rxjs';
200 * import { timeout, expand } from 'rxjs/operators';
201 *
202 * const getRandomTime = () => Math.round(Math.random() * 10000);
203 *
204 * // An observable that waits a random amount of time between each delivered value
205 * const source$ = timer(getRandomTime()).pipe(
206 * expand(() => timer(getRandomTime()))
207 * )
208 *
209 * source$.pipe(
210 * timeout({ each: 5000 })
211 * )
212 * .subscribe(console.log);
213 * ```
214 *
215 * ### Example
216 *
217 * Emit a {@link TimeoutError} if the the source does not emit before 7 seconds, _or_ if the source waits longer than
218 * 5 seconds between any two values after the first.
219 *
220 * ```ts
221 * import { timer } from 'rxjs';
222 * import { timeout, expand } from 'rxjs/operators';
223 *
224 * const getRandomTime = () => Math.round(Math.random() * 10000);
225 *
226 * // An observable that waits a random amount of time between each delivered value
227 * const source$ = timer(getRandomTime()).pipe(
228 * expand(() => timer(getRandomTime()))
229 * )
230 *
231 * source$.pipe(
232 * timeout({ first: 7000, each: 5000 })
233 * )
234 * .subscribe(console.log);
235 * ```
236 */
237export declare function timeout<T, M = unknown>(config: Omit<TimeoutConfig<T, any, M>, 'with'>): OperatorFunction<T, T>;
238/**
239 * Returns an observable that will error if the source does not push its first value before the specified time passed as a `Date`.
240 * This is functionally the same as `timeout({ first: someDate })`.
241 *
242 * <span class="informal">Errors if the first value doesn't show up before the given date and time</span>
243 *
244 * ![](timeout.png)
245 *
246 * @param first The date to at which the resulting observable will timeout if the source observable
247 * does not emit at least one value.
248 * @param scheduler The scheduler to use. Defaults to {@link asyncScheduler}.
249 */
250export declare function timeout<T>(first: Date, scheduler?: SchedulerLike): MonoTypeOperatorFunction<T>;
251/**
252 * Returns an observable that will error if the source does not push a value within the specified time in milliseconds.
253 * This is functionally the same as `timeout({ each: milliseconds })`.
254 *
255 * <span class="informal">Errors if it waits too long between any value</span>
256 *
257 * ![](timeout.png)
258 *
259 * @param each The time allowed between each pushed value from the source before the resulting observable
260 * will timeout.
261 * @param scheduler The scheduler to use. Defaults to {@link asyncScheduler}.
262 */
263export declare function timeout<T>(each: number, scheduler?: SchedulerLike): MonoTypeOperatorFunction<T>;
264//# sourceMappingURL=timeout.d.ts.map
\No newline at end of file