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_rx

epic middleware for built_redux
https://github.com/davidmarne/built_redux_rx

Last synced: 2 months ago
JSON representation

epic middleware for built_redux

Awesome Lists containing this project

README

        

## Rx

By adding the rx middleware to any of your built_redux stores, one can register side-effects, or epics, to run on any action. Epics provide a clean way to perform async operations in response to actions.

### Example

Create a function that listens to the action stream and makes an http request
on actions with the name CounterActionsNames.fetchData

```dart
Observable httpRequestEpic(Observable> stream,
MiddlewareApi mwApi) =>
stream
.where((a) => a.name == CounterActionsNames.fetchData)
.map((a) => a as Action)
.asyncMap(
(action) => HttpRequest
.getString(action.payload)
.then(api.actions.onRequestResolved),
);
```

```dart
var store = new Store(
createReducer(),
new Counter(),
new CounterActions(),
middleware: >[
createEpicMiddleware([httpRequestEpic]),
],
);
```

Dispatch the action

```dart
store.actions.fetchData('http://example.com/');
```

### EpicBuilder

EpicBuilder lets you map a given action to an epic thats action stream
is one that only fires on the given action. The payload of the action stream is typed!

Write the epic, notice the generic of Action is String.
```dart
Observable httpRequestEpic(Observable> stream,
MiddlewareApi mwApi) =>
stream
.asyncMap(
(action) => HttpRequest
.getString(action.payload)
.then(api.actions.onRequestResolved),
);
```

Write an Epic builder.
```dart
Iterable> createEpicBuilder() =>
(new EpicBuilder()
..add(CounterActionsNames.fetchData, httpRequestEpic)
.build();

```

```dart
var store = new Store(
createReducer(),
new Counter(),
new CounterActions(),
middleware: >[
createEpicMiddleware([httpRequestEpic]),
],
);
```

Dispatch the action

```dart
store.actions.fetchData('http://example.com/');
```