From f6daf6274eb97094e2f33bb41c47aacf12280728 Mon Sep 17 00:00:00 2001 From: Dmitriy Berdnikov Date: Fri, 27 Dec 2024 11:54:39 +0300 Subject: [PATCH] make custom key as Any --- elmslie-core/api/elmslie-core.api | 7 +++---- .../money/vivid/elmslie/core/store/Actor.kt | 18 +++++------------- .../samples/coroutines/timer/elm/TimerActor.kt | 8 ++++++-- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/elmslie-core/api/elmslie-core.api b/elmslie-core/api/elmslie-core.api index 845e901..a268c2b 100644 --- a/elmslie-core/api/elmslie-core.api +++ b/elmslie-core/api/elmslie-core.api @@ -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 ()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 { diff --git a/elmslie-core/src/commonMain/kotlin/money/vivid/elmslie/core/store/Actor.kt b/elmslie-core/src/commonMain/kotlin/money/vivid/elmslie/core/store/Actor.kt index f7dafbd..f3914d2 100644 --- a/elmslie-core/src/commonMain/kotlin/money/vivid/elmslie/core/store/Actor.kt +++ b/elmslie-core/src/commonMain/kotlin/money/vivid/elmslie/core/store/Actor.kt @@ -13,7 +13,7 @@ import money.vivid.elmslie.core.switcher.Switcher abstract class Actor { - @PublishedApi internal val switchers = mutableMapOf() + @PublishedApi internal val switchers = mutableMapOf() private val mutex = Mutex() /** @@ -33,16 +33,11 @@ abstract class Actor { * 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 Flow.switch( - key: String = defaultSwitchFlowKey, - delay: Duration = 0.milliseconds, - ): Flow { + fun Flow.switch(key: Any, delay: Duration = 0.milliseconds): Flow { return flow { val switcher = mutex.withLock { switchers.getOrPut(key) { Switcher() } } switcher.switch(delay) { this@switch }.collect { emit(it) } @@ -52,10 +47,10 @@ abstract class Actor { /** * 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 { + fun cancelSwitchFlows(vararg keys: Any): Flow { return flow { keys.forEach { key -> mutex.withLock { switchers.remove(key)?.cancel() } } emit(Unit) @@ -65,7 +60,4 @@ abstract class Actor { 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() } diff --git a/samples/coroutines-loader/src/main/kotlin/money/vivid/elmslie/samples/coroutines/timer/elm/TimerActor.kt b/samples/coroutines-loader/src/main/kotlin/money/vivid/elmslie/samples/coroutines/timer/elm/TimerActor.kt index a4161f2..1d6706f 100644 --- a/samples/coroutines-loader/src/main/kotlin/money/vivid/elmslie/samples/coroutines/timer/elm/TimerActor.kt +++ b/samples/coroutines-loader/src/main/kotlin/money/vivid/elmslie/samples/coroutines/timer/elm/TimerActor.kt @@ -11,10 +11,10 @@ internal object TimerActor : Actor() { 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().mapEvents() } @Suppress("MagicNumber") @@ -26,3 +26,7 @@ internal object TimerActor : Actor() { error("Test error") } } + +internal inline fun Actor<*, *>.cancelSwitchFlowOn(): Flow { + return cancelSwitchFlows(StartCommand::class) +}