Skip to content

Commit bd2f2c7

Browse files
committed
Merge pull request NativeScript#1877 from NativeScript/hdeshev/zone-aware
Zone-aware versions of certain APIs: setTimeout/setInterval mostly.
2 parents f0cfd11 + 437e220 commit bd2f2c7

4 files changed

Lines changed: 21 additions & 7 deletions

File tree

declarations.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ declare function setInterval(callback: Function, milliseconds?: number): number;
146146
*/
147147
declare function clearInterval(id: number): void;
148148

149+
//@private
150+
declare function zonedCallback(callback: Function): Function;
151+
//@endprivate
152+
149153
declare class WeakRef<T> {
150154
constructor(obj: T);
151155
get(): T;

globals/globals.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ global.loadModule = function(name: string): any {
2929
}
3030
}
3131

32+
global.zonedCallback = function(callback: Function): Function {
33+
if (global.zone) {
34+
return global.zone.bind(callback);
35+
} else {
36+
return callback;
37+
}
38+
}
39+
3240
global.registerModule("timer", () => require("timer"));
3341
global.registerModule("ui/dialogs", () => require("ui/dialogs"));
3442
global.registerModule("xhr", () => require("xhr"));

timer/timer.android.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ function createHandlerAndGetId(): number {
1515
}
1616

1717
export function setTimeout(callback: Function, milliseconds = 0): number {
18-
var id = createHandlerAndGetId();
18+
const id = createHandlerAndGetId();
19+
const zoneBound = zonedCallback(callback);
1920

2021
var runnable = new java.lang.Runnable({
2122
run: () => {
22-
callback();
23+
zoneBound();
2324

2425
if (timeoutCallbacks[id]) {
2526
delete timeoutCallbacks[id];
@@ -44,12 +45,13 @@ export function clearTimeout(id: number): void {
4445
}
4546

4647
export function setInterval(callback: Function, milliseconds = 0): number {
47-
var id = createHandlerAndGetId();
48-
var handler = timeoutHandler;
48+
const id = createHandlerAndGetId();
49+
const handler = timeoutHandler;
50+
const zoneBound = zonedCallback(callback);
4951

5052
var runnable = new java.lang.Runnable({
5153
run: () => {
52-
callback();
54+
zoneBound();
5355
if (timeoutCallbacks[id]) {
5456
handler.postDelayed(runnable, long(milliseconds));
5557
}

timer/timer.ios.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function createTimerAndGetId(callback: Function, milliseconds: number, shouldRep
4040
}
4141

4242
export function setTimeout(callback: Function, milliseconds = 0): number {
43-
return createTimerAndGetId(callback, milliseconds, false);
43+
return createTimerAndGetId(zonedCallback(callback), milliseconds, false);
4444
}
4545

4646
export function clearTimeout(id: number): void {
@@ -51,7 +51,7 @@ export function clearTimeout(id: number): void {
5151
}
5252

5353
export function setInterval(callback: Function, milliseconds = 0): number {
54-
return createTimerAndGetId(callback, milliseconds, true);
54+
return createTimerAndGetId(zonedCallback(callback), milliseconds, true);
5555
}
5656

5757
export var clearInterval = clearTimeout;

0 commit comments

Comments
 (0)