Skip to content

Commit

Permalink
Merge pull request #274 from vivid-money/any-custom-key
Browse files Browse the repository at this point in the history
make custom key as Any
  • Loading branch information
rcmkt authored Dec 27, 2024
2 parents ce3b193 + f6daf62 commit d67bc52
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
7 changes: 3 additions & 4 deletions elmslie-core/api/elmslie-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,13 @@ public abstract interface class money/vivid/elmslie/core/logger/strategy/LogStra

public abstract class money/vivid/elmslie/core/store/Actor {
public fun <init> ()V
protected final fun cancelSwitchFlows ([Ljava/lang/String;)Lkotlinx/coroutines/flow/Flow;
public static synthetic fun cancelSwitchFlows$default (Lmoney/vivid/elmslie/core/store/Actor;[Ljava/lang/String;ILjava/lang/Object;)Lkotlinx/coroutines/flow/Flow;
public final fun cancelSwitchFlows ([Ljava/lang/Object;)Lkotlinx/coroutines/flow/Flow;
public abstract fun execute (Ljava/lang/Object;)Lkotlinx/coroutines/flow/Flow;
public final fun getSwitchers ()Ljava/util/Map;
public final fun mapEvents (Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Lkotlinx/coroutines/flow/Flow;
public static synthetic fun mapEvents$default (Lmoney/vivid/elmslie/core/store/Actor;Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lkotlinx/coroutines/flow/Flow;
protected final fun switch-SxA4cEA (Lkotlinx/coroutines/flow/Flow;Ljava/lang/String;J)Lkotlinx/coroutines/flow/Flow;
public static synthetic fun switch-SxA4cEA$default (Lmoney/vivid/elmslie/core/store/Actor;Lkotlinx/coroutines/flow/Flow;Ljava/lang/String;JILjava/lang/Object;)Lkotlinx/coroutines/flow/Flow;
public final fun switch-SxA4cEA (Lkotlinx/coroutines/flow/Flow;Ljava/lang/Object;J)Lkotlinx/coroutines/flow/Flow;
public static synthetic fun switch-SxA4cEA$default (Lmoney/vivid/elmslie/core/store/Actor;Lkotlinx/coroutines/flow/Flow;Ljava/lang/Object;JILjava/lang/Object;)Lkotlinx/coroutines/flow/Flow;
}

public final class money/vivid/elmslie/core/store/EffectCachingElmStore : money/vivid/elmslie/core/store/Store {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import money.vivid.elmslie.core.switcher.Switcher

abstract class Actor<Command : Any, Event : Any> {

@PublishedApi internal val switchers = mutableMapOf<String, Switcher>()
@PublishedApi internal val switchers = mutableMapOf<Any, Switcher>()
private val mutex = Mutex()

/**
Expand All @@ -33,16 +33,11 @@ abstract class Actor<Command : Any, Event : Any> {
* Extension function to switch the flow by a given key and optional delay. This function ensures
* that only one flow with the same key is active at a time.
*
* @param key The key to identify the flow. Defaults to the class name of the Actor. If there is
* more than one usage of the switch function in the same Actor, it is recommended to provide a
* unique key.
* @param key The key to identify the flow.
* @param delay The delay in milliseconds before launching the initial flow. Defaults to 0.
* @return A new flow that emits the values from the original flow.
*/
protected fun <T : Any> Flow<T>.switch(
key: String = defaultSwitchFlowKey,
delay: Duration = 0.milliseconds,
): Flow<T> {
fun <T : Any> Flow<T>.switch(key: Any, delay: Duration = 0.milliseconds): Flow<T> {
return flow {
val switcher = mutex.withLock { switchers.getOrPut(key) { Switcher() } }
switcher.switch(delay) { this@switch }.collect { emit(it) }
Expand All @@ -52,10 +47,10 @@ abstract class Actor<Command : Any, Event : Any> {
/**
* Cancels the switch flow(s) by a given key(s).
*
* @param keys The keys to identify the flows. Defaults to the class name of the Actor.
* @param keys The keys to identify the flows.
* @return A new flow that emits [Unit] when switch flows are cancelled.
*/
protected fun cancelSwitchFlows(vararg keys: String = arrayOf(defaultSwitchFlowKey)): Flow<Unit> {
fun cancelSwitchFlows(vararg keys: Any): Flow<Unit> {
return flow {
keys.forEach { key -> mutex.withLock { switchers.remove(key)?.cancel() } }
emit(Unit)
Expand All @@ -65,7 +60,4 @@ abstract class Actor<Command : Any, Event : Any> {
private fun Throwable.logErrorEvent(errorMapper: (Throwable) -> Event?): Event? {
return errorMapper(this).also { ElmslieConfig.logger.nonfatal(error = this) }
}

private inline val defaultSwitchFlowKey: String
get() = this::class.simpleName.orEmpty()
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ internal object TimerActor : Actor<Command, Event>() {
when (command) {
is Command.Start ->
secondsFlow()
.switch()
.switch(Command.Start::class)
.mapEvents(eventMapper = { Event.OnTimeTick }, errorMapper = { Event.OnTimeError(it) })

is Command.Stop -> cancelSwitchFlows().mapEvents()
is Command.Stop -> cancelSwitchFlowOn<Command.Start>().mapEvents()
}

@Suppress("MagicNumber")
Expand All @@ -26,3 +26,7 @@ internal object TimerActor : Actor<Command, Event>() {
error("Test error")
}
}

internal inline fun <reified StartCommand : Any> Actor<*, *>.cancelSwitchFlowOn(): Flow<Unit> {
return cancelSwitchFlows(StartCommand::class)
}

0 comments on commit d67bc52

Please sign in to comment.