Skip to content

Commit

Permalink
Make TimeoutId platform-specific type
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaGulya committed Jan 10, 2024
1 parent d32fd31 commit 11174af
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.badoo.reaktive.scheduler

internal expect fun jsSetTimeout(task: () -> Unit, delayMillis: Int): Any
internal expect fun jsSetTimeout(task: () -> Unit, delayMillis: Int): TimeoutId

internal expect fun jsSetInterval(task: () -> Unit, delayMillis: Int): Any
internal expect fun jsSetInterval(task: () -> Unit, delayMillis: Int): TimeoutId

internal expect fun jsClearTimeout(id: Any)
internal expect fun jsClearTimeout(id: TimeoutId)

internal expect fun jsClearInterval(id: Any)
internal expect fun jsClearInterval(id: TimeoutId)
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ internal class MainScheduler : Scheduler {

private var _isDisposed = false

private val timeoutIds = mutableSetOf<Any>()
private val intervalIds = mutableSetOf<Any>()
private val timeoutIds = mutableSetOf<TimeoutId>()
private val intervalIds = mutableSetOf<TimeoutId>()

init {
disposables += this
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.badoo.reaktive.scheduler

internal expect class TimeoutId
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ 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 fun jsSetTimeout(task: () -> Unit, delayMillis: Int): TimeoutId =
TimeoutId(globalThis.setTimeout(task, delayMillis))

internal actual fun jsSetInterval(task: () -> Unit, delayMillis: Int): Any =
globalThis.setInterval(task, delayMillis)
internal actual fun jsSetInterval(task: () -> Unit, delayMillis: Int): TimeoutId =
TimeoutId(globalThis.setInterval(task, delayMillis))

internal actual fun jsClearTimeout(id: Any) {
globalThis.clearTimeout(id)
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)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.badoo.reaktive.scheduler

internal actual class TimeoutId(val id: dynamic)
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
package com.badoo.reaktive.scheduler

internal actual fun jsSetTimeout(task: () -> Unit, delayMillis: Int): Any =
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)")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.badoo.reaktive.scheduler

internal actual class TimeoutId(val id: JsAny)

0 comments on commit 11174af

Please sign in to comment.