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

Commit

Permalink
fix #551, add toJSON to ZoneTask to prevent cyclic error (#576)
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaLiPassion authored and mhevery committed Jan 12, 2017
1 parent 41f5306 commit 03d19f9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/zone.ts
Expand Up @@ -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 {
Expand Down
16 changes: 16 additions & 0 deletions test/common/zone.spec.ts
Expand Up @@ -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();
});
});
});

Expand Down

0 comments on commit 03d19f9

Please sign in to comment.