From 57098ee31749a32a98d0aa8ee39cf82179901b68 Mon Sep 17 00:00:00 2001 From: Hamidreza Jahtalab Date: Tue, 12 Jul 2016 13:08:08 +0200 Subject: [PATCH] feat: return timeout Id in ZoneTask.toString (fixes #341) --- dist/zone.js.d.ts | 4 ++++ lib/zone.ts | 13 +++++++++++++ test/common/setTimeout.spec.ts | 6 ++++++ 3 files changed, 23 insertions(+) diff --git a/dist/zone.js.d.ts b/dist/zone.js.d.ts index 93c2aa39a..f31ed6dde 100644 --- a/dist/zone.js.d.ts +++ b/dist/zone.js.d.ts @@ -372,6 +372,10 @@ interface TaskData { * Delay in milliseconds when the Task will run. */ delay?: number; + /** + * identifier returned by the native setTimeout. + */ + handleId?: number; } /** * Represents work which is executed with a clean stack. diff --git a/lib/zone.ts b/lib/zone.ts index c7cbe663b..560579e69 100644 --- a/lib/zone.ts +++ b/lib/zone.ts @@ -395,6 +395,11 @@ interface TaskData { * Delay in milliseconds when the Task will run. */ delay?: number; + + /** + * identifier returned by the native setTimeout. + */ + handleId?: number; } /** @@ -819,6 +824,14 @@ const Zone: ZoneType = (function(global: any) { } }; } + + public toString() { + if (this.data && typeof this.data.handleId !== 'undefined') { + return this.data.handleId; + } else { + return this.toString(); + } + } } interface UncaughtPromiseError extends Error { diff --git a/test/common/setTimeout.spec.ts b/test/common/setTimeout.spec.ts index 213eb390a..0119eea62 100644 --- a/test/common/setTimeout.spec.ts +++ b/test/common/setTimeout.spec.ts @@ -88,6 +88,12 @@ describe('setTimeout', function () { }); }); + it('should return the timeout Id through toString', function () { + var cancelId = setTimeout(() => { + }, 0); + expect(typeof (cancelId.toString())).toBe('number'); + }) + it('should pass invalid values through', function () { clearTimeout(null); clearTimeout({});