-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathactions.js
69 lines (64 loc) · 1.89 KB
/
actions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import _ from "lodash";
import { entries } from "./utils";
import { actionChannel } from "./bus";
export const actions = Object.create( null );
export const actionGroups = Object.create( null );
export function generateActionCreator( actionList ) {
actionList.forEach( function( action ) {
if ( !actions[ action ] ) {
actions[ action ] = function() {
const args = Array.from( arguments );
actionChannel.publish( {
topic: `execute.${ action }`,
data: {
actionType: action,
actionArgs: args
}
} );
};
}
} );
}
// This method is deprecated, but will remain as
// long as the print utils need it.
/* istanbul ignore next */
export function getActionGroup( group ) {
if ( actionGroups[ group ] ) {
return _.pick( actions, actionGroups[ group ] );
}
throw new Error( `There is no action group named '${ group }'` );
}
// This method is deprecated, but will remain as
// long as the print utils need it.
/* istanbul ignore next */
export function getGroupsWithAction( actionName ) {
const groups = [];
for ( const [ group, list ] of entries( actionGroups ) ) {
if ( list.indexOf( actionName ) >= 0 ) {
groups.push( group );
}
}
return groups;
}
export function customActionCreator( action ) {
Object.assign( actions, action );
}
// This method is deprecated, but will remain as
// long as the print utils need it.
/* istanbul ignore next */
export function addToActionGroup( groupName, actionList ) {
let group = actionGroups[ groupName ];
if ( !group ) {
group = actionGroups[ groupName ] = [];
}
actionList = typeof actionList === "string" ? [ actionList ] : actionList;
const diff = _.difference( actionList, Object.keys( actions ) );
if ( diff.length ) {
throw new Error( `The following actions do not exist: ${ diff.join( ", " ) }` );
}
actionList.forEach( function( action ) {
if ( group.indexOf( action ) === -1 ) {
group.push( action );
}
} );
}