built_redux_thunk 0.5.1
built_redux_thunk: ^0.5.1 copied to clipboard

Thunks for built_redux

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:

// api's type is inferred
Thunk<TestCounter, TestCounterBuilder, TestCounterActions> httpRequestThunk(String url) => (api) {
  // call the web server asynchronously.

Add an thunk dispatcher to your ReduxActions:

abstract class CounterActions extends ReduxActions {
  ActionDispatcher<Thunk<Counter, CounterBuilder, CounterActions>> thunkDispatcher;
  ActionDispatcher<String> onRequestResolved;

  // factory to create on instance of the generated implementation of CounterActions
  factory CounterActions() => new _$CounterActions();

The action with Thunk isn't mapped to a reducer.

Register the Thunk middleware:

var store = new Store<Counter, CounterBuilder, CounterActions>(
  new Counter(),
  new CounterActions(),
  middleware: <Middleware<TestCounter, TestCounterBuilder, TestCounterActions>>[

Dispatch the thunk