| 1 |
import { async } from '../scheduler/async'; |
| 2 |
import { isValidDate } from '../util/isDate'; |
| 3 |
import { ObservableInput, OperatorFunction, SchedulerLike } from '../types'; |
| 4 |
import { timeout } from './timeout'; |
| 5 |
|
| 6 |
/** @deprecated Replaced with {@link timeout}. Instead of `timeoutWith(someDate, a$, scheduler)`, use the configuration object |
| 7 |
* `timeout({ first: someDate, with: () => a$, scheduler })`. Will be removed in v8. */ |
| 8 |
export function timeoutWith<T, R>(dueBy: Date, switchTo: ObservableInput<R>, scheduler?: SchedulerLike): OperatorFunction<T, T | R>; |
| 9 |
/** @deprecated Replaced with {@link timeout}. Instead of `timeoutWith(100, a$, scheduler)`, use the configuration object |
| 10 |
* `timeout({ each: 100, with: () => a$, scheduler })`. Will be removed in v8. */ |
| 11 |
export function timeoutWith<T, R>(waitFor: number, switchTo: ObservableInput<R>, scheduler?: SchedulerLike): OperatorFunction<T, T | R>; |
| 12 |
|
| 13 |
/** |
| 14 |
* When the passed timespan elapses before the source emits any given value, it will unsubscribe from the source, |
| 15 |
* and switch the subscription to another observable. |
| 16 |
* |
| 17 |
* <span class="informal">Used to switch to a different observable if your source is being slow.</span> |
| 18 |
* |
| 19 |
* Useful in cases where: |
| 20 |
* |
| 21 |
* - You want to switch to a different source that may be faster. |
| 22 |
* - You want to notify a user that the data stream is slow. |
| 23 |
* - You want to emit a custom error rather than the {@link TimeoutError} emitted |
| 24 |
* by the default usage of {@link timeout}. |
| 25 |
* |
| 26 |
* If the first parameter is passed as Date and the time of the Date arrives before the first value arrives from the source, |
| 27 |
* it will unsubscribe from the source and switch the subscription to another observable. |
| 28 |
* |
| 29 |
* <span class="informal">Use Date object to switch to a different observable if the first value doesn't arrive by a specific time.</span> |
| 30 |
* |
| 31 |
* Can be used to set a timeout only for the first value, however it's recommended to use the {@link timeout} operator with |
| 32 |
* the `first` configuration to get the same effect. |
| 33 |
* |
| 34 |
* ## Examples |
| 35 |
* |
| 36 |
* Fallback to a faster observable |
| 37 |
* |
| 38 |
* ```ts |
| 39 |
* import { interval, timeoutWith } from 'rxjs'; |
| 40 |
* |
| 41 |
* const slow$ = interval(1000); |
| 42 |
* const faster$ = interval(500); |
| 43 |
* |
| 44 |
* slow$ |
| 45 |
* .pipe(timeoutWith(900, faster$)) |
| 46 |
* .subscribe(console.log); |
| 47 |
* ``` |
| 48 |
* |
| 49 |
* Emit your own custom timeout error |
| 50 |
* |
| 51 |
* ```ts |
| 52 |
* import { interval, timeoutWith, throwError } from 'rxjs'; |
| 53 |
* |
| 54 |
* class CustomTimeoutError extends Error { |
| 55 |
* constructor() { |
| 56 |
* super('It was too slow'); |
| 57 |
* this.name = 'CustomTimeoutError'; |
| 58 |
* } |
| 59 |
* } |
| 60 |
* |
| 61 |
* const slow$ = interval(1000); |
| 62 |
* |
| 63 |
* slow$ |
| 64 |
* .pipe(timeoutWith(900, throwError(() => new CustomTimeoutError()))) |
| 65 |
* .subscribe({ |
| 66 |
* error: err => console.error(err.message) |
| 67 |
* }); |
| 68 |
* ``` |
| 69 |
* |
| 70 |
* @see {@link timeout} |
| 71 |
* |
| 72 |
* @param due When passed a number, used as the time (in milliseconds) allowed between each value from the source before timeout |
| 73 |
* is triggered. When passed a Date, used as the exact time at which the timeout will be triggered if the first value does not arrive. |
| 74 |
* @param withObservable The observable to switch to when timeout occurs. |
| 75 |
* @param scheduler The scheduler to use with time-related operations within this operator. Defaults to {@link asyncScheduler} |
| 76 |
* @return A function that returns an Observable that mirrors behaviour of the |
| 77 |
* source Observable, unless timeout happens when it starts emitting values |
| 78 |
* from the `ObservableInput` passed as a second parameter. |
| 79 |
* @deprecated Replaced with {@link timeout}. Instead of `timeoutWith(100, a$, scheduler)`, use {@link timeout} with the configuration |
| 80 |
* object: `timeout({ each: 100, with: () => a$, scheduler })`. Instead of `timeoutWith(someDate, a$, scheduler)`, use {@link timeout} |
| 81 |
* with the configuration object: `timeout({ first: someDate, with: () => a$, scheduler })`. Will be removed in v8. |
| 82 |
*/ |
| 83 |
export function timeoutWith<T, R>( |
| 84 |
due: number | Date, |
| 85 |
withObservable: ObservableInput<R>, |
| 86 |
scheduler?: SchedulerLike |
| 87 |
): OperatorFunction<T, T | R> { |
| 88 |
let first: number | Date | undefined; |
| 89 |
let each: number | undefined; |
| 90 |
let _with: () => ObservableInput<R>; |
| 91 |
scheduler = scheduler ?? async; |
| 92 |
|
| 93 |
if (isValidDate(due)) { |
| 94 |
first = due; |
| 95 |
} else if (typeof due === 'number') { |
| 96 |
each = due; |
| 97 |
} |
| 98 |
|
| 99 |
if (withObservable) { |
| 100 |
_with = () => withObservable; |
| 101 |
} else { |
| 102 |
throw new TypeError('No observable provided to switch to'); |
| 103 |
} |
| 104 |
|
| 105 |
if (first == null && each == null) { |
| 106 |
// Ensure timeout was provided at runtime. |
| 107 |
throw new TypeError('No timeout provided.'); |
| 108 |
} |
| 109 |
|
| 110 |
return timeout<T, ObservableInput<R>>({ |
| 111 |
first, |
| 112 |
each, |
| 113 |
scheduler, |
| 114 |
with: _with, |
| 115 |
}); |
| 116 |
} |