From 1af5326260dd424ef6157dd8e492c314994b0833 Mon Sep 17 00:00:00 2001 From: Ilya Gulya Date: Wed, 10 Jan 2024 14:50:06 +0600 Subject: [PATCH] Make TimeoutId platform-specific type --- .../badoo/reaktive/scheduler/JsFunctions.kt | 10 ++++--- .../badoo/reaktive/scheduler/MainScheduler.kt | 4 +-- .../badoo/reaktive/scheduler/JsFunctions.kt | 18 ++++++------ .../badoo/reaktive/scheduler/JsFunctions.kt | 28 ++++++++++++++++--- 4 files changed, 42 insertions(+), 18 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 53e794ee..352872e8 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,11 @@ package com.badoo.reaktive.scheduler -internal expect fun jsSetTimeout(task: () -> Unit, delayMillis: Int): Any +internal expect class TimeoutId -internal expect fun jsSetInterval(task: () -> Unit, delayMillis: Int): Any +internal expect fun jsSetTimeout(task: () -> Unit, delayMillis: Int): TimeoutId -internal expect fun jsClearTimeout(id: Any) +internal expect fun jsSetInterval(task: () -> Unit, delayMillis: Int): TimeoutId -internal expect fun jsClearInterval(id: Any) +internal expect fun jsClearTimeout(id: TimeoutId) + +internal expect fun jsClearInterval(id: TimeoutId) 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 3bf5870a..b31fb5e6 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 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 ab5c4381..37c7939b 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,18 @@ package com.badoo.reaktive.scheduler import com.badoo.reaktive.global.external.globalThis -internal actual fun jsSetTimeout(task: () -> Unit, delayMillis: Int): Any = - globalThis.setTimeout(task, delayMillis) +internal actual class TimeoutId(val id: dynamic) -internal actual fun jsSetInterval(task: () -> Unit, delayMillis: Int): Any = - globalThis.setInterval(task, delayMillis) +internal actual fun jsSetTimeout(task: () -> Unit, delayMillis: Int): TimeoutId = + TimeoutId(globalThis.setTimeout(task, delayMillis)) -internal actual fun jsClearTimeout(id: Any) { - globalThis.clearTimeout(id) +internal actual fun jsSetInterval(task: () -> Unit, delayMillis: Int): TimeoutId = + TimeoutId(globalThis.setInterval(task, delayMillis)) + +internal actual fun jsClearTimeout(id: TimeoutId) { + globalThis.clearTimeout(id.id) } -internal actual fun jsClearInterval(id: Any) { - globalThis.clearInterval(id) +internal actual fun jsClearInterval(id: TimeoutId) { + globalThis.clearInterval(id.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 08e9636e..326076b4 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,35 @@ package com.badoo.reaktive.scheduler -internal actual fun jsSetTimeout(task: () -> Unit, delayMillis: Int): Any = +internal actual class TimeoutId(val id: JsAny) + +internal actual fun jsSetTimeout(task: () -> Unit, delayMillis: Int): TimeoutId = + TimeoutId(jsSetTimeoutInternal(task, delayMillis)) + +internal actual fun jsSetInterval(task: () -> Unit, delayMillis: Int): TimeoutId = + TimeoutId(jsSetIntervalInternal(task, delayMillis)) + +internal actual fun jsClearTimeout(id: TimeoutId) { + jsClearTimeoutInternal(id.id) +} + +internal actual fun jsClearInterval(id: TimeoutId) { + jsClearIntervalInternal(id.id) +} + +@Suppress("UnusedPrivateMember") +private fun jsSetTimeoutInternal(task: () -> Unit, delayMillis: Int): JsAny = js("setTimeout(task, delayMillis)") -internal actual fun jsSetInterval(task: () -> Unit, delayMillis: Int): Any = +@Suppress("UnusedPrivateMember") +private fun jsSetIntervalInternal(task: () -> Unit, delayMillis: Int): JsAny = js("setInterval(task, delayMillis)") -internal actual fun jsClearTimeout(id: Any) { +@Suppress("UnusedPrivateMember") +private fun jsClearTimeoutInternal(id: JsAny) { js("clearTimeout(id)") } -internal actual fun jsClearInterval(id: Any) { +@Suppress("UnusedPrivateMember") +private fun jsClearIntervalInternal(id: JsAny) { js("clearInterval(id)") }