From 7cd8e12e0b4592b570782873ae2de0df7eeb7323 Mon Sep 17 00:00:00 2001 From: vakrilov Date: Tue, 26 Apr 2016 11:55:31 +0300 Subject: [PATCH] Fix: Timer somtimes crashing in IOS --- apps/tests/timer-tests.ts | 52 ++++++++++++++++++++++++++++++++++++++- timer/timer.ios.ts | 22 ++++++++++------- 2 files changed, 64 insertions(+), 10 deletions(-) diff --git a/apps/tests/timer-tests.ts b/apps/tests/timer-tests.ts index d6db49f53..23baaab55 100644 --- a/apps/tests/timer-tests.ts +++ b/apps/tests/timer-tests.ts @@ -118,4 +118,54 @@ export function test_setInterval_callbackShouldBeCleared() { TKUnit.waitUntilReady(() => false, 0.5); TKUnit.assert(counter === 1, "Callback should be raised only once!"); -}; \ No newline at end of file +}; + +export function test_clearTimeout_multipleTimes_afterTick() { + let completed = false; + + let id = timer.setTimeout(() => { + completed = true; + }); + + TKUnit.waitUntilReady(() => completed, 0.5); + TKUnit.assert(completed, "Callback should be called"); + + timer.clearTimeout(id); + timer.clearTimeout(id); +} + +export function test_clearTimeout_immediatelyAfterCreate() { + let completed = false; + + let id = timer.setTimeout(() => { + completed = true; + }); + timer.clearTimeout(id); + + TKUnit.waitUntilReady(() => false, 0.02); + TKUnit.assert(!completed, "Callback should not be called"); +} + +export function test_clearInterval_immediatelyAfterCreate() { + let completed = false; + + let id = timer.setInterval(() => { + completed = true; + }); + timer.clearInterval(id); + + TKUnit.waitUntilReady(() => false, 0.02); + TKUnit.assert(!completed, "Callback should not be called"); +} + +export function test_clearTimeout_insideCallback() { + let completed = false; + + let id = timer.setTimeout(() => { + completed = true; + timer.clearTimeout(id); + }); + + TKUnit.waitUntilReady(() => completed, 0.5); + TKUnit.assert(completed, "Callback should be called"); +} diff --git a/timer/timer.ios.ts b/timer/timer.ios.ts index fbd938573..621069d78 100644 --- a/timer/timer.ios.ts +++ b/timer/timer.ios.ts @@ -10,33 +10,37 @@ interface KeyValuePair { } class TimerTargetImpl extends NSObject { - private _callback: Function; - public canceled = false; + 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.new(); - handler._callback = callback; + handler.callback = callback; handler.id = id; handler.shouldRepeat = shouldRepeat; return handler; } public tick(timer): void { - if (!this.canceled) { - this._callback(); + if (!this.disposed) { + this.callback(); } - if (this.canceled || !this.shouldRepeat) { + if (!this.shouldRepeat) { this.unregister(); } } public unregister() { - let timer = timeoutCallbacks.get(this.id).k; - timer.invalidate(); - timeoutCallbacks.delete(this.id); + if (!this.disposed) { + this.disposed = true; + + let timer = timeoutCallbacks.get(this.id).k; + timer.invalidate(); + timeoutCallbacks.delete(this.id); + } } public static ObjCExposedMethods = {