Files
Hristo Deshev 86481cce4a Make typings compatible with @types/node.
Fixes name clashes and uses Node-compatible typings where possible.

Changes:
 - setTimout et al now return NodeJS.Timer instead of number
 - No "console" module anymore. Everyone uses it through global.console
 anyway.
 - We have a typed "global" instance with exposed properties now. Any
 "freeform" accesses must go through a `(<any>global).blah` cast.
 - remove tns-core-modules.{base,es6,es2015}.d.ts. Those were needed
 as workarounds for the ES6/DOM/Node type clashes.
2017-02-15 13:01:10 +02:00

72 lines
1.7 KiB
TypeScript

/**
* Android specific timer functions implementation.
*/
var timeoutHandler;
var timeoutCallbacks = {};
var timerId = 0;
function createHandlerAndGetId(): number {
if (!timeoutHandler) {
timeoutHandler = new android.os.Handler(android.os.Looper.myLooper());
}
timerId++;
return timerId;
}
export function setTimeout(callback: Function, milliseconds = 0): number {
const id = createHandlerAndGetId();
const zoneBound = zonedCallback(callback);
var runnable = new java.lang.Runnable({
run: () => {
zoneBound();
if (timeoutCallbacks[id]) {
delete timeoutCallbacks[id];
}
}
});
if (!timeoutCallbacks[id]) {
timeoutCallbacks[id] = runnable;
}
timeoutHandler.postDelayed(runnable, long(milliseconds));
return id;
}
export function clearTimeout(id: number): void {
let index = id;
if (timeoutCallbacks[index]) {
timeoutHandler.removeCallbacks(timeoutCallbacks[index]);
delete timeoutCallbacks[index];
}
}
export function setInterval(callback: Function, milliseconds = 0): number {
const id = createHandlerAndGetId();
const handler = timeoutHandler;
const zoneBound = zonedCallback(callback);
var runnable = new java.lang.Runnable({
run: () => {
zoneBound();
if (timeoutCallbacks[id]) {
handler.postDelayed(runnable, long(milliseconds));
}
}
});
if (!timeoutCallbacks[id]) {
timeoutCallbacks[id] = runnable;
}
timeoutHandler.postDelayed(runnable, long(milliseconds));
return id;
}
export var clearInterval = clearTimeout;