Skip to content

Commit

Permalink
fix(TimerObservable): accepts absolute date for dueTime
Browse files Browse the repository at this point in the history
closes #648
  • Loading branch information
kwonoj authored and benlesh committed Nov 18, 2015
1 parent 8859702 commit e284fb8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
15 changes: 15 additions & 0 deletions spec/observables/timer-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,19 @@ describe('Observable.timer', function () {

expectObservable(source, unsub).toBe(expected, values);
});

it('should schedule a value at a specified Date', function () {
var source = Observable.timer(new Date(rxTestScheduler.now() + 40), null, rxTestScheduler);
var expected = '----(a|)';

expectObservable(source).toBe(expected, {a: 0});
});

it('should start after delay and periodically emit values', function () {
var source = Observable.timer(new Date(rxTestScheduler.now() + 40), 20, rxTestScheduler).take(5);
var expected = '----a-b-c-d-(e|)';
var values = { a: 0, b: 1, c: 2, d: 3, e: 4};

expectObservable(source).toBe(expected, values);
});
});
2 changes: 1 addition & 1 deletion src/Observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export class Observable<T> implements CoreOperators<T> {
static of: <T>(...values: Array<T | Scheduler>) => Observable<T>;
static range: (start: number, end: number, scheduler?: Scheduler) => Observable<number>;
static throw: <T>(error: T) => Observable<T>;
static timer: (dueTime: number, period?: number | Scheduler, scheduler?: Scheduler) => Observable<number>;
static timer: (dueTime?: number | Date, period?: number | Scheduler, scheduler?: Scheduler) => Observable<number>;
static zip: <T>(...observables: Array<Observable<any> | ((...values: Array<any>) => T)>) => Observable<T>;

// core operators
Expand Down
12 changes: 10 additions & 2 deletions src/observables/TimerObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import {Scheduler} from '../Scheduler';
import {Observable} from '../Observable';
import {nextTick} from '../schedulers/nextTick';
import {isScheduler} from '../util/isScheduler';
import {isDate} from '../util/isDate';

export class TimerObservable<T> extends Observable<T> {

static create(dueTime: number = 0, period?: number | Scheduler, scheduler?: Scheduler): Observable<number> {
static create(dueTime: number | Date = 0, period?: number | Scheduler, scheduler?: Scheduler): Observable<number> {
return new TimerObservable(dueTime, period, scheduler);
}

Expand Down Expand Up @@ -35,9 +36,13 @@ export class TimerObservable<T> extends Observable<T> {
}

_period: number;
private dueTime: number = 0;

constructor(private dueTime: number = 0, private period?: number | Scheduler, private scheduler?: Scheduler) {
constructor(dueTime: number | Date = 0,
private period?: number | Scheduler,
private scheduler?: Scheduler) {
super();

if (isNumeric(period)) {
this._period = Number(period) < 1 && 1 || Number(period);
} else if (isScheduler(period)) {
Expand All @@ -47,6 +52,9 @@ export class TimerObservable<T> extends Observable<T> {
scheduler = nextTick;
}
this.scheduler = scheduler;

const absoluteDueTime = isDate(dueTime);
this.dueTime = absoluteDueTime ? (+dueTime - this.scheduler.now()) : <number>dueTime;
}

_subscribe(subscriber) {
Expand Down

0 comments on commit e284fb8

Please sign in to comment.