Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/brianegan/redux_thunk
Redux Middleware for handling functions as actions
https://github.com/brianegan/redux_thunk
dart flutter redux redux-thunk
Last synced: 4 days ago
JSON representation
Redux Middleware for handling functions as actions
- Host: GitHub
- URL: https://github.com/brianegan/redux_thunk
- Owner: brianegan
- License: mit
- Created: 2017-11-25T22:50:14.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2021-03-18T15:26:39.000Z (almost 4 years ago)
- Last Synced: 2024-12-10T21:24:49.558Z (16 days ago)
- Topics: dart, flutter, redux, redux-thunk
- Language: Dart
- Size: 20.5 KB
- Stars: 89
- Watchers: 3
- Forks: 8
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# redux_thunk
[![Build Status](https://travis-ci.org/brianegan/redux_thunk.svg?branch=master)](https://travis-ci.org/brianegan/redux_thunk) [![codecov](https://codecov.io/gh/brianegan/redux_thunk/branch/master/graph/badge.svg)](https://codecov.io/gh/brianegan/redux_thunk)
[Redux](https://pub.dartlang.org/packages/redux) provides a simple way to update a your application's State in response to synchronous Actions. However, it lacks tools to handle asynchronous code. This is where Thunks come in.
The `thunkMiddleware` intercepts and calls `ThunkAction`s, which is simply a fancy name for any function that takes 1 argument: a Redux Store. This allows you to dispatch functions (aka `ThunkAction`s) to your Store that can perform asynchronous work, then dispatch actions using the Store after the work is complete.
The dispatched `ThunkAction`s will be swallowed, meaning they will not go through the rest of your middleware to the `Store`'s `Reducer`.
### Example
```dart
// First, create a quick reducer
State reducer(String state, action) =>
action is String ? action : state;// Next, apply the `thunkMiddleware` to the Store
final store = new Store(
reducer,
middleware: [thunkMiddleware],
);// Create a `ThunkAction`, which is any function that accepts the
// Store as it's only argument. Our function (aka ThunkAction) will
// simply send an action after 1 second. This is just an example,
// but in real life, you could make a call to an HTTP service or
// database instead!
void action(Store store) async {
final searchResults = await new Future.delayed(
new Duration(seconds: 1),
() => "Search Results",
);store.dispatch(searchResults);
}// You can also use a function with some parameters to create a thunk,
// and then use it like so: store.dispatch(waitAndDispatch(3));
ThunkAction waitAndDispatch(int secondsToWait) {
return (Store store) async {
final searchResults = await new Future.delayed(
new Duration(seconds: secondsToWait),
() => "Search Results",
);store.dispatch(searchResults);
};
}```
## CreditsAll the ideas in this lib are shamelessly stolen from the original [redux-thunk](https://github.com/gaearon/redux-thunk) library and simply adapted to Dart.