Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/davidmarne/built_redux_thunk
thunk middleware for built_redux
https://github.com/davidmarne/built_redux_thunk
Last synced: 2 months ago
JSON representation
thunk middleware for built_redux
- Host: GitHub
- URL: https://github.com/davidmarne/built_redux_thunk
- Owner: davidmarne
- License: mit
- Created: 2017-08-29T05:42:01.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-08-11T15:55:16.000Z (over 4 years ago)
- Last Synced: 2024-10-23T04:42:41.246Z (3 months ago)
- Language: Dart
- Size: 18.6 KB
- Stars: 6
- Watchers: 2
- Forks: 4
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Thunks
By adding the thunkMiddleware to any of your built_redux stores, any action dispatched
that's payload meets the typedef signature ```dynamic thunk(MiddlewareApi api)```
will be called.### Example
Create a function that returns a thunk configured with an url,
that dispatches an action (`onRequestResolved`) in async code:```dart
// api's type is inferred
Thunk httpRequestThunk(String url) => (api) {
// call the web server asynchronously.
HttpRequest.getString(url)
.then(api.actions.onRequestResolved);
};
```Add an thunk dispatcher to your ReduxActions:
```dart
abstract class CounterActions extends ReduxActions {
ActionDispatcher> thunkDispatcher;
ActionDispatcher onRequestResolved;// factory to create on instance of the generated implementation of CounterActions
CounterActions._();
factory CounterActions() => new _$CounterActions();
}
```The action with `Thunk` isn't mapped to a reducer.
Register the `Thunk` middleware:
```dart
var store = new Store(
createReducer(),
new Counter(),
new CounterActions(),
middleware: >[
thunkMiddleware
],
);
```Dispatch the thunk
```dart
store.actions.thunkDispatcher(httpRquestThunk('http://example.com/'));
```