Skip to content

Commit

Permalink
feat(timeout): One timeout to rule them all
Browse files Browse the repository at this point in the history
- Adds configuration argument pattern
- Fixes issue where `timeout` and `timeoutWith` would always timeout when provided a Date as a due time, even if the source emitted a value before then.
- Adds ability to configure timeout to have a different `first` timeout check and subsequent `each` timeout checks
- Adds ability to timeout if _just_ the first value does not arrive after a specified number of milliseconds
- Adds additional information to `TimeoutError`s thrown by bare `timeout` calls.
- Adds `with` parameter to `timeout` operator's configuration, such that now `timeout` can be used in exactly the same ways as `timeoutWith` and more. The `with` parameter is a function that provides information about the timeout and expects an `ObservableInput` to switch to in response to a timeout.
- Deprecates `timeoutWith`.
- Updates tests to use run mode.
- Adds additional tests to `timeout` to cover more aspects of the configuration possiblities.
- Improves type signatures for `timeout` and `timeoutWith`.
  • Loading branch information
benlesh committed Aug 2, 2020
1 parent 28bd198 commit def1d34
Show file tree
Hide file tree
Showing 9 changed files with 1,388 additions and 673 deletions.
3 changes: 2 additions & 1 deletion api_guard/dist/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,8 @@ export interface TimeInterval<T> {
value: T;
}

export interface TimeoutError extends Error {
export interface TimeoutError<T = unknown, M = unknown> extends Error {
info: TimeoutInfo<T, M> | null;
}

export declare const TimeoutError: TimeoutErrorCtor;
Expand Down
12 changes: 9 additions & 3 deletions api_guard/dist/types/operators/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,15 @@ export declare function throwIfEmpty<T>(errorFactory?: (() => any)): MonoTypeOpe

export declare function timeInterval<T>(scheduler?: SchedulerLike): OperatorFunction<T, TimeInterval<T>>;

export declare function timeout<T>(due: number | Date, scheduler?: SchedulerLike): MonoTypeOperatorFunction<T>;

export declare function timeoutWith<T, R>(due: number | Date, withObservable: ObservableInput<R>, scheduler?: SchedulerLike): OperatorFunction<T, T | R>;
export declare function timeout<T, R, M = unknown>(config: TimeoutConfig<T, R, M> & {
with: (info: TimeoutInfo<T, M>) => ObservableInput<R>;
}): OperatorFunction<T, T | R>;
export declare function timeout<T, M = unknown>(config: Omit<TimeoutConfig<T, any, M>, 'with'>): OperatorFunction<T, T>;
export declare function timeout<T>(first: Date, scheduler?: SchedulerLike): MonoTypeOperatorFunction<T>;
export declare function timeout<T>(each: number, scheduler?: SchedulerLike): MonoTypeOperatorFunction<T>;

export declare function timeoutWith<T, R>(dueBy: Date, switchTo: ObservableInput<R>, scheduler?: SchedulerLike): OperatorFunction<T, T | R>;
export declare function timeoutWith<T, R>(waitFor: number, switchTo: ObservableInput<R>, scheduler?: SchedulerLike): OperatorFunction<T, T | R>;

export declare function timestamp<T>(timestampProvider?: TimestampProvider): OperatorFunction<T, Timestamp<T>>;

Expand Down
28 changes: 28 additions & 0 deletions spec-dtslint/operators/timeout-spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { of, asyncScheduler } from 'rxjs';
import { timeout } from 'rxjs/operators';
import { A } from '../helpers';

it('should infer correctly', () => {
const o = of('a', 'b', 'c').pipe(timeout(10)); // $ExpectType Observable<string>
Expand All @@ -25,3 +26,30 @@ it('should enforce types of due', () => {
it('should enforce types of scheduler', () => {
const o = of('a', 'b', 'c').pipe(timeout(5, 'foo')); // $ExpectError
});

it('Check info argument to factory', () => {
const o = of('a').pipe( // $ExpectType Observable<string | number>
timeout({
meta: new A(),
with: (info) => {
const i = info; // $ExpectType TimeoutInfo<string, A>
const m = info.meta; // $ExpectType A
const s = info.seen; // $ExpectType number
const l = info.lastValue; // $ExpectType string | null
// These should be readonly
info.meta = new A(); // $ExpectError
info.seen = 12; // $ExpectError
info.lastValue = 'blah'; // $ExpectError
return of(123);
}
})
);
});

it('Check config arguments', () => {
const o = of('a').pipe( // $ExpectType Observable<string>
timeout({
first: 1000
})
);
});
Loading

0 comments on commit def1d34

Please sign in to comment.