Skip to content

Commit

Permalink
Fix MainScheduler timeout and interval types. Fix MainScheduler wrong…
Browse files Browse the repository at this point in the history
…ly removes timeout and interval ids on first task execution.
  • Loading branch information
IlyaGulya committed Jan 10, 2024
1 parent f37282e commit f685041
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 34 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): 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)
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<Int>()
private val intervalIds = mutableSetOf<Int>()
private val timeoutIds = mutableSetOf<Any>()
private val intervalIds = mutableSetOf<Any>()

init {
disposables += this
Expand All @@ -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() {
Expand Down
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): 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)
}
Original file line number Diff line number Diff line change
@@ -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)")
}

0 comments on commit f685041

Please sign in to comment.