chore: cleanup

This commit is contained in:
Nathan Walker
2020-07-06 14:25:11 -07:00
parent 94cf5c4706
commit 6336eac4b5
477 changed files with 59087 additions and 74961 deletions

View File

@@ -6,83 +6,75 @@ const timeoutCallbacks = {};
let timerId = 0;
function createHandlerAndGetId(): number {
if (!timeoutHandler) {
timeoutHandler = new android.os.Handler(android.os.Looper.myLooper());
}
if (!timeoutHandler) {
timeoutHandler = new android.os.Handler(android.os.Looper.myLooper());
}
timerId++;
timerId++;
return timerId;
return timerId;
}
export function setTimeout(
callback: Function,
milliseconds = 0,
...args
): number {
// Cast to Number
milliseconds += 0;
export function setTimeout(callback: Function, milliseconds = 0, ...args): number {
// Cast to Number
milliseconds += 0;
const id = createHandlerAndGetId();
const invoke = () => callback(...args);
const zoneBound = zonedCallback(invoke);
const id = createHandlerAndGetId();
const invoke = () => callback(...args);
const zoneBound = zonedCallback(invoke);
const runnable = new java.lang.Runnable({
run: () => {
zoneBound();
const runnable = new java.lang.Runnable({
run: () => {
zoneBound();
if (timeoutCallbacks[id]) {
delete timeoutCallbacks[id];
}
},
});
if (timeoutCallbacks[id]) {
delete timeoutCallbacks[id];
}
},
});
if (!timeoutCallbacks[id]) {
timeoutCallbacks[id] = runnable;
}
if (!timeoutCallbacks[id]) {
timeoutCallbacks[id] = runnable;
}
timeoutHandler.postDelayed(runnable, long(milliseconds));
timeoutHandler.postDelayed(runnable, long(milliseconds));
return id;
return id;
}
export function clearTimeout(id: number): void {
let index = id;
if (timeoutCallbacks[index]) {
timeoutHandler.removeCallbacks(timeoutCallbacks[index]);
delete timeoutCallbacks[index];
}
let index = id;
if (timeoutCallbacks[index]) {
timeoutHandler.removeCallbacks(timeoutCallbacks[index]);
delete timeoutCallbacks[index];
}
}
export function setInterval(
callback: Function,
milliseconds = 0,
...args
): number {
// Cast to Number
milliseconds += 0;
export function setInterval(callback: Function, milliseconds = 0, ...args): number {
// Cast to Number
milliseconds += 0;
const id = createHandlerAndGetId();
const handler = timeoutHandler;
const invoke = () => callback(...args);
const zoneBound = zonedCallback(invoke);
const id = createHandlerAndGetId();
const handler = timeoutHandler;
const invoke = () => callback(...args);
const zoneBound = zonedCallback(invoke);
const runnable = new java.lang.Runnable({
run: () => {
zoneBound();
if (timeoutCallbacks[id]) {
handler.postDelayed(runnable, long(milliseconds));
}
},
});
const runnable = new java.lang.Runnable({
run: () => {
zoneBound();
if (timeoutCallbacks[id]) {
handler.postDelayed(runnable, long(milliseconds));
}
},
});
if (!timeoutCallbacks[id]) {
timeoutCallbacks[id] = runnable;
}
if (!timeoutCallbacks[id]) {
timeoutCallbacks[id] = runnable;
}
timeoutHandler.postDelayed(runnable, long(milliseconds));
timeoutHandler.postDelayed(runnable, long(milliseconds));
return id;
return id;
}
export const clearInterval = clearTimeout;

View File

@@ -4,11 +4,7 @@
* @param milliseconds The time to wait before the function is called. Defaults to 0.
* @param args One or more parameter to use once the function is called. Defaults to no parameters.
*/
export function setTimeout(
callback: Function,
milliseconds?: number,
...args: any[]
): number;
export function setTimeout(callback: Function, milliseconds?: number, ...args: any[]): number;
/**
* Clears the delay set by a call to the setTimeout function.
@@ -22,11 +18,7 @@ export function clearTimeout(id: number): void;
* @param milliseconds The delay between each function call.
* @param args One or more parameter to use once the function is called. Defaults to no parameters.
*/
export function setInterval(
callback: Function,
milliseconds?: number,
...args: any[]
): number;
export function setInterval(callback: Function, milliseconds?: number, ...args: any[]): number;
/**
* Clears repeated function which was set up by calling setInterval().

View File

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