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 #6862
  • Loading branch information
danielwiehl committed Mar 7, 2022
1 parent e48e296 commit 5b2ac2c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
11 changes: 10 additions & 1 deletion spec/operators/timeout-spec.ts
Expand Up @@ -2,7 +2,7 @@
import { expect } from 'chai';
import { timeout, mergeMap, take } from 'rxjs/operators';
import { TestScheduler } from 'rxjs/testing';
import { TimeoutError, of, Observable } from 'rxjs';
import { TimeoutError, of, Observable, BehaviorSubject } from 'rxjs';
import { observableMatcher } from '../helpers/observableMatcher';

/** @test {timeout} */
Expand Down 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
2 changes: 1 addition & 1 deletion src/internal/operators/timeout.ts
Expand Up @@ -389,7 +389,7 @@ export function timeout<T, O extends ObservableInput<any>, M>(
// 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!);
!seen && startTimer(first != null ? (typeof first === 'number' ? first : +first - scheduler!.now()) : each!);
});
}

Expand Down

0 comments on commit 5b2ac2c

Please sign in to comment.