Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/davidmarne/built_redux_rx
- Owner: davidmarne
- License: mit
- Created: 2017-11-01T04:07:18.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2018-08-07T04:16:33.000Z (over 6 years ago)
- Last Synced: 2024-10-23T04:42:49.741Z (3 months ago)
- Language: Dart
- Size: 9.77 KB
- Stars: 6
- Watchers: 3
- Forks: 4
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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/');
```