From 03d19f9f1724ecbe49dd62fef331fe5f6c551a68 Mon Sep 17 00:00:00 2001 From: JiaLiPassion Date: Thu, 12 Jan 2017 09:14:09 +0900 Subject: [PATCH] fix #551, add toJSON to ZoneTask to prevent cyclic error (#576) --- lib/zone.ts | 16 ++++++++++++++++ test/common/zone.spec.ts | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/lib/zone.ts b/lib/zone.ts index 7434a6a80..522b56fd3 100644 --- a/lib/zone.ts +++ b/lib/zone.ts @@ -962,6 +962,22 @@ const Zone: ZoneType = (function(global: any) { return Object.prototype.toString.call(this); } } + + // add toJSON method to prevent cyclic error when + // call JSON.stringify(zoneTask) + public toJSON() { + return { + type: this.type, + source: this.source, + data: this.data, + zone: this.zone.name, + invoke: this.invoke, + scheduleFn: this.scheduleFn, + cancelFn: this.cancelFn, + runCount: this.runCount, + callback: this.callback + }; + } } interface UncaughtPromiseError extends Error { diff --git a/test/common/zone.spec.ts b/test/common/zone.spec.ts index c93b4b856..258f100ee 100644 --- a/test/common/zone.spec.ts +++ b/test/common/zone.spec.ts @@ -231,6 +231,22 @@ describe('Zone', function() { macro.invoke(); }); + + it('should convert task to json without cyclic error', () => { + const z = Zone.current; + const event = z.scheduleEventTask('test', () => {}, null, noop, noop); + const micro = z.scheduleMicroTask('test', () => {}); + const macro = z.scheduleMacroTask('test', () => {}, null, noop, noop); + expect(function() { + JSON.stringify(event); + }).not.toThrow(); + expect(function() { + JSON.stringify(micro); + }).not.toThrow(); + expect(function() { + JSON.stringify(macro); + }).not.toThrow(); + }); }); });