From f685041a915bb3e310c4e2a35e418016da72e6ff Mon Sep 17 00:00:00 2001 From: Ilya Gulya Date: Wed, 10 Jan 2024 14:21:00 +0600 Subject: [PATCH] Fix MainScheduler timeout and interval types. Fix MainScheduler wrongly removes timeout and interval ids on first task execution. --- .../badoo/reaktive/scheduler/JsFunctions.kt | 8 ++--- .../badoo/reaktive/scheduler/MainScheduler.kt | 34 +++++++------------ .../badoo/reaktive/scheduler/JsFunctions.kt | 8 ++--- .../badoo/reaktive/scheduler/JsFunctions.kt | 8 ++--- 4 files changed, 24 insertions(+), 34 deletions(-) diff --git a/reaktive/src/jsCommonMain/kotlin/com/badoo/reaktive/scheduler/JsFunctions.kt b/reaktive/src/jsCommonMain/kotlin/com/badoo/reaktive/scheduler/JsFunctions.kt index ad03bf2af..53e794eeb 100644 --- a/reaktive/src/jsCommonMain/kotlin/com/badoo/reaktive/scheduler/JsFunctions.kt +++ b/reaktive/src/jsCommonMain/kotlin/com/badoo/reaktive/scheduler/JsFunctions.kt @@ -1,9 +1,9 @@ package com.badoo.reaktive.scheduler -internal expect fun jsSetTimeout(task: () -> Unit, delayMillis: Int): Int +internal expect fun jsSetTimeout(task: () -> Unit, delayMillis: Int): Any -internal expect fun jsSetInterval(task: () -> Unit, delayMillis: Int): Int +internal expect fun jsSetInterval(task: () -> Unit, delayMillis: Int): Any -internal expect fun jsClearTimeout(id: Int) +internal expect fun jsClearTimeout(id: Any) -internal expect fun jsClearInterval(id: Int) +internal expect fun jsClearInterval(id: Any) diff --git a/reaktive/src/jsCommonMain/kotlin/com/badoo/reaktive/scheduler/MainScheduler.kt b/reaktive/src/jsCommonMain/kotlin/com/badoo/reaktive/scheduler/MainScheduler.kt index 2f4d88a3b..3bf5870ab 100644 --- a/reaktive/src/jsCommonMain/kotlin/com/badoo/reaktive/scheduler/MainScheduler.kt +++ b/reaktive/src/jsCommonMain/kotlin/com/badoo/reaktive/scheduler/MainScheduler.kt @@ -20,8 +20,8 @@ internal class MainScheduler : Scheduler { private var _isDisposed = false - private val timeoutIds = mutableSetOf() - private val intervalIds = mutableSetOf() + private val timeoutIds = mutableSetOf() + private val intervalIds = mutableSetOf() init { disposables += this @@ -48,31 +48,21 @@ internal class MainScheduler : Scheduler { } private fun setTimeout(delay: Duration, task: () -> Unit) { - var id: Int? = null - - id = jsSetTimeout( - { - timeoutIds.remove(id) - task() - }, - delay.coerceAtLeastZero().inWholeMilliseconds.toInt(), + timeoutIds.add( + jsSetTimeout( + task = task, + delayMillis = delay.coerceAtLeastZero().inWholeMilliseconds.toInt() + ) ) - - timeoutIds.add(id) } private fun setInterval(period: Duration, task: () -> Unit) { - var id: Int? = null - - id = jsSetInterval( - { - intervalIds.remove(id) - task() - }, - period.coerceAtLeastZero().inWholeMilliseconds.toInt(), + intervalIds.add( + jsSetInterval( + task = task, + delayMillis = period.coerceAtLeastZero().inWholeMilliseconds.toInt(), + ) ) - - intervalIds.add(id) } override fun cancel() { diff --git a/reaktive/src/jsMain/kotlin/com/badoo/reaktive/scheduler/JsFunctions.kt b/reaktive/src/jsMain/kotlin/com/badoo/reaktive/scheduler/JsFunctions.kt index f3fb2e762..ab5c43818 100644 --- a/reaktive/src/jsMain/kotlin/com/badoo/reaktive/scheduler/JsFunctions.kt +++ b/reaktive/src/jsMain/kotlin/com/badoo/reaktive/scheduler/JsFunctions.kt @@ -2,16 +2,16 @@ package com.badoo.reaktive.scheduler import com.badoo.reaktive.global.external.globalThis -internal actual fun jsSetTimeout(task: () -> Unit, delayMillis: Int): Int = +internal actual fun jsSetTimeout(task: () -> Unit, delayMillis: Int): Any = globalThis.setTimeout(task, delayMillis) -internal actual fun jsSetInterval(task: () -> Unit, delayMillis: Int): Int = +internal actual fun jsSetInterval(task: () -> Unit, delayMillis: Int): Any = globalThis.setInterval(task, delayMillis) -internal actual fun jsClearTimeout(id: Int) { +internal actual fun jsClearTimeout(id: Any) { globalThis.clearTimeout(id) } -internal actual fun jsClearInterval(id: Int) { +internal actual fun jsClearInterval(id: Any) { globalThis.clearInterval(id) } diff --git a/reaktive/src/wasmJsMain/kotlin/com/badoo/reaktive/scheduler/JsFunctions.kt b/reaktive/src/wasmJsMain/kotlin/com/badoo/reaktive/scheduler/JsFunctions.kt index 35cec9772..08e9636e9 100644 --- a/reaktive/src/wasmJsMain/kotlin/com/badoo/reaktive/scheduler/JsFunctions.kt +++ b/reaktive/src/wasmJsMain/kotlin/com/badoo/reaktive/scheduler/JsFunctions.kt @@ -1,15 +1,15 @@ package com.badoo.reaktive.scheduler -internal actual fun jsSetTimeout(task: () -> Unit, delayMillis: Int): Int = +internal actual fun jsSetTimeout(task: () -> Unit, delayMillis: Int): Any = js("setTimeout(task, delayMillis)") -internal actual fun jsSetInterval(task: () -> Unit, delayMillis: Int): Int = +internal actual fun jsSetInterval(task: () -> Unit, delayMillis: Int): Any = js("setInterval(task, delayMillis)") -internal actual fun jsClearTimeout(id: Int) { +internal actual fun jsClearTimeout(id: Any) { js("clearTimeout(id)") } -internal actual fun jsClearInterval(id: Int) { +internal actual fun jsClearInterval(id: Any) { js("clearInterval(id)") }