Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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/'));
```