Skip to content

Commit

Permalink
Make MainExecutor abstract and extract platform-specific logic to rel…
Browse files Browse the repository at this point in the history
…evant targets
  • Loading branch information
IlyaGulya committed Jan 10, 2024
1 parent d32fd31 commit e39b45f
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 24 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import com.badoo.reaktive.disposable.plusAssign
import com.badoo.reaktive.utils.coerceAtLeastZero
import kotlin.time.Duration

@Suppress("FunctionName")
internal expect fun MainThreadExecutor(disposables: CompositeDisposable): Scheduler.Executor

internal class MainScheduler : Scheduler {

private val disposables = CompositeDisposable()
Expand All @@ -14,14 +17,14 @@ internal class MainScheduler : Scheduler {

override fun destroy() = disposables.dispose()

private class MainThreadExecutor(
internal abstract class BaseExecutor<IntervalType>(
private val disposables: CompositeDisposable,
) : Scheduler.Executor {

private var _isDisposed = false

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

init {
disposables += this
Expand Down Expand Up @@ -49,7 +52,7 @@ internal class MainScheduler : Scheduler {

private fun setTimeout(delay: Duration, task: () -> Unit) {
timeoutIds.add(
jsSetTimeout(
setTimeout(
task = task,
delayMillis = delay.coerceAtLeastZero().inWholeMilliseconds.toInt()
)
Expand All @@ -58,18 +61,23 @@ internal class MainScheduler : Scheduler {

private fun setInterval(period: Duration, task: () -> Unit) {
intervalIds.add(
jsSetInterval(
setInterval(
task = task,
delayMillis = period.coerceAtLeastZero().inWholeMilliseconds.toInt(),
)
)
}

override fun cancel() {
timeoutIds.forEach { jsClearTimeout(it) }
intervalIds.forEach { jsClearInterval(it) }
timeoutIds.forEach { clearTimeout(it) }
intervalIds.forEach { clearInterval(it) }
}

abstract fun setTimeout(task: () -> Unit, delayMillis: Int): IntervalType
abstract fun setInterval(task: () -> Unit, delayMillis: Int): IntervalType
abstract fun clearTimeout(id: IntervalType)
abstract fun clearInterval(id: IntervalType)

override val isDisposed: Boolean
get() = _isDisposed

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): Any =
internal fun jsSetTimeout(task: () -> Unit, delayMillis: Int): dynamic =
globalThis.setTimeout(task, delayMillis)

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

internal actual fun jsClearTimeout(id: Any) {
internal fun jsClearTimeout(id: Any) {
globalThis.clearTimeout(id)
}

internal actual fun jsClearInterval(id: Any) {
internal fun jsClearInterval(id: Any) {
globalThis.clearInterval(id)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.badoo.reaktive.scheduler

import com.badoo.reaktive.disposable.CompositeDisposable

@Suppress("FunctionName")
actual fun MainThreadExecutor(disposables: CompositeDisposable): Scheduler.Executor =
object : MainScheduler.BaseExecutor<dynamic>(disposables) {
override fun setTimeout(task: () -> Unit, delayMillis: Int): dynamic =
jsSetTimeout(task, delayMillis)

override fun setInterval(task: () -> Unit, delayMillis: Int): dynamic =
jsSetInterval(task, delayMillis)

override fun clearTimeout(id: dynamic) {
jsClearTimeout(id)
}

override fun clearInterval(id: dynamic) {
jsClearInterval(id)
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package com.badoo.reaktive.scheduler

internal actual fun jsSetTimeout(task: () -> Unit, delayMillis: Int): Any =
@Suppress("UnusedPrivateMember")
internal fun jsSetTimeout(task: () -> Unit, delayMillis: Int): JsAny =
js("setTimeout(task, delayMillis)")

internal actual fun jsSetInterval(task: () -> Unit, delayMillis: Int): Any =
@Suppress("UnusedPrivateMember")
internal fun jsSetInterval(task: () -> Unit, delayMillis: Int): JsAny =
js("setInterval(task, delayMillis)")

internal actual fun jsClearTimeout(id: Any) {
@Suppress("UnusedPrivateMember")
internal fun jsClearTimeout(id: JsAny) {
js("clearTimeout(id)")
}

internal actual fun jsClearInterval(id: Any) {
@Suppress("UnusedPrivateMember")
internal fun jsClearInterval(id: JsAny) {
js("clearInterval(id)")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.badoo.reaktive.scheduler

import com.badoo.reaktive.disposable.CompositeDisposable

@Suppress("FunctionName")
actual fun MainThreadExecutor(disposables: CompositeDisposable): Scheduler.Executor =
object : MainScheduler.BaseExecutor<JsAny>(disposables) {
override fun setTimeout(task: () -> Unit, delayMillis: Int): JsAny =
jsSetTimeout(task, delayMillis)

override fun setInterval(task: () -> Unit, delayMillis: Int): JsAny =
jsSetInterval(task, delayMillis)

override fun clearTimeout(id: JsAny) {
jsClearTimeout(id)
}

override fun clearInterval(id: JsAny) {
jsClearInterval(id)
}
}

0 comments on commit e39b45f

Please sign in to comment.