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

Commit afa1363

Browse files
JiaLiPassionmhevery
authored andcommitted
fix(core): fix #1153, ZoneTask.toString should always be a string (#1166)
1 parent 33a0ad6 commit afa1363

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

Diff for: lib/zone.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,7 @@ const Zone: ZoneType = (function(global: any) {
12401240

12411241
public toString() {
12421242
if (this.data && typeof this.data.handleId !== 'undefined') {
1243-
return this.data.handleId;
1243+
return this.data.handleId.toString();
12441244
} else {
12451245
return Object.prototype.toString.call(this);
12461246
}

Diff for: test/common/toString.spec.ts

+40
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,43 @@ describe('global function patch', () => {
4747
}));
4848
});
4949
});
50+
51+
describe('ZoneTask', () => {
52+
it('should return handleId.toString if handleId is available', () => {
53+
let macroTask1: any = undefined;
54+
let macroTask2: any = undefined;
55+
let microTask: any = undefined;
56+
const zone = Zone.current.fork({
57+
name: 'timer',
58+
onScheduleTask: (delegate: ZoneDelegate, curr: Zone, target: Zone, task: Task) => {
59+
if (task.type === 'macroTask') {
60+
if (!macroTask1) {
61+
macroTask1 = task;
62+
} else {
63+
macroTask2 = task;
64+
}
65+
} else if (task.type === 'microTask') {
66+
microTask = task;
67+
}
68+
return task;
69+
}
70+
});
71+
zone.run(() => {
72+
const id1 = setTimeout(() => {});
73+
clearTimeout(id1);
74+
const id2 = setTimeout(() => {});
75+
clearTimeout(id2);
76+
Promise.resolve().then(() => {});
77+
const macroTask1Str = macroTask1.toString();
78+
const macroTask2Str = macroTask2.toString();
79+
expect(typeof macroTask1Str).toEqual('string');
80+
expect(macroTask1Str).toEqual(id1.toString());
81+
expect(typeof macroTask2Str).toEqual('string');
82+
expect(macroTask2Str).toEqual(id2.toString());
83+
if (macroTask1.data && typeof macroTask1.data.handleId === 'number') {
84+
expect(macroTask1Str).not.toEqual(macroTask2Str);
85+
}
86+
expect(typeof microTask.toString()).toEqual('string');
87+
});
88+
});
89+
});

0 commit comments

Comments
 (0)