Skip to content

Commit

Permalink
Merge pull request #273 from vivid-money/minor-changes
Browse files Browse the repository at this point in the history
Minor changes
  • Loading branch information
rcmkt authored Dec 27, 2024
2 parents 3d1785e + 029bfba commit ce3b193
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
8 changes: 4 additions & 4 deletions elmslie-core/api/elmslie-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public abstract class money/vivid/elmslie/core/store/Actor {
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 (Lkotlinx/coroutines/flow/Flow;Ljava/lang/String;J)Lkotlinx/coroutines/flow/Flow;
public static synthetic fun switch$default (Lmoney/vivid/elmslie/core/store/Actor;Lkotlinx/coroutines/flow/Flow;Ljava/lang/String;JILjava/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 class money/vivid/elmslie/core/store/EffectCachingElmStore : money/vivid/elmslie/core/store/Store {
Expand Down Expand Up @@ -119,9 +119,9 @@ public final class money/vivid/elmslie/core/store/Result {

public abstract class money/vivid/elmslie/core/store/ScreenReducer : money/vivid/elmslie/core/store/StateReducer {
public fun <init> (Lkotlin/reflect/KClass;Lkotlin/reflect/KClass;)V
protected abstract fun internal (Lmoney/vivid/elmslie/core/store/StateReducer$Result;Ljava/lang/Object;)Ljava/lang/Object;
protected abstract fun internal (Lmoney/vivid/elmslie/core/store/StateReducer$Result;Ljava/lang/Object;)V
protected fun reduce (Lmoney/vivid/elmslie/core/store/StateReducer$Result;Ljava/lang/Object;)V
protected abstract fun ui (Lmoney/vivid/elmslie/core/store/StateReducer$Result;Ljava/lang/Object;)Ljava/lang/Object;
protected abstract fun ui (Lmoney/vivid/elmslie/core/store/StateReducer$Result;Ljava/lang/Object;)V
}

public abstract class money/vivid/elmslie/core/store/StateReducer {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package money.vivid.elmslie.core.store

import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flow
Expand Down Expand Up @@ -34,16 +36,16 @@ abstract class Actor<Command : Any, Event : Any> {
* @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 delayMillis The delay in milliseconds before launching the initial flow. Defaults to 0.
* @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,
delayMillis: Long = 0,
delay: Duration = 0.milliseconds,
): Flow<T> {
return flow {
val switcher = mutex.withLock { switchers.getOrPut(key) { Switcher() } }
switcher.switch(delayMillis) { this@switch }.collect { emit(it) }
switcher.switch(delay) { this@switch }.collect { emit(it) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ abstract class ScreenReducer<
>(private val uiEventClass: KClass<Ui>, private val internalEventClass: KClass<Internal>) :
StateReducer<Event, State, Effect, Command>() {

protected abstract fun Result.ui(event: Ui): Any?
protected abstract fun Result.ui(event: Ui)

protected abstract fun Result.internal(event: Internal): Any?
protected abstract fun Result.internal(event: Internal)

override fun Result.reduce(event: Event) {
@Suppress("UNCHECKED_CAST")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package money.vivid.elmslie.core.switcher

import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds
import kotlinx.coroutines.channels.SendChannel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
Expand Down Expand Up @@ -34,18 +36,21 @@ internal class Switcher {
/**
* Collect given flow as a job and cancels all previous ones.
*
* @param delayMillis operation delay measured with milliseconds. Can be specified to debounce
* existing requests.
* @param delay operation delay measured with milliseconds. Can be specified to debounce existing
* requests.
* @param action actual event source
*/
fun <Event : Any> switch(delayMillis: Long = 0, action: () -> Flow<Event>): Flow<Event> {
fun <Event : Any> switch(
delay: Duration = 0.milliseconds,
action: () -> Flow<Event>,
): Flow<Event> {
return callbackFlow {
lock.withLock {
currentChannel?.close()
currentChannel = channel
}

delay(delayMillis)
delay(delay)

action.invoke().onEach { send(it) }.catch { close(it) }.collect()

Expand Down

0 comments on commit ce3b193

Please sign in to comment.