Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare release PART 1 #268

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal class ElmRenderer<Effect : Any, State : Any>(
) {

private val logger = ElmslieConfig.logger
private val ioDispatcher: CoroutineDispatcher = ElmslieConfig.ioDispatchers
private val elmDispatcher: CoroutineDispatcher = ElmslieConfig.elmDispatcher
private val canRender
get() = lifecycle.currentState.isAtLeast(STARTED)

Expand Down Expand Up @@ -52,7 +52,7 @@ internal class ElmRenderer<Effect : Any, State : Any>(
error = it,
)
}
.flowOn(ioDispatcher)
.flowOn(elmDispatcher)
.collect { (state, listItems) ->
catchStateErrors {
if (canRender) {
Expand Down
8 changes: 4 additions & 4 deletions elmslie-core/api/elmslie-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ public final class money/vivid/elmslie/core/ElmScopeKt {

public final class money/vivid/elmslie/core/config/ElmslieConfig {
public static final field INSTANCE Lmoney/vivid/elmslie/core/config/ElmslieConfig;
public final fun elmDispatcher (Lkotlin/jvm/functions/Function0;)V
public final fun getElmDispatcher ()Lkotlinx/coroutines/CoroutineDispatcher;
public final fun getGlobalStoreListeners ()Ljava/util/Set;
public final fun getIoDispatchers ()Lkotlinx/coroutines/CoroutineDispatcher;
public final fun getLogger ()Lmoney/vivid/elmslie/core/logger/ElmslieLogger;
public final fun getShouldStopOnProcessDeath ()Z
public final fun globalStoreListeners (Lkotlin/jvm/functions/Function0;)V
public final fun ioDispatchers (Lkotlin/jvm/functions/Function0;)V
public final fun logger (Lkotlin/jvm/functions/Function1;)V
public final fun shouldStopOnProcessDeath (Lkotlin/jvm/functions/Function0;)V
}
Expand Down Expand Up @@ -176,8 +176,8 @@ public class money/vivid/elmslie/core/store/dsl/ResultBuilder {

public final class money/vivid/elmslie/core/switcher/Switcher {
public fun <init> ()V
public final fun cancel (J)Lkotlinx/coroutines/flow/Flow;
public static synthetic fun cancel$default (Lmoney/vivid/elmslie/core/switcher/Switcher;JILjava/lang/Object;)Lkotlinx/coroutines/flow/Flow;
public final fun cancel (JLkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun cancel$default (Lmoney/vivid/elmslie/core/switcher/Switcher;JLkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public final fun switch (JLkotlin/jvm/functions/Function0;)Lkotlinx/coroutines/flow/Flow;
public static synthetic fun switch$default (Lmoney/vivid/elmslie/core/switcher/Switcher;JLkotlin/jvm/functions/Function0;ILjava/lang/Object;)Lkotlinx/coroutines/flow/Flow;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import money.vivid.elmslie.core.config.ElmslieConfig
fun ElmScope(name: String): CoroutineScope =
CoroutineScope(
context =
ElmslieConfig.ioDispatchers +
ElmslieConfig.elmDispatcher +
SupervisorJob() +
CoroutineName(name) +
CoroutineExceptionHandler { _, throwable ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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 money.vivid.elmslie.core.utils.ElmDispatcher
import kotlin.concurrent.Volatile

object ElmslieConfig {
Expand All @@ -16,7 +16,7 @@ object ElmslieConfig {
private set

@Volatile
var ioDispatchers: CoroutineDispatcher = IoDispatcher
var elmDispatcher: CoroutineDispatcher = ElmDispatcher
private set

@Volatile
Expand Down Expand Up @@ -45,10 +45,10 @@ object ElmslieConfig {

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

fun shouldStopOnProcessDeath(builder: () -> Boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package money.vivid.elmslie.core.store

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import money.vivid.elmslie.core.config.ElmslieConfig
import money.vivid.elmslie.core.switcher.Switcher
import kotlin.reflect.KClass

Expand All @@ -16,16 +16,16 @@ abstract class Actor<Command : Any, Event : Any> {
private val mutex = Mutex()

/**
* Executes a command. This method is performed on the [Dispatchers.IO]
* [kotlinx.coroutines.Dispatchers.IO] which is set by ElmslieConfig.ioDispatchers()
* Executes a command. This method is performed on the [Dispatchers.Default]
* [kotlinx.coroutines.Dispatchers.Default] which is set by ElmslieConfig.elmDispatcher()
*/
abstract fun execute(command: Command): Flow<Event>

fun <T : Any> Flow<T>.mapEvents(
eventMapper: (T) -> Event? = { null },
errorMapper: (error: Throwable) -> Event? = { null },
) = mapNotNull { eventMapper(it) }
.catch { errorMapper(it)?.let { event -> emit(event) } ?: throw it }
.catch { it.logErrorEvent(errorMapper)?.let { event -> emit(event) } ?: throw it }

protected fun <T : Any, Command : Any> Flow<T>.asSwitchFlow(command: Command, delayMillis: Long = 0): Flow<T> {
return flow {
Expand All @@ -41,6 +41,12 @@ abstract class Actor<Command : Any, Event : Any> {
}

protected fun <T : Any> cancelSwitchFlow(command: KClass<out Any>): Flow<T> {
return switchers[command]?.cancel() ?: emptyFlow()
return flow { switchers[command]?.cancel() }
}

private fun Throwable.logErrorEvent(errorMapper: (Throwable) -> Event?): Event? {
return errorMapper(this).also {
ElmslieConfig.logger.nonfatal(error = this)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ElmStore<Event : Any, State : Any, Effect : Any, Command : Any>(
) : Store<Event, Effect, State> {

private val logger = ElmslieConfig.logger
private val eventDispatcher = ElmslieConfig.ioDispatchers.limitedParallelism(parallelism = 1)
private val eventDispatcher = ElmslieConfig.elmDispatcher.limitedParallelism(parallelism = 1)

private val effectsFlow = MutableSharedFlow<Effect>()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface Store<Event : Any, Effect: Any, State: Any> {
* Note that there will be no emission if a state isn't changed (it's [equals] method returned
* `true`.
*
* By default, [State] is collected in [Dispatchers.IO].
* By default, [State] is collected in [Dispatchers.Default].
*/
val states: StateFlow<State>

Expand All @@ -29,7 +29,7 @@ interface Store<Event : Any, Effect: Any, State: Any> {
* In order to implement cache of [Effect], consider extending [Store] with appropriate
* behavior.
*
* By default, [Effect] is collected in [Dispatchers.IO].
* By default, [Effect] is collected in [Dispatchers.Default].
*/
val effects: Flow<Effect>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ class Switcher {
}
}

fun <Event : Any> cancel(
suspend fun cancel(
delayMillis: Long = 0,
): Flow<Event> = flow {
) {
delay(delayMillis)
lock.withLock {
currentChannel?.close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package money.vivid.elmslie.core.utils

import kotlinx.coroutines.CoroutineDispatcher

internal expect val IoDispatcher: CoroutineDispatcher
internal expect val ElmDispatcher: CoroutineDispatcher
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class EffectCachingElmStoreTest {
@BeforeTest
fun beforeEach() {
val testDispatcher = StandardTestDispatcher()
ElmslieConfig.ioDispatchers { testDispatcher }
ElmslieConfig.elmDispatcher { testDispatcher }
Dispatchers.setMain(testDispatcher)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ElmStoreTest {
@BeforeTest
fun beforeEach() {
val testDispatcher = StandardTestDispatcher()
ElmslieConfig.ioDispatchers { testDispatcher }
ElmslieConfig.elmDispatcher { testDispatcher }
Dispatchers.setMain(testDispatcher)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package money.vivid.elmslie.core.utils
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers

internal actual val IoDispatcher: CoroutineDispatcher = Dispatchers.Default
internal actual val ElmDispatcher: CoroutineDispatcher = Dispatchers.Default
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package money.vivid.elmslie.core.utils
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers

internal actual val IoDispatcher: CoroutineDispatcher = Dispatchers.IO
internal actual val ElmDispatcher: CoroutineDispatcher = Dispatchers.Default
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO

internal actual val IoDispatcher: CoroutineDispatcher = Dispatchers.IO
internal actual val ElmDispatcher: CoroutineDispatcher = Dispatchers.Default
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal class StoreTest {
@BeforeTest
fun beforeEach() {
val testDispatcher = StandardTestDispatcher()
ElmslieConfig.ioDispatchers { testDispatcher }
ElmslieConfig.elmDispatcher { testDispatcher }
Dispatchers.setMain(testDispatcher)
}

Expand Down
Loading