Skip to content

Commit 8a68669

Browse files
committed
refactor(zone.js): rename several internal apis in fake async zone spec
In `FakeAsyncZoneSpec`, there are several variables and APIs to identify different times, and the names are confusing, in this commit, they are renamed for more clear understandings. 1. currentTickTime, the tick millis advanced. 2. getFakeBaseSystemTime(), return the fake base system time. 3. setFakeBaseSystemTime(), set the fake base system time. 4. getRealSystemTime(), get the underlying native system time.
1 parent 59d6f97 commit 8a68669

File tree

4 files changed

+51
-43
lines changed

4 files changed

+51
-43
lines changed

packages/zone.js/lib/jasmine/jasmine.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ Zone.__load_patch('jasmine', (global: any, Zone: ZoneType, api: _ZonePrivate) =>
127127
const fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
128128
if (fakeAsyncZoneSpec) {
129129
const dateTime = arguments.length > 0 ? arguments[0] : new Date();
130-
return fakeAsyncZoneSpec.setCurrentRealTime.apply(
130+
return fakeAsyncZoneSpec.setFakeBaseSystemTime.apply(
131131
fakeAsyncZoneSpec,
132132
dateTime && typeof dateTime.getTime === 'function' ? [dateTime.getTime()] :
133133
arguments);

packages/zone.js/lib/jest/jest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ Zone.__load_patch('jest', (context: any, Zone: ZoneType, api: _ZonePrivate) => {
188188
return function(self: any, args: any[]) {
189189
const fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
190190
if (fakeAsyncZoneSpec && isPatchingFakeTimer()) {
191-
fakeAsyncZoneSpec.setCurrentRealTime(args[0]);
191+
fakeAsyncZoneSpec.setFakeBaseSystemTime(args[0]);
192192
} else {
193193
return delegate.apply(self, args);
194194
}

packages/zone.js/lib/zone-spec/fake-async-test.ts

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,15 @@ function FakeDate() {
4545
}
4646
}
4747

48-
FakeDate.now =
49-
function(this: unknown) {
48+
FakeDate.now = function(this: unknown) {
5049
const fakeAsyncTestZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
5150
if (fakeAsyncTestZoneSpec) {
52-
return fakeAsyncTestZoneSpec.getCurrentRealTime() + fakeAsyncTestZoneSpec.getCurrentTime();
51+
return fakeAsyncTestZoneSpec.getFakeSystemTime();
5352
}
5453
return OriginalDate.now.apply(this, arguments);
55-
}
54+
};
5655

57-
FakeDate.UTC = OriginalDate.UTC;
56+
FakeDate.UTC = OriginalDate.UTC;
5857
FakeDate.parse = OriginalDate.parse;
5958

6059
// keep a reference for zone patched timer function
@@ -72,24 +71,24 @@ class Scheduler {
7271
// Scheduler queue with the tuple of end time and callback function - sorted by end time.
7372
private _schedulerQueue: ScheduledFunction[] = [];
7473
// Current simulated time in millis.
75-
private _currentTime: number = 0;
76-
// Current real time in millis.
77-
private _currentRealTime: number = OriginalDate.now();
74+
private _currentTickTime: number = 0;
75+
// Current fake system base time in millis.
76+
private _currentFakeBaseSystemTime: number = OriginalDate.now();
7877
// track requeuePeriodicTimer
7978
private _currentTickRequeuePeriodicEntries: any[] = [];
8079

8180
constructor() {}
8281

83-
getCurrentTime() {
84-
return this._currentTime;
82+
getCurrentTickTime() {
83+
return this._currentTickTime;
8584
}
8685

87-
getCurrentRealTime() {
88-
return this._currentRealTime;
86+
getFakeSystemTime() {
87+
return this._currentFakeBaseSystemTime + this._currentTickTime;
8988
}
9089

91-
setCurrentRealTime(realTime: number) {
92-
this._currentRealTime = realTime;
90+
setFakeBaseSystemTime(fakeBaseSystemTime: number) {
91+
this._currentFakeBaseSystemTime = fakeBaseSystemTime;
9392
}
9493

9594
getRealSystemTime() {
@@ -114,7 +113,7 @@ class Scheduler {
114113
...options
115114
};
116115
let currentId = options.id! < 0 ? Scheduler.nextId++ : options.id!;
117-
let endTime = this._currentTime + delay;
116+
let endTime = this._currentTickTime + delay;
118117

119118
// Insert so that scheduler queue remains sorted by end time.
120119
let newEntry: ScheduledFunction = {
@@ -165,15 +164,15 @@ class Scheduler {
165164
}
166165
// Find the last task currently queued in the scheduler queue and tick
167166
// till that time.
168-
const startTime = this._currentTime;
167+
const startTime = this._currentTickTime;
169168
const targetTask = this._schedulerQueue[step - 1];
170169
this.tick(targetTask.endTime - startTime, doTick, tickOptions);
171170
}
172171

173172
tick(millis: number = 0, doTick?: (elapsed: number) => void, tickOptions?: {
174173
processNewMacroTasksSynchronously: boolean
175174
}): void {
176-
let finalTime = this._currentTime + millis;
175+
let finalTime = this._currentTickTime + millis;
177176
let lastCurrentTime = 0;
178177
tickOptions = Object.assign({processNewMacroTasksSynchronously: true}, tickOptions);
179178
// we need to copy the schedulerQueue so nested timeout
@@ -202,13 +201,13 @@ class Scheduler {
202201
this._schedulerQueue.splice(idx, 1);
203202
}
204203
}
205-
lastCurrentTime = this._currentTime;
206-
this._currentTime = current.endTime;
204+
lastCurrentTime = this._currentTickTime;
205+
this._currentTickTime = current.endTime;
207206
if (doTick) {
208-
doTick(this._currentTime - lastCurrentTime);
207+
doTick(this._currentTickTime - lastCurrentTime);
209208
}
210209
let retval = current.func.apply(
211-
global, current.isRequestAnimationFrame ? [this._currentTime] : current.args);
210+
global, current.isRequestAnimationFrame ? [this._currentTickTime] : current.args);
212211
if (!retval) {
213212
// Uncaught exception in the current scheduled function. Stop processing the queue.
214213
break;
@@ -230,10 +229,10 @@ class Scheduler {
230229
}
231230
}
232231
}
233-
lastCurrentTime = this._currentTime;
234-
this._currentTime = finalTime;
232+
lastCurrentTime = this._currentTickTime;
233+
this._currentTickTime = finalTime;
235234
if (doTick) {
236-
doTick(this._currentTime - lastCurrentTime);
235+
doTick(this._currentTickTime - lastCurrentTime);
237236
}
238237
}
239238

@@ -243,10 +242,10 @@ class Scheduler {
243242
}
244243
// Find the last task currently queued in the scheduler queue and tick
245244
// till that time.
246-
const startTime = this._currentTime;
245+
const startTime = this._currentTickTime;
247246
const lastTask = this._schedulerQueue[this._schedulerQueue.length - 1];
248247
this.tick(lastTask.endTime - startTime, doTick, {processNewMacroTasksSynchronously: false});
249-
return this._currentTime - startTime;
248+
return this._currentTickTime - startTime;
250249
}
251250

252251
flush(limit = 20, flushPeriodic = false, doTick?: (elapsed: number) => void): number {
@@ -263,14 +262,14 @@ class Scheduler {
263262
}
264263
// Find the last task currently queued in the scheduler queue and tick
265264
// till that time.
266-
const startTime = this._currentTime;
265+
const startTime = this._currentTickTime;
267266
const lastTask = this._schedulerQueue[this._schedulerQueue.length - 1];
268267
this.tick(lastTask.endTime - startTime, doTick);
269-
return this._currentTime - startTime;
268+
return this._currentTickTime - startTime;
270269
}
271270

272271
private flushNonPeriodic(limit: number, doTick?: (elapsed: number) => void): number {
273-
const startTime = this._currentTime;
272+
const startTime = this._currentTickTime;
274273
let lastCurrentTime = 0;
275274
let count = 0;
276275
while (this._schedulerQueue.length > 0) {
@@ -289,19 +288,19 @@ class Scheduler {
289288
}
290289

291290
const current = this._schedulerQueue.shift()!;
292-
lastCurrentTime = this._currentTime;
293-
this._currentTime = current.endTime;
291+
lastCurrentTime = this._currentTickTime;
292+
this._currentTickTime = current.endTime;
294293
if (doTick) {
295294
// Update any secondary schedulers like Jasmine mock Date.
296-
doTick(this._currentTime - lastCurrentTime);
295+
doTick(this._currentTickTime - lastCurrentTime);
297296
}
298297
const retval = current.func.apply(global, current.args);
299298
if (!retval) {
300299
// Uncaught exception in the current scheduled function. Stop processing the queue.
301300
break;
302301
}
303302
}
304-
return this._currentTime - startTime;
303+
return this._currentTickTime - startTime;
305304
}
306305
}
307306

@@ -426,16 +425,16 @@ class FakeAsyncTestZoneSpec implements ZoneSpec {
426425
throw error;
427426
}
428427

429-
getCurrentTime() {
430-
return this._scheduler.getCurrentTime();
428+
getCurrentTickTime() {
429+
return this._scheduler.getCurrentTickTime();
431430
}
432431

433-
getCurrentRealTime() {
434-
return this._scheduler.getCurrentRealTime();
432+
getFakeSystemTime() {
433+
return this._scheduler.getFakeSystemTime();
435434
}
436435

437-
setCurrentRealTime(realTime: number) {
438-
this._scheduler.setCurrentRealTime(realTime);
436+
setFakeBaseSystemTime(realTime: number) {
437+
this._scheduler.setFakeBaseSystemTime(realTime);
439438
}
440439

441440
getRealSystemTime() {

packages/zone.js/test/jest/jest.spec.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,23 @@ describe('jest modern fakeTimers with zone.js fakeAsync', () => {
125125
expect(typeof fakeAsyncZoneSpec.tick).toEqual('function');
126126
});
127127

128-
test('setSystemTime should set FakeDate.currentRealTime', () => {
128+
test('setSystemTime should set FakeDate.currentFakeTime', () => {
129129
const fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
130-
const d = Date.now();
130+
let d = fakeAsyncZoneSpec.getRealSystemTime();
131131
jest.setSystemTime(d);
132132
expect(Date.now()).toEqual(d);
133133
for (let i = 0; i < 100000; i++) {
134134
}
135135
expect(fakeAsyncZoneSpec.getRealSystemTime()).not.toEqual(d);
136+
d = fakeAsyncZoneSpec.getRealSystemTime();
137+
let timeoutTriggered = false;
138+
setTimeout(() => {
139+
timeoutTriggered = true;
140+
}, 100);
141+
jest.setSystemTime(d);
142+
tick(100);
143+
expect(timeoutTriggered).toBe(true);
144+
expect(Date.now()).toEqual(d + 100);
136145
});
137146

138147
test('runAllTicks should run all microTasks', () => {

0 commit comments

Comments
 (0)