diff --git a/spec/operators/timeout-spec.ts b/spec/operators/timeout-spec.ts index ebedfdac82a..5f69e46e3ce 100644 --- a/spec/operators/timeout-spec.ts +++ b/spec/operators/timeout-spec.ts @@ -691,6 +691,15 @@ describe('timeout operator', () => { expectSubscriptions(inner.subscriptions).toBe([]); }); }); + + it('should not timeout if source emits synchronously when subscribed', () => { + rxTestScheduler.run(({ expectObservable, time }) => { + const source = new BehaviorSubject('a'); + const t = time(' ---|'); + const expected = 'a---'; + expectObservable(source.pipe(timeout({ first: new Date(t) }))).toBe(expected); + }); + }); }); it('should stop listening to a synchronous observable when unsubscribed', () => { diff --git a/src/internal/operators/timeout.ts b/src/internal/operators/timeout.ts index 04e05f0ef65..d8b00dbdf2d 100644 --- a/src/internal/operators/timeout.ts +++ b/src/internal/operators/timeout.ts @@ -360,6 +360,12 @@ export function timeout, M>( ); }; + // Intentionally terse code. + // If `first` was provided, and it's a number, then use it. + // If `first` was provided and it's not a number, it's a Date, and we get the difference between it and "now". + // If `first` was not provided at all, then our first timer will be the value from `each`. + startTimer(first != null ? (typeof first === 'number' ? first : +first - scheduler!.now()) : each!); + originalSourceSubscription = source.subscribe( createOperatorSubscriber( subscriber, @@ -384,12 +390,6 @@ export function timeout, M>( } ) ); - - // Intentionally terse code. - // If `first` was provided, and it's a number, then use it. - // If `first` was provided and it's not a number, it's a Date, and we get the difference between it and "now". - // If `first` was not provided at all, then our first timer will be the value from `each`. - startTimer(first != null ? (typeof first === 'number' ? first : +first - scheduler!.now()) : each!); }); }