From a569bb2931f9a9db8f24e6c513a7f18a52f0dc1a Mon Sep 17 00:00:00 2001 From: "Bundyo (Kamen Bundev)" Date: Fri, 2 Aug 2019 16:06:03 +0300 Subject: [PATCH] fix(timer): setTimeout/setInterval support for boolean period (#7569) * Fix setTimeout/setInterval can't be called with boolean period * Add typings and a test * Revert typings * Ignore the wrong type --- tests/app/timer/timer-tests.ts | 17 +++++++++++++++++ tns-core-modules/timer/timer.android.ts | 6 ++++++ tns-core-modules/timer/timer.ios.ts | 3 +++ 3 files changed, 26 insertions(+) diff --git a/tests/app/timer/timer-tests.ts b/tests/app/timer/timer-tests.ts index c8c8a55d6..2c6f7997f 100644 --- a/tests/app/timer/timer-tests.ts +++ b/tests/app/timer/timer-tests.ts @@ -72,6 +72,23 @@ 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) + // @ts-ignore + }, 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.android.ts b/tns-core-modules/timer/timer.android.ts index 672dfb423..f18b81567 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 7436cba95..777c7fa21 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);