Skip to content

Commit

Permalink
rework dispatchers usage
Browse files Browse the repository at this point in the history
  • Loading branch information
dsvag committed Apr 29, 2024
1 parent 5eb7a61 commit 3a99b51
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,30 @@ package money.vivid.elmslie.core.config

import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import money.vivid.elmslie.core.logger.ElmslieLogConfiguration
import money.vivid.elmslie.core.logger.ElmslieLogger
import money.vivid.elmslie.core.logger.strategy.IgnoreLog
import money.vivid.elmslie.core.store.StoreListener
import money.vivid.elmslie.core.utils.IoDispatcher
import kotlin.concurrent.Volatile

object ElmslieConfig {

@Volatile
private lateinit var _logger: ElmslieLogger
var logger: ElmslieLogger = ElmslieLogConfiguration().apply { always(IgnoreLog) }.build()
private set

@Volatile private lateinit var _ioDispatchers: CoroutineDispatcher

@Volatile private var _shouldStopOnProcessDeath: Boolean = true

@Volatile private var _globalStoreListeners: Set<StoreListener<Any, Any, Any, Any>> = emptySet()

val logger: ElmslieLogger
get() = _logger

val ioDispatchers: CoroutineDispatcher
get() = _ioDispatchers

val shouldStopOnProcessDeath: Boolean
get() = _shouldStopOnProcessDeath
@Volatile
var ioDispatchers: CoroutineDispatcher = IoDispatcher
private set

val globalStoreListeners: Set<StoreListener<Any, Any, Any, Any>>
get() = _globalStoreListeners
@Volatile
var shouldStopOnProcessDeath: Boolean = true
private set

init {
logger { always(IgnoreLog) }
ioDispatchers { Dispatchers.IO }
}
@Volatile
var globalStoreListeners: Set<StoreListener<Any, Any, Any, Any>> = emptySet()
private set

/**
* Configures logging and error handling
Expand All @@ -50,22 +40,22 @@ object ElmslieConfig {
* ```
*/
fun logger(config: (ElmslieLogConfiguration.() -> Unit)) {
ElmslieLogConfiguration().apply(config).build().also { _logger = it }
ElmslieLogConfiguration().apply(config).build().also { logger = it }
}

/**
* Configures CoroutineDispatcher for performing operations in background. Default is
* [Dispatchers.IO]
*/
fun ioDispatchers(builder: () -> CoroutineDispatcher) {
_ioDispatchers = builder()
ioDispatchers = builder()
}

fun shouldStopOnProcessDeath(builder: () -> Boolean) {
_shouldStopOnProcessDeath = builder()
shouldStopOnProcessDeath = builder()
}

fun globalStoreListeners(builder: () -> Set<StoreListener<Any, Any, Any, Any>>) {
_globalStoreListeners = builder()
globalStoreListeners = builder()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ElmStore<Event : Any, State : Any, Effect : Any, Command : Any>(
storeListeners?.forEach(::add)
}

override val scope = ElmScope("StoreScope")
override val scope = ElmScope("${key}Scope")

override val states: StateFlow<State> = statesFlow.asStateFlow()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package money.vivid.elmslie.core.utils

import kotlinx.coroutines.CoroutineDispatcher

internal expect val IoDispatcher: CoroutineDispatcher
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package money.vivid.elmslie.core.utils

import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers

internal actual val IoDispatcher: CoroutineDispatcher = Dispatchers.IO
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package money.vivid.elmslie.core.utils

import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO

internal actual val IoDispatcher: CoroutineDispatcher = Dispatchers.IO
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package money.vivid.elmslie.core.utils

import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers

internal actual val IoDispatcher: CoroutineDispatcher = Dispatchers.Default
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[versions]
agp = "8.0.2"
coroutines = "1.7.1"
coroutines = "1.8.0"
dokka = "1.8.20"
kotlin = "1.9.21"
kotlin = "1.9.23"
lifecycle = "2.6.1"

[libraries]
Expand Down

0 comments on commit 3a99b51

Please sign in to comment.