-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8bd4e86
commit e869b13
Showing
3 changed files
with
47 additions
and
0 deletions.
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,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; |
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,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); | ||
}); |