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
This commit is contained in:
Bundyo (Kamen Bundev)
2019-08-02 16:06:03 +03:00
committed by Alexander Vakrilov
parent 78058087c8
commit a569bb2931
3 changed files with 26 additions and 0 deletions

View File

@@ -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;

View File

@@ -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);

View File

@@ -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);