Skip to content

Commit

Permalink
fix(timer): multiple subscriptions to timer(Date) behaves correctly
Browse files Browse the repository at this point in the history
- Moves the calculation of the due time to the subscription phase of the Observable
- Adds a test confirming multiple subscriptions on delay

fixes #3252
  • Loading branch information
benlesh committed Jan 23, 2018
1 parent cfd846d commit aafa7ff
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
21 changes: 20 additions & 1 deletion spec/observables/timer-spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import marbleTestingSignature = require('../helpers/marble-testing'); // tslint:disable-line:no-require-imports
import { timer, never } from '../../src/create';
import { timer, never, merge } from '../../src/create';
import { TestScheduler } from '../../src/testing';
import { mergeMap } from '../../src/operators';

declare const asDiagram: any;
declare const time: typeof marbleTestingSignature.time;
declare const expectObservable: typeof marbleTestingSignature.expectObservable;
declare const cold: typeof marbleTestingSignature.cold;
declare const rxTestScheduler: TestScheduler;

/** @test {timer} */
Expand Down Expand Up @@ -89,4 +91,21 @@ describe('timer', () => {
const values = { a: 0, b: 1, c: 2, d: 3, e: 4};
expectObservable(source).toBe(expected, values);
});

it('should still target the same date if a date is provided even for the ' +
'second subscription', () => {
const offset = time('----| ');
const t1 = cold( 'a| ');
const t2 = cold( '--a| ');
const expected = '----(aa|)';

const dueTime = new Date(rxTestScheduler.now() + offset);
const source = timer(dueTime, null, rxTestScheduler);

const testSource = merge(t1, t2).pipe(
mergeMap(() => source)
);

expectObservable(testSource).toBe(expected, {a: 0});
});
});
14 changes: 8 additions & 6 deletions src/internal/observable/timer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ export function timer(dueTime: number | Date = 0,
scheduler = async;
}

const due = isNumeric(dueTime)
? (dueTime as number)
: (+dueTime - scheduler.now());
return new Observable(subscriber => {
const due = isNumeric(dueTime)
? (dueTime as number)
: (+dueTime - scheduler.now());

return new Observable(subscriber => scheduler.schedule(dispatch, due, {
index: 0, period, subscriber
}));
return scheduler.schedule(dispatch, due, {
index: 0, period, subscriber
});
});
}

interface TimerState {
Expand Down

0 comments on commit aafa7ff

Please sign in to comment.