From 5635dd2e151e39dd579092cf265d1b74c4b1d155 Mon Sep 17 00:00:00 2001 From: Kamen Bundev Date: Tue, 23 Jul 2019 10:36:25 +0300 Subject: [PATCH 1/4] Fix setTimeout/setInterval can't be called with boolean period --- tns-core-modules/timer/timer.android.ts | 6 ++++++ tns-core-modules/timer/timer.ios.ts | 3 +++ 2 files changed, 9 insertions(+) diff --git a/tns-core-modules/timer/timer.android.ts b/tns-core-modules/timer/timer.android.ts index 672dfb423f..f18b81567a 100644 --- a/tns-core-modules/timer/timer.android.ts +++ b/tns-core-modules/timer/timer.android.ts @@ -16,6 +16,9 @@ function createHandlerAndGetId(): number { } 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); @@ -48,6 +51,9 @@ export function clearTimeout(id: number): void { } export function setInterval(callback: Function, milliseconds = 0, ...args): number { + // Cast to Number + milliseconds += 0; + const id = createHandlerAndGetId(); const handler = timeoutHandler; const invoke = () => callback(...args); diff --git a/tns-core-modules/timer/timer.ios.ts b/tns-core-modules/timer/timer.ios.ts index 7436cba95f..777c7fa210 100644 --- a/tns-core-modules/timer/timer.ios.ts +++ b/tns-core-modules/timer/timer.ios.ts @@ -48,6 +48,9 @@ class TimerTargetImpl extends NSObject { } 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); From 44e5ed6cdcf26f061b1db28aa795cfe01cbb72fc Mon Sep 17 00:00:00 2001 From: Kamen Bundev Date: Tue, 23 Jul 2019 10:55:59 +0300 Subject: [PATCH 2/4] Add typings and a test --- tests/app/timer/timer-tests.ts | 16 ++++++++++++++++ tns-core-modules/timer/timer.d.ts | 4 ++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/tests/app/timer/timer-tests.ts b/tests/app/timer/timer-tests.ts index c8c8a55d6a..b455729e9a 100644 --- a/tests/app/timer/timer-tests.ts +++ b/tests/app/timer/timer-tests.ts @@ -72,6 +72,22 @@ export function test_setTimeout_callbackCalledAfterSpecifiedTime() { TKUnit.assert(completed, "Callback should be called after the specified time!"); } +export function test_setTimeout_callbackCalledWithBooleanPeriod() { + let completed = false; + + // >> timer-set-false + const id = timer.setTimeout(() => { + // >> (hide) + completed = true; + // << (hide) + }, false); + // << timer-set-false + + TKUnit.waitUntilReady(() => completed, 1); + timer.clearTimeout(id); + TKUnit.assert(completed, "Callback should be called in 0 seconds!"); +} + export function test_setTimeout_callbackNotCalled() { let completed = false; diff --git a/tns-core-modules/timer/timer.d.ts b/tns-core-modules/timer/timer.d.ts index 0f6c9b5aa4..4825af35e4 100644 --- a/tns-core-modules/timer/timer.d.ts +++ b/tns-core-modules/timer/timer.d.ts @@ -9,7 +9,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 | boolean, ...args: any[]): number; /** * Clears the delay set by a call to the setTimeout function. @@ -23,7 +23,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 | boolean, ...args: any[]): number; /** * Clears repeated function which was set up by calling setInterval(). From 643527202a57046a33e207df25ad554b93c911cb Mon Sep 17 00:00:00 2001 From: Kamen Bundev Date: Wed, 24 Jul 2019 16:19:43 +0300 Subject: [PATCH 3/4] Revert typings --- tns-core-modules/timer/timer.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tns-core-modules/timer/timer.d.ts b/tns-core-modules/timer/timer.d.ts index 4825af35e4..0f6c9b5aa4 100644 --- a/tns-core-modules/timer/timer.d.ts +++ b/tns-core-modules/timer/timer.d.ts @@ -9,7 +9,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 | boolean, ...args: any[]): number; +export function setTimeout(callback: Function, milliseconds?: number, ...args: any[]): number; /** * Clears the delay set by a call to the setTimeout function. @@ -23,7 +23,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 | boolean, ...args: any[]): number; +export function setInterval(callback: Function, milliseconds?: number, ...args: any[]): number; /** * Clears repeated function which was set up by calling setInterval(). From 47e2ffe35a7f8008456ec127033f872087c12652 Mon Sep 17 00:00:00 2001 From: Kamen Bundev Date: Wed, 24 Jul 2019 16:44:16 +0300 Subject: [PATCH 4/4] Ignore the wrong type --- tests/app/timer/timer-tests.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/app/timer/timer-tests.ts b/tests/app/timer/timer-tests.ts index b455729e9a..2c6f7997fb 100644 --- a/tests/app/timer/timer-tests.ts +++ b/tests/app/timer/timer-tests.ts @@ -80,6 +80,7 @@ export function test_setTimeout_callbackCalledWithBooleanPeriod() { // >> (hide) completed = true; // << (hide) + // @ts-ignore }, false); // << timer-set-false