Skip to content

Commit e9f7fa5

Browse files
authored
Update README.md
1 parent f935c98 commit e9f7fa5

File tree

1 file changed

+14
-27
lines changed

1 file changed

+14
-27
lines changed

README.md

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,32 @@
1313
![badge][badge-wasm]
1414

1515
A 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:
16+
A Thunk must conform to the `Thunk` typealias, which is a function with 3 paramaters: `dispatch`, `getState`, & `extraArg`.
17+
A common use is to make a function return a `Thunk`. This allows passing params to the function.
18+
19+
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.
1820

1921
```
2022
val store = createStore(::reducer, applymiddleware(createThunkMiddleware()))
2123
2224
...
2325
24-
class FooThunk: Thunk<State> {
25-
26-
override fun dispatch(dispatch: Dispatcher, getState: GetState<State>, 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-
}
35-
}
36-
}
37-
}
38-
39-
val fetchBar = createThunk<State> {dispatch, getState, extraArgument ->
40-
dispatch(FetchingBarAction)
26+
fun fooThunk(query: String): Thunk<AppState> = { dispatch, getState, extraArg ->
27+
dispatch(FetchingFooAction)
4128
launch {
42-
val result = api.bar()
29+
val result = api.foo(query)
4330
if (result.isSuccessful()) {
44-
dispatch(FetchBarSuccess(result.payload)
31+
dispatch(FetchFooSuccess(result.payload)
4532
} else {
46-
dispatch(FetchBarFailure(result.message)
47-
}
48-
}
49-
33+
dispatch(FetchFooFailure(result.message)
34+
}
35+
}
36+
}
37+
5038
...
5139
5240
fun bar() {
53-
dispatch(FooThunk()::dispatch)
54-
dispatch(fetchBar)
41+
dispatch(fooThunk("my query"))
5542
}
5643
```
5744

0 commit comments

Comments
 (0)