Redux-Kotlin
A redux Thunk implementation for async action dispatch. A Thunk must conform to the Thunk
typealias, which is a function with 3 paramaters: dispatch
, getState
, & extraArg
. A common use is to make a function return a Thunk
. This allows passing params to the function.
NOTE: Before v0.4.0 Thunk
was an interface. Kotlin 1.3.70 fixed a bug which allows using a typealias instead, which is more concise and closer to the JS implementation.
val store = createStore(::reducer, applymiddleware(createThunkMiddleware()))
...
fun fooThunk(query: String): Thunk<AppState> = { dispatch, getState, extraArg ->
dispatch(FetchingFooAction)
launch {
val result = api.foo(query)
if (result.isSuccessful()) {
dispatch(FetchFooSuccess(result.payload)
} else {
dispatch(FetchFooFailure(result.message)
}
}
}
...
fun bar() {
dispatch(fooThunk("my query"))
}
How to add to project:
Artifacts are hosted on maven central. For multiplatform, add the following to your shared module:
kotlin {
sourceSets {
commonMain { // <--- name may vary on your project
dependencies {
implementation "org.reduxkotlin:redux-kotlin-thunk:0.5.3"
}
}
}
For JVM only:
implementation "org.reduxkotlin:redux-kotlin-jvm-thunk:0.5.3"