definitions fixed

This commit is contained in:
Erjan Gavalji
2015-03-03 10:34:40 +02:00
parent a03ce4ca1d
commit cc829e0152
705 changed files with 321431 additions and 148812 deletions

View File

@@ -1,2 +0,0 @@
declare var module, require;
module.exports = require("timer/timer");

2
timer/package.json Normal file
View File

@@ -0,0 +1,2 @@
{ "name" : "timer",
"main" : "timer.js" }

View File

@@ -1,6 +1,6 @@
/**
* Android specific timer functions implementation.
*/
* Android specific timer functions implementation.
*/
var timeoutHandler;
var timeoutCallbacks = {};
@@ -18,7 +18,10 @@ export function setTimeout(callback: Function, milliseconds = 0): number {
var runnable = new java.lang.Runnable({
run: () => {
callback();
timeoutCallbacks[id] = null;
if (timeoutCallbacks && timeoutCallbacks[id]) {
timeoutCallbacks[id] = null;
}
}
});
@@ -40,11 +43,12 @@ export function clearTimeout(id: number): void {
export function setInterval(callback: Function, milliseconds = 0): number {
var id = createHadlerAndGetId();
var handler = timeoutHandler;
var runnable = new java.lang.Runnable({
run: () => {
callback();
timeoutHandler.postDelayed(runnable, long(milliseconds));
handler.postDelayed(runnable, long(milliseconds));
}
});

47
timer/timer.d.ts vendored
View File

@@ -1,25 +1,30 @@
/**
* Calls a function after a specified delay.
* @param callback The function to be called.
* @param milliseconds The time to wait before the function is called. Defaults to 0.
*/
export declare function setTimeout(callback: Function, milliseconds?: number): number;
* Allows you to create, start, stop and react to timers.
*/
declare module "timer" {
/**
* Calls a function after a specified delay.
* @param callback The function to be called.
* @param milliseconds The time to wait before the function is called. Defaults to 0.
*/
export function setTimeout(callback: Function, milliseconds?: number): number;
/**
* Clears the delay set by a call to the setTimeout function.
* @param id The identifier returned by the previously called setTimeout() method.
*/
export declare function clearTimeout(id: number): void;
/**
* Clears the delay set by a call to the setTimeout function.
* @param id The identifier returned by the previously called setTimeout() method.
*/
export function clearTimeout(id: number): void;
/**
* Calls a function repeatedly with a delay between each call.
* @param callback The function to be called.
* @param milliseconds The delay between each function call.
*/
export declare function setInterval(callback: Function, milliseconds?: number): number;
/**
* Calls a function repeatedly with a delay between each call.
* @param callback The function to be called.
* @param milliseconds The delay between each function call.
*/
export function setInterval(callback: Function, milliseconds?: number): number;
/**
* Clears repeated function which was set up by calling setInterval().
* @param id The identifier returned by the setInterval() method.
*/
export declare function clearInterval(id: number): void;
/**
* Clears repeated function which was set up by calling setInterval().
* @param id The identifier returned by the setInterval() method.
*/
export function clearInterval(id: number): void;
}

View File

@@ -1,13 +1,34 @@
/**
* iOS specific timer functions implementation.
*/
* iOS specific timer functions implementation.
*/
var timeoutCallbacks = {};
class TimerTargetImpl extends NSObject {
static new(): TimerTargetImpl {
return <TimerTargetImpl>super.new();
}
private _callback: Function;
public initWithCallback(callback: Function): TimerTargetImpl {
this._callback = callback;
return this;
}
public tick(timer): void {
this._callback();
}
public static ObjCExposedMethods = {
"tick": { returns: interop.types.void, params: [NSTimer] }
};
}
function createTimerAndGetId(callback: Function, milliseconds: number, shouldRepeat: boolean): number {
var id = new Date().getUTCMilliseconds();
var target = Foundation.NSObject.extends({ tick: (timer) => { callback(); } }, { exposedMethods: { "tick:": "v@:@" } });
var timer = Foundation.NSTimer.scheduledTimerWithTimeIntervalTargetSelectorUserInfoRepeats(milliseconds / 1000, new target(), "tick:", null, shouldRepeat);
var timerTarget = TimerTargetImpl.new().initWithCallback(callback);
var timer = NSTimer.scheduledTimerWithTimeIntervalTargetSelectorUserInfoRepeats(milliseconds / 1000, timerTarget, "tick", null, shouldRepeat);
if (!timeoutCallbacks[id]) {
timeoutCallbacks[id] = timer;