Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
feat(spec): add a 'tick' callback to flush()
Browse files Browse the repository at this point in the history
This can be used to advance a secondary test scheduler like the Jasmine mock
Date every time flush advances to the next task to be processed.
  • Loading branch information
vikerman committed Aug 4, 2017
1 parent b0c5076 commit 06c2e90
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions lib/zone-spec/fake-async-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,11 @@
this._currentTime = finalTime;
}

flush(limit: number = 20, flushPeriodic = false): number {
flush(limit = 20, flushPeriodic = false, tick?: (elapsed: number) => void): number {
const startTime = this._currentTime;
let lastCurrentTime = this._currentTime;
let count = 0;
let seenTimers: number[] = [];
const seenTimers: number[] = [];
while (this._schedulerQueue.length > 0) {
count++;
if (count > limit) {
Expand All @@ -120,12 +121,17 @@
break;
}
}
let current = this._schedulerQueue.shift();
const current = this._schedulerQueue.shift();
if (seenTimers.indexOf(current.id) === -1) {
seenTimers.push(current.id);
}
lastCurrentTime = this._currentTime;
this._currentTime = current.endTime;
let retval = current.func.apply(global, current.args);
if (tick) {
// Tick any secondary schedulers like Jasmine mock Date.
tick(this._currentTime - lastCurrentTime);
}
const retval = current.func.apply(global, current.args);
if (!retval) {
// Uncaught exception in the current scheduled function. Stop processing the queue.
break;
Expand Down Expand Up @@ -271,10 +277,10 @@
flushErrors();
}

flush(limit?: number, flushPeriodic?: boolean): number {
flush(limit?: number, flushPeriodic?: boolean, tick?: (elapsed:number) => void): number {
FakeAsyncTestZoneSpec.assertInZone();
this.flushMicrotasks();
let elapsed = this._scheduler.flush(limit, flushPeriodic);
const elapsed = this._scheduler.flush(limit, flushPeriodic, tick);
if (this._lastError !== null) {
this._resetLastErrorAndThrow();
}
Expand Down

0 comments on commit 06c2e90

Please sign in to comment.