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

Commit 03d19f9

Browse files
JiaLiPassionmhevery
authored andcommitted
fix #551, add toJSON to ZoneTask to prevent cyclic error (#576)
1 parent 41f5306 commit 03d19f9

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

lib/zone.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,22 @@ const Zone: ZoneType = (function(global: any) {
962962
return Object.prototype.toString.call(this);
963963
}
964964
}
965+
966+
// add toJSON method to prevent cyclic error when
967+
// call JSON.stringify(zoneTask)
968+
public toJSON() {
969+
return {
970+
type: this.type,
971+
source: this.source,
972+
data: this.data,
973+
zone: this.zone.name,
974+
invoke: this.invoke,
975+
scheduleFn: this.scheduleFn,
976+
cancelFn: this.cancelFn,
977+
runCount: this.runCount,
978+
callback: this.callback
979+
};
980+
}
965981
}
966982

967983
interface UncaughtPromiseError extends Error {

test/common/zone.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,22 @@ describe('Zone', function() {
231231

232232
macro.invoke();
233233
});
234+
235+
it('should convert task to json without cyclic error', () => {
236+
const z = Zone.current;
237+
const event = z.scheduleEventTask('test', () => {}, null, noop, noop);
238+
const micro = z.scheduleMicroTask('test', () => {});
239+
const macro = z.scheduleMacroTask('test', () => {}, null, noop, noop);
240+
expect(function() {
241+
JSON.stringify(event);
242+
}).not.toThrow();
243+
expect(function() {
244+
JSON.stringify(micro);
245+
}).not.toThrow();
246+
expect(function() {
247+
JSON.stringify(macro);
248+
}).not.toThrow();
249+
});
234250
});
235251
});
236252

0 commit comments

Comments
 (0)