1- package org.reduxkotlin
1+ package com.willowtreeapps.common.external
2+
3+ import org.reduxkotlin.Dispatcher
4+ import org.reduxkotlin.GetState
5+ import org.reduxkotlin.Middleware
6+ import org.reduxkotlin.Store
7+
28
39/* *
410 * Thunk middleware for async action dispatches.
@@ -17,13 +23,14 @@ package org.reduxkotlin
1723 *
1824 * store.dispatch(myNetworkThunk("query"))
1925 */
20- fun createThunkMiddleware (extraArgument : Any? = null): ThunkMiddleware =
21- { store ->
26+ @Suppress(" UNCHECKED_CAST" )
27+ fun <State > createThunkMiddleware (extraArgument : Any? = null): ThunkMiddleware <State > =
28+ { store: Store <State > ->
2229 { next: Dispatcher ->
2330 { action: Any ->
24- if (action is Thunk ) {
31+ if (action is Thunk < * > ) {
2532 try {
26- action.dispatch(store.dispatch, store.getState, extraArgument)
33+ ( action as Thunk < State >) .dispatch(store.dispatch, store.getState, extraArgument)
2734 } catch (e: Exception ) {
2835// Logger.d("Dispatching functions must use type Thunk: " + e.message)
2936 throw IllegalArgumentException ()
@@ -35,18 +42,17 @@ fun createThunkMiddleware(extraArgument: Any? = null): ThunkMiddleware =
3542 }
3643 }
3744
38- typealias ThunkMiddleware = Middleware
39- val thunk = createThunkMiddleware()
45+ typealias ThunkMiddleware <State > = Middleware <State >
4046
41- fun ThunkMiddleware.withExtraArgument (arg : Any? ) = createThunkMiddleware(arg)
47+ fun < State > ThunkMiddleware<State> .withExtraArgument (arg : Any? ) = createThunkMiddleware< State > (arg)
4248
4349/* *
4450 * Interface that can be dispatched and handled by ThunkMiddleware.
4551 * Asynchronous operations and Actions may be dispatched from within a Thunk.
4652 * Due to limitation of K/N a type alias does not work currently.
4753 */
48- interface Thunk {
49- fun dispatch (dispatch : Dispatcher , getState : GetState , extraArgument : Any? ): Any
54+ interface Thunk < State > {
55+ fun dispatch (dispatch : Dispatcher , getState : GetState < State > , extraArgument : Any? ): Any
5056}
5157/* *
5258 * Convenience function for creating thunks.
@@ -62,16 +68,17 @@ interface Thunk {
6268 *
6369 * DEV NOTE: This will not be needed if/when typealias for Thunk works with K/N
6470 */
65- fun createThunk (thunkLambda : (dispatch: Dispatcher , getState: GetState , extraArgument: Any? ) -> Any ): Thunk {
66- return object : Thunk {
67- override fun dispatch (dispatch : Dispatcher , getState : GetState , extraArgument : Any? ): Any =
71+ fun < State > createThunk (thunkLambda : (dispatch: Dispatcher , getState: GetState < State > , extraArgument: Any? ) -> Any ): Thunk < State > {
72+ return object : Thunk < State > {
73+ override fun dispatch (dispatch : Dispatcher , getState : GetState < State > , extraArgument : Any? ): Any =
6874 thunkLambda(dispatch, getState, extraArgument)
6975 }
7076}
7177
7278/*
7379 * Unable to use Thunk as a typealias currently do to a limitation of kotlin native.
74- * Leaving code here as a reference to revisit.
80+ * Dependent on https://youtrack.jetbrains.com/issue/KT-33149
81+ * Leaving code here as a reference to revisit once the above is fixed.
7582 * /
7683typealias Thunk = (Dispatcher, GetState, Any?) -> Any
7784fun createThunkMiddleware(extraArgument: Any? = null): ThunkMiddleware =
0 commit comments