diff options
author | Joffrey Bion <joffrey.bion@booking.com> | 2020-03-26 10:49:14 +0100 |
---|---|---|
committer | Joffrey Bion <joffrey.bion@booking.com> | 2020-03-27 10:59:39 +0100 |
commit | 29920713c3a6160eee15db570006466527213150 (patch) | |
tree | 819a0051432de5a922a5448b111125a310a15265 /sw-ui-kt/src/main | |
parent | Rename Utils to ReactUtils to allow for other Utils (diff) | |
download | seven-wonders-29920713c3a6160eee15db570006466527213150.tar.gz seven-wonders-29920713c3a6160eee15db570006466527213150.tar.bz2 seven-wonders-29920713c3a6160eee15db570006466527213150.zip |
Add more functions to sagas framework
Diffstat (limited to 'sw-ui-kt/src/main')
-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) { |