mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
Merge pull request #1877 from NativeScript/hdeshev/zone-aware
Zone-aware versions of certain APIs: setTimeout/setInterval mostly.
This commit is contained in:
4
declarations.d.ts
vendored
4
declarations.d.ts
vendored
@@ -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<T> {
|
||||
constructor(obj: T);
|
||||
get(): T;
|
||||
|
||||
@@ -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"));
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user