Permalink
Browse files

feat(combineDelegators): add a method to combine different delegators…

… to make it easier to create a rootDelegator
  • Loading branch information...
benlesh committed May 24, 2016
1 parent 2dbc3b3 commit da2eeaf2a9159f63d17398cac26861d49d2fd881
Showing with 43 additions and 0 deletions.
  1. +7 −0 src/combineDelegators.js
  2. +1 −0 src/index.js
  3. +35 −0 test/combineDelegators-spec.js
@@ -0,0 +1,7 @@
import { merge } from 'rxjs/observable/merge';

/**
merges all delegators into a single delegator.
*/
export const combineDelegators = (...delegators) => (actions, store) =>
merge(...(delegators.map((delegator) => delegator(actions, store))));
@@ -1,2 +1,3 @@
export { reduxObservable } from './reduxObservable';
export { ActionsObservable } from './ActionsObservable';
export { combineDelegators } from './combineDelegators';
@@ -0,0 +1,35 @@
/* globals describe it */
import { expect } from 'chai';
import { combineDelegators, ActionsObservable } from '../';
import { Subject } from 'rxjs/Subject';
import { map } from 'rxjs/operator/map';

describe('combineDelegators', () => {
it('should combine delegators', () => {
let delegator1 = (actions, store) =>
actions.ofType('ACTION1')::map(action => ({ type: 'DELEGATED1', action, store }));
let delegator2 = (actions, store) =>
actions.ofType('ACTION2')::map(action => ({ type: 'DELEGATED2', action, store }));

let delegator = combineDelegators(
delegator1,
delegator2
);

let store = { I: 'am', a: 'store' };
let subject = new Subject();
let actions = new ActionsObservable(subject);
let result = delegator(actions, store);
let delegatedActions = [];

result.subscribe(delegatedAction => delegatedActions.push(delegatedAction));

subject.next({ type: 'ACTION1' });
subject.next({ type: 'ACTION2' });

expect(delegatedActions).to.deep.equal([
{ type: 'DELEGATED1', action: { type: 'ACTION1' }, store },
{ type: 'DELEGATED2', action: { type: 'ACTION2' }, store },
]);
});
});

0 comments on commit da2eeaf

Please sign in to comment.