Skip to content

Commit

Permalink
feat: added create-multi-action
Browse files Browse the repository at this point in the history
  • Loading branch information
BerkeleyTrue committed Aug 14, 2021
1 parent 8bd4e86 commit e869b13
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/create-multi-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { Action, AnyAction } from 'redux';

export interface ActionCreator<A extends Action = AnyAction> {
(...args: any[]): A;
}

/**
* Create a multi action. Useful when you have an event in a subdomains that triggers a
* a command action that triggers changes in parent domains.
*
* requires the use of arrayMiddleware
*/
const createMultiAction = (
event: ActionCreator,
...commands: ActionCreator[]
) => {
const actionCreator = (...args: any[]): Action[] => [
event(...args),
...commands.map((ac) => ac(...args)),
];

return actionCreator;
};

export default createMultiAction;
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export { default as handleActions } from './handle-actions';
export { default as arrayMiddleware } from './middlewares/array';
export { default as mapFromAction } from './map-from-action';
export type { MapAction, Updater } from './map-from-action';
export { default as createMultiAction } from './create-multi-action';
21 changes: 21 additions & 0 deletions test/create-multi-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import _ from 'lodash/fp';

import { createMultiAction, createAction } from '../src';

test('should create a multi action', () => {
const foo = createAction('foo');
const bar = createAction('bar');
const ac = createMultiAction(foo, bar);
const actual = ac();
expect(_.flow(_.head, _.get('type'))(actual)).toBe('foo');
expect(_.flow(_.tail, _.head, _.get('type'))(actual)).toBe('bar');
});

test('should create a multi action if only event', () => {
// don't know why you would do this.
const foo = createAction('foo');
const ac = createMultiAction(foo);
const actual = ac();
expect(_.flow(_.head, _.get('type'))(actual)).toBe('foo');
expect(_.flow(_.tail, _.head, _.get('type'))(actual)).toBe(undefined);
});

0 comments on commit e869b13

Please sign in to comment.