@@ -6,7 +6,7 @@ package org.reduxkotlin
66 * val store = createStore(myReducer, initialState,
77 * applyMiddleware(thunk, myMiddleware))
88 *
9- * val myNetworkThunk(query: String) = createThunk { dispatch, getState, extraArgument ->
9+ * fun myNetworkThunk(query: String): Thunk<AppState> = { dispatch, getState, extraArgument ->
1010 * launch {
1111 * dispatch(LoadingAction())
1212 * //do async stuff
@@ -17,71 +17,18 @@ package org.reduxkotlin
1717 *
1818 * store.dispatch(myNetworkThunk("query"))
1919 */
20- @Suppress(" UNCHECKED_CAST" )
21- fun <State > createThunkMiddleware (extraArgument : Any? = null): ThunkMiddleware <State > =
22- { store: Store <State > ->
23- { next: Dispatcher ->
24- { action: Any ->
25- if (action is Thunk <* >) {
26- try {
27- (action as Thunk <State >).dispatch(store.dispatch, store.getState, extraArgument)
28- } catch (e: Exception ) {
29- // Logger.d("Dispatching functions must use type Thunk: " + e.message)
30- throw IllegalArgumentException ()
31- }
32- } else {
33- next(action)
34- }
35- }
36- }
37- }
38-
20+ typealias Thunk <State > = (dispatch: Dispatcher , getState: GetState <State >, extraArg: Any? ) -> Any
3921typealias ThunkMiddleware <State > = Middleware <State >
4022
4123fun <State > ThunkMiddleware<State>.withExtraArgument (arg : Any? ) = createThunkMiddleware<State >(arg)
4224
43- /* *
44- * Interface that can be dispatched and handled by ThunkMiddleware.
45- * Asynchronous operations and Actions may be dispatched from within a Thunk.
46- * Due to limitation of K/N a type alias does not work currently.
47- */
48- interface Thunk <State > {
49- fun dispatch (dispatch : Dispatcher , getState : GetState <State >, extraArgument : Any? ): Any
50- }
51- /* *
52- * Convenience function for creating thunks.
53- * Usage:
54- * val myThunk = createThunk { dispatch, getState, extraArgument ->
55- * //do async stuff
56- * dispatch(NewAction())
57- * }
58- *
59- * ....
60- *
61- * store.dispatch(myThunk)
62- *
63- * DEV NOTE: This will not be needed if/when typealias for Thunk works with K/N
64- */
65- fun <State > createThunk (thunkLambda : (dispatch: Dispatcher , getState: GetState <State >, extraArgument: Any? ) -> Any ): Thunk <State > {
66- return object : Thunk <State > {
67- override fun dispatch (dispatch : Dispatcher , getState : GetState <State >, extraArgument : Any? ): Any =
68- thunkLambda(dispatch, getState, extraArgument)
69- }
70- }
71-
72- /*
73- * Unable to use Thunk as a typealias currently do to a limitation of kotlin native.
74- * Dependent on https://youtrack.jetbrains.com/issue/KT-33149
75- * Leaving code here as a reference to revisit once the above is fixed.
76- * /
77- typealias Thunk = (Dispatcher, GetState, Any?) -> Any
78- fun createThunkMiddleware(extraArgument: Any? = null): ThunkMiddleware =
25+ fun <State > createThunkMiddleware (extraArgument : Any? = null): ThunkMiddleware <State > =
7926 { store ->
8027 { next: Dispatcher ->
8128 { action: Any ->
8229 if (action is Function <* >) {
8330 try {
84- (action as Thunk)(store.dispatch, store.getState, extraArgument)
31+ (action as Thunk < * > )(store.dispatch, store.getState, extraArgument)
8532 } catch (e: Exception ) {
8633 throw IllegalArgumentException ()
8734// Logger.d("Dispatching functions must use type Thunk: " + e.message)
@@ -92,6 +39,3 @@ fun createThunkMiddleware(extraArgument: Any? = null): ThunkMiddleware =
9239 }
9340 }
9441 }
95- */
96-
97-
0 commit comments