aboutsummaryrefslogtreecommitdiff
path: root/kvision-modules/kvision-redux/src/main
diff options
context:
space:
mode:
authorRobert Jaros <rjaros@finn.pl>2019-03-19 16:11:24 +0100
committerRobert Jaros <rjaros@finn.pl>2019-03-19 16:12:30 +0100
commit11b9c93cbcdb35ce6b5c0827ccbe58fd36959b9e (patch)
tree8f7eebbb5921010f384ed6f54bce0754159b25ef /kvision-modules/kvision-redux/src/main
parentdf20b45426f223a75ec902a2778b138080205c84 (diff)
downloadkvision-11b9c93cbcdb35ce6b5c0827ccbe58fd36959b9e.tar.gz
kvision-11b9c93cbcdb35ce6b5c0827ccbe58fd36959b9e.tar.bz2
kvision-11b9c93cbcdb35ce6b5c0827ccbe58fd36959b9e.zip
Fix async action handling in ReduxStore
Diffstat (limited to 'kvision-modules/kvision-redux/src/main')
-rw-r--r--kvision-modules/kvision-redux/src/main/kotlin/pl/treksoft/kvision/redux/ReduxStore.kt19
1 files changed, 14 insertions, 5 deletions
diff --git a/kvision-modules/kvision-redux/src/main/kotlin/pl/treksoft/kvision/redux/ReduxStore.kt b/kvision-modules/kvision-redux/src/main/kotlin/pl/treksoft/kvision/redux/ReduxStore.kt
index 4c4f5cc5..47d2c3c8 100644
--- a/kvision-modules/kvision-redux/src/main/kotlin/pl/treksoft/kvision/redux/ReduxStore.kt
+++ b/kvision-modules/kvision-redux/src/main/kotlin/pl/treksoft/kvision/redux/ReduxStore.kt
@@ -34,12 +34,13 @@ import redux.rEnhancer
typealias Dispatch<A> = (A) -> WrapperAction
typealias GetState<S> = () -> S
+typealias ActionCreator<A, S> = (Dispatch<A>, GetState<S>) -> Unit
/**
* An inline helper function for creating Redux store.
*
* @param reducer a reducer function
- * @param state an initial state
+ * @param initialState an initial state
* @param middlewares a list of optional Redux JS middlewares
*/
@UseExperimental(ImplicitReflectionSerializer::class)
@@ -60,7 +61,7 @@ inline fun <reified S : Any, A : RAction> createReduxStore(
* @param S redux state type (@Serializable)
* @param A redux action type
* @param reducer a reducer function
- * @param state an initial state
+ * @param initialState an initial state
* @param stateSerializer a serializer for the state type
* @param middlewares a list of optional Redux JS middlewares
*/
@@ -107,9 +108,17 @@ class ReduxStore<S : Any, A : RAction>(
/**
* Dispatches an asynchronous action function.
*/
- fun dispatch(action: (Dispatch<A>, GetState<S>) -> Unit): WrapperAction {
- return store.dispatch({ d: Dispatch<A>, g: GetState<String> ->
- action(d, { JSON.plain.parse(stateSerializer, g()) })
+ fun dispatch(actionCreator: ActionCreator<dynamic, S>): WrapperAction {
+ return store.dispatch({ reduxDispatch: Dispatch<dynamic>, reduxGetState: GetState<String> ->
+ val newDispatch: Dispatch<dynamic> = { elem ->
+ @Suppress("UnsafeCastFromDynamic")
+ if (js("typeof elem === 'function'")) {
+ dispatch(actionCreator = elem)
+ } else {
+ reduxDispatch(elem)
+ }
+ }
+ actionCreator(newDispatch, { JSON.plain.parse(stateSerializer, reduxGetState()) })
})
}