From 29920713c3a6160eee15db570006466527213150 Mon Sep 17 00:00:00 2001 From: Joffrey Bion Date: Thu, 26 Mar 2020 10:49:14 +0100 Subject: Add more functions to sagas framework --- .../sevenwonders/ui/redux/sagas/SagasFramework.kt | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'sw-ui-kt') 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,8 +67,14 @@ class SagaManager( @OptIn(FlowPreview::class, ExperimentalCoroutinesApi::class) class SagaContext( - private val reduxApi: MiddlewareApi, private val actions: BroadcastChannel + private val reduxApi: MiddlewareApi, + private val actions: BroadcastChannel ) { + /** + * Gets the current redux state. + */ + fun getState(): S = reduxApi.getState() + /** * Dispatches the given redux [action]. */ @@ -75,6 +82,15 @@ class SagaContext( reduxApi.dispatch(action) } + /** + * Dispatches an action given by [createAction] for each message received in [channel]. + */ + suspend fun dispatchAll(channel: ReceiveChannel, 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. */ -- cgit