From 437e220c42917cf7028d0ecc60a5d2cefa616c4a Mon Sep 17 00:00:00 2001 From: Hristo Deshev Date: Thu, 31 Mar 2016 18:29:42 +0300 Subject: [PATCH] Zone-aware versions of certain APIs: setTimeout/setInterval mostly. Instead of waiting for zone.js to patch our global declarations and break the lazy module loading optimizations there, we'll let it patch stuff before "globals" gets loaded, so that it doesn't touch (and break) the code there. Of course, that requires that functions that need legit patching need to be made aware of the zone, or get patched later on. The global.zonedCallback function makes that easy, and we use it for setTimeout/setInterval. --- declarations.d.ts | 4 ++++ globals/globals.ts | 8 ++++++++ timer/timer.android.ts | 12 +++++++----- timer/timer.ios.ts | 4 ++-- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/declarations.d.ts b/declarations.d.ts index 00eef4ad2..a97d8e50a 100644 --- a/declarations.d.ts +++ b/declarations.d.ts @@ -146,6 +146,10 @@ declare function setInterval(callback: Function, milliseconds?: number): number; */ declare function clearInterval(id: number): void; +//@private +declare function zonedCallback(callback: Function): Function; +//@endprivate + declare class WeakRef { constructor(obj: T); get(): T; diff --git a/globals/globals.ts b/globals/globals.ts index a166615e2..4fea68af0 100644 --- a/globals/globals.ts +++ b/globals/globals.ts @@ -29,6 +29,14 @@ global.loadModule = function(name: string): any { } } +global.zonedCallback = function(callback: Function): Function { + if (global.zone) { + return global.zone.bind(callback); + } else { + return callback; + } +} + global.registerModule("timer", () => require("timer")); global.registerModule("ui/dialogs", () => require("ui/dialogs")); global.registerModule("xhr", () => require("xhr")); diff --git a/timer/timer.android.ts b/timer/timer.android.ts index 8b2dc7d3d..5e362d083 100644 --- a/timer/timer.android.ts +++ b/timer/timer.android.ts @@ -15,11 +15,12 @@ function createHandlerAndGetId(): number { } export function setTimeout(callback: Function, milliseconds = 0): number { - var id = createHandlerAndGetId(); + const id = createHandlerAndGetId(); + const zoneBound = zonedCallback(callback); var runnable = new java.lang.Runnable({ run: () => { - callback(); + zoneBound(); if (timeoutCallbacks[id]) { delete timeoutCallbacks[id]; @@ -44,12 +45,13 @@ export function clearTimeout(id: number): void { } export function setInterval(callback: Function, milliseconds = 0): number { - var id = createHandlerAndGetId(); - var handler = timeoutHandler; + const id = createHandlerAndGetId(); + const handler = timeoutHandler; + const zoneBound = zonedCallback(callback); var runnable = new java.lang.Runnable({ run: () => { - callback(); + zoneBound(); if (timeoutCallbacks[id]) { handler.postDelayed(runnable, long(milliseconds)); } diff --git a/timer/timer.ios.ts b/timer/timer.ios.ts index aadd91b32..d97d4fc04 100644 --- a/timer/timer.ios.ts +++ b/timer/timer.ios.ts @@ -40,7 +40,7 @@ function createTimerAndGetId(callback: Function, milliseconds: number, shouldRep } export function setTimeout(callback: Function, milliseconds = 0): number { - return createTimerAndGetId(callback, milliseconds, false); + return createTimerAndGetId(zonedCallback(callback), milliseconds, false); } export function clearTimeout(id: number): void { @@ -51,7 +51,7 @@ export function clearTimeout(id: number): void { } export function setInterval(callback: Function, milliseconds = 0): number { - return createTimerAndGetId(callback, milliseconds, true); + return createTimerAndGetId(zonedCallback(callback), milliseconds, true); } export var clearInterval = clearTimeout;