Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ofType): add operator to provided actions observable
This adds an `ofType` operator to the actions observable provided to the dispatched function that will be available anywhere in the operator chain for that observable. It basically filters by the action's type, which is a common need.
- Loading branch information
Showing
3 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Observable } from 'rxjs/Observable'; | ||
import { filter } from 'rxjs/operator/filter'; | ||
|
||
export class ActionsObservable extends Observable { | ||
constructor(actionsSubject) { | ||
super(); | ||
this.source = actionsSubject; | ||
} | ||
|
||
lift(operator) { | ||
const observable = new ActionsObservable(this); | ||
observable.operator = operator; | ||
return observable; | ||
} | ||
|
||
ofType(key) { | ||
return this::filter((action) => action.type === key); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* globals describe it */ | ||
import { expect } from 'chai'; | ||
import { ActionsObservable, reduxObservable } from '../'; | ||
import { createStore, applyMiddleware } from 'redux'; | ||
import { of } from 'rxjs/observable/of'; | ||
import { Subject } from 'rxjs/Subject'; | ||
|
||
describe('ActionsObservable', () => { | ||
it('should exist', () => { | ||
expect(ActionsObservable).to.be.a('function'); | ||
}); | ||
|
||
it('should be the type provided to a dispatched function', () => { | ||
let middleware = reduxObservable(); | ||
let reducer = (state, action) => { | ||
return state; | ||
}; | ||
|
||
let store = createStore(reducer, applyMiddleware(middleware)); | ||
|
||
store.dispatch((arg1) => { | ||
expect(arg1).to.be.an.instanceof(ActionsObservable); | ||
return of({ type: 'WEEE' }); | ||
}); | ||
}); | ||
|
||
it('should have a ofType operator that filters by action type', () => { | ||
let actions = new Subject(); | ||
let actionsObs = new ActionsObservable(actions); | ||
let lulz = []; | ||
let haha = []; | ||
|
||
actionsObs.ofType('LULZ').subscribe(x => lulz.push(x)); | ||
actionsObs.ofType('HAHA').subscribe(x => haha.push(x)); | ||
|
||
actions.next({ type: 'LULZ', i: 0 }); | ||
|
||
expect(lulz).to.deep.equal([{ type: 'LULZ', i: 0 }]); | ||
expect(haha).to.deep.equal([]); | ||
|
||
actions.next({ type: 'LULZ', i: 1 }); | ||
|
||
expect(lulz).to.deep.equal([{ type: 'LULZ', i: 0 }, { type: 'LULZ', i: 1 }]); | ||
expect(haha).to.deep.equal([]); | ||
|
||
actions.next({ type: 'HAHA', i: 0 }); | ||
|
||
expect(lulz).to.deep.equal([{ type: 'LULZ', i: 0 }, { type: 'LULZ', i: 1 }]); | ||
expect(haha).to.deep.equal([{ type: 'HAHA', i: 0 }]); | ||
}); | ||
}); |