1313![ badge] [ badge-wasm ]
1414
1515A redux Thunk implementation for async action dispatch.
16+ Thunk implement must implement the ` Thunk ` interface, which only has one dispatch method.
17+ A more friendly way to create a thunk is with the ` createThunk ` function. Both are illustrated below:
1618
1719```
1820 val store = createStore(::reducer, applymiddleware(thunk))
1921
2022 ...
2123
22- fun fetchFoo(dispatch: Dispatcher, getState: GetState, extraArg: Any?) {
23- dispatch(FetchingFooAction)
24- launch {
25- val result = api.foo()
26- if (result.isSuccessful()) {
27- dispatch(FetchFooSuccess(result.payload)
28- } else {
29- dispatch(FetchFooFailure(result.message)
24+ class FooThunk: Thunk
25+
26+ override fun dispatch(dispatch: Dispatcher, getState: GetState, extraArg: Any?) {
27+ dispatch(FetchingFooAction)
28+ launch {
29+ val result = api.foo()
30+ if (result.isSuccessful()) {
31+ dispatch(FetchFooSuccess(result.payload)
32+ } else {
33+ dispatch(FetchFooFailure(result.message)
34+ }
3035 }
31- }
36+ }
3237 }
3338
34- val fetchBar: Thunk = {dispatch, getState, extraArgument ->
39+ val fetchBar = createThunk {dispatch, getState, extraArgument ->
3540 dispatch(FetchingBarAction)
3641 launch {
3742 val result = api.bar()
@@ -45,7 +50,7 @@ A redux Thunk implementation for async action dispatch.
4550 ...
4651
4752 fun bar() {
48- dispatch(::fetchFoo )
53+ dispatch(FooThunk()::dispatch )
4954 dispatch(fetchBar)
5055 }
5156```
@@ -59,15 +64,15 @@ kotlin {
5964 sourceSets {
6065 commonMain { // <--- name may vary on your project
6166 dependencies {
62- implementation "org.reduxkotlin:redux-kotlin-thunk:0.2.5 "
67+ implementation "org.reduxkotlin:redux-kotlin-thunk:0.2.6 "
6368 }
6469 }
6570 }
6671```
6772
6873For JVM only:
6974```
70- implementation "org.reduxkotlin:redux-kotlin-jvm-thunk:0.2.5 "
75+ implementation "org.reduxkotlin:redux-kotlin-jvm-thunk:0.2.6 "
7176```
7277
7378[ badge-android ] : http://img.shields.io/badge/platform-android-brightgreen.svg?style=flat
0 commit comments