Skip to content

Commit

Permalink
fix(timeout): do not timeout if source emits synchronously when subsc…
Browse files Browse the repository at this point in the history
…ribed

Closes ReactiveX#6862
  • Loading branch information
danielwiehl committed Mar 7, 2022
1 parent e48e296 commit 309dcef
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
9 changes: 9 additions & 0 deletions spec/operators/timeout-spec.ts
Expand Up @@ -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', () => {
Expand Down
12 changes: 6 additions & 6 deletions src/internal/operators/timeout.ts
Expand Up @@ -360,6 +360,12 @@ export function timeout<T, O extends ObservableInput<any>, 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,
Expand All @@ -384,12 +390,6 @@ export function timeout<T, O extends ObservableInput<any>, 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!);
});
}

Expand Down

0 comments on commit 309dcef

Please sign in to comment.