diff options
Diffstat (limited to 'sw-ui-kt/src/main/kotlin/org')
-rw-r--r-- | sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/SagasFramework.kt | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/SagasFramework.kt b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/SagasFramework.kt index dc31f41a..6f9e2f26 100644 --- a/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/SagasFramework.kt +++ b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/SagasFramework.kt @@ -7,6 +7,7 @@ import kotlinx.coroutines.FlowPreview import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.Job import kotlinx.coroutines.channels.BroadcastChannel +import kotlinx.coroutines.channels.ReceiveChannel import kotlinx.coroutines.isActive import kotlinx.coroutines.launch import kotlinx.coroutines.promise @@ -66,9 +67,15 @@ class SagaManager<S, A : RAction, R>( @OptIn(FlowPreview::class, ExperimentalCoroutinesApi::class) class SagaContext<S, A : RAction, R>( - private val reduxApi: MiddlewareApi<S, A, R>, private val actions: BroadcastChannel<A> + private val reduxApi: MiddlewareApi<S, A, R>, + private val actions: BroadcastChannel<A> ) { /** + * Gets the current redux state. + */ + fun getState(): S = reduxApi.getState() + + /** * Dispatches the given redux [action]. */ fun dispatch(action: A) { @@ -76,6 +83,15 @@ class SagaContext<S, A : RAction, R>( } /** + * Dispatches an action given by [createAction] for each message received in [channel]. + */ + suspend fun <T> dispatchAll(channel: ReceiveChannel<T>, createAction: (T) -> A) { + for (msg in channel) { + reduxApi.dispatch(createAction(msg)) + } + } + + /** * Executes [handle] on every action dispatched. This runs forever until the current coroutine is cancelled. */ suspend fun onEach(handle: suspend SagaContext<S, A, R>.(A) -> Unit) { |