https://github.com/ksachdeva/redux-observable-extensions
Useful extensions to redux-observable
https://github.com/ksachdeva/redux-observable-extensions
Last synced: about 1 year ago
JSON representation
Useful extensions to redux-observable
- Host: GitHub
- URL: https://github.com/ksachdeva/redux-observable-extensions
- Owner: ksachdeva
- License: mit
- Created: 2016-09-17T22:11:51.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-09-18T00:15:21.000Z (over 9 years ago)
- Last Synced: 2025-02-04T22:51:47.429Z (about 1 year ago)
- Language: TypeScript
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# redux-observable-extensions
Useful extensions for [redux-observable](https://github.com/redux-observable/redux-observable)
```bash
npm install redux-observable-extensions --save
```
## Epic decorator
If you want to have your epic functions inside a class for e.g.
```typescript
class MyEpics {
epic1$ = (action$: ActionsObservable) =>
action$.ofType(MyActions.AN_ACTION_1)
.mergeMap(() =>
this.myService.doSomething()
.map(res => this.myActions.anotherAction())
);
epic2$ = (action$: ActionsObservable) =>
action$.ofType(MyActions.AN_ACTION_2)
.mergeMap(() =>
this.myService.doSomethingElse()
.map(res => this.myActions.yetAnotherAction())
);
constructor(
private myActions: MyActions,
private myService: MyService) {
}
}
```
then you would something like this
```typescript
import { createEpicMiddleware, combineEpics } from 'redux-observable';
const myEpics = ... // create the object or have it dependency injected
const combinedEpics = combineEpics(
myEpics.epic1$,
myEpics.epic2$
);
const middleware = [
createEpicMiddleware(combinedEpics)
];
```
The Epic decorator introduced as part of this module let's you do following.
```typescript
import { Epic } from 'redux-observable-extensions';
class MyEpics {
@Epic() epic1$ = (action$: ActionsObservable) =>
action$.ofType(MyActions.AN_ACTION_1)
.mergeMap(() =>
this.myService.doSomething()
.map(res => this.myActions.anotherAction())
);
@Epic() epic2$ = (action$: ActionsObservable) =>
action$.ofType(MyActions.AN_ACTION_2)
.mergeMap(() =>
this.myService.doSomethingElse()
.map(res => this.myActions.yetAnotherAction())
);
constructor(
private myActions: MyActions,
private myService: MyService) {
}
}
```
and when it comes the time to create the middleware simply do
```typescript
import { mergeEpics } from 'redux-observable-extensions';
const combinedEpics = mergeEpics(myEpics);
const middleware = [
createEpicMiddleware(combinedEpics)
];
```