mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-18 05:18:39 +08:00

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.
82 lines
2.4 KiB
TypeScript
82 lines
2.4 KiB
TypeScript
import * as utils from "utils/utils";
|
|
|
|
//iOS specific timer functions implementation.
|
|
var timeoutCallbacks = new Map<number, KeyValuePair<NSTimer, TimerTargetImpl>>();
|
|
var timerId = 0;
|
|
|
|
interface KeyValuePair<K, V> {
|
|
k: K;
|
|
v: V
|
|
}
|
|
|
|
class TimerTargetImpl extends NSObject {
|
|
private callback: Function;
|
|
private disposed: boolean;
|
|
private id: number
|
|
private shouldRepeat: boolean
|
|
|
|
public static initWithCallback(callback: Function, id: number, shouldRepeat: boolean): TimerTargetImpl {
|
|
let handler = <TimerTargetImpl>TimerTargetImpl.new();
|
|
handler.callback = callback;
|
|
handler.id = id;
|
|
handler.shouldRepeat = shouldRepeat;
|
|
return handler;
|
|
}
|
|
|
|
public tick(timer): void {
|
|
if (!this.disposed) {
|
|
this.callback();
|
|
}
|
|
|
|
if (!this.shouldRepeat) {
|
|
this.unregister();
|
|
}
|
|
}
|
|
|
|
public unregister() {
|
|
if (!this.disposed) {
|
|
this.disposed = true;
|
|
|
|
let timer = timeoutCallbacks.get(this.id).k;
|
|
timer.invalidate();
|
|
timeoutCallbacks.delete(this.id);
|
|
}
|
|
}
|
|
|
|
public static ObjCExposedMethods = {
|
|
"tick": { returns: interop.types.void, params: [NSTimer] }
|
|
};
|
|
}
|
|
|
|
function createTimerAndGetId(callback: Function, milliseconds: number, shouldRepeat: boolean): number {
|
|
timerId++;
|
|
let id = timerId;
|
|
let timerTarget = TimerTargetImpl.initWithCallback(callback, id, shouldRepeat);
|
|
let timer = NSTimer.scheduledTimerWithTimeIntervalTargetSelectorUserInfoRepeats(milliseconds / 1000, timerTarget, "tick", null, shouldRepeat);
|
|
|
|
// https://github.com/NativeScript/NativeScript/issues/2116
|
|
utils.ios.getter(NSRunLoop, NSRunLoop.currentRunLoop).addTimerForMode(timer, NSRunLoopCommonModes);
|
|
|
|
let pair: KeyValuePair<NSTimer, TimerTargetImpl> = { k: timer, v: timerTarget };
|
|
timeoutCallbacks.set(id, pair);
|
|
|
|
return id;
|
|
}
|
|
|
|
export function setTimeout(callback: Function, milliseconds = 0): number {
|
|
return createTimerAndGetId(zonedCallback(callback), milliseconds, false);
|
|
}
|
|
|
|
export function clearTimeout(id: number): void {
|
|
let pair = timeoutCallbacks.get(<number><any>id);
|
|
if (pair) {
|
|
pair.v.unregister();
|
|
}
|
|
}
|
|
|
|
export function setInterval(callback: Function, milliseconds = 0): number {
|
|
return createTimerAndGetId(zonedCallback(callback), milliseconds, true);
|
|
}
|
|
|
|
export var clearInterval = clearTimeout;
|