Skip to content

Commit a38a132

Browse files
committed
Add rest parameter
1 parent 8815d2c commit a38a132

File tree

5 files changed

+34
-6
lines changed

5 files changed

+34
-6
lines changed

examples/counter/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"react-dom": "^0.14.0",
99
"react-redux": "^4.0.0",
1010
"redux": "^3.0.0",
11+
"redux-actions": "^0.9.1",
1112
"redux-thunk": "^1.0.0"
1213
},
1314
"devDependencies": {

examples/counter/src/actions/CounterActions.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,11 @@ export function incrementAsync() {
3131
}, 1000);
3232
};
3333
}
34+
35+
export function add(...rest) {
36+
const compute = rest.concat([0]).reduce((prev, next) => prev + next);
37+
const action = compute < 0 ? decrement() : increment();
38+
return dispatch => {
39+
Array.apply(null, Array(Math.abs(compute))).forEach(() => dispatch(action));
40+
};
41+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { createAction } from 'redux-actions';
2+
3+
export const increment = createAction('INCREMENT_COUNTER');
4+
export const decrement = createAction('DECREMENT_COUNTER', payload => payload, ({meta}) => meta);

examples/counter/src/containers/DevTools.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ import MultipleMonitors from 'redux-devtools-multiple-monitors';
66
import Dispatcher from 'redux-devtools-dispatch';
77

88
import * as actionCreators from '../actions/CounterActions';
9+
import * as reduxActionCreators from '../actions/CounterReduxActions';
910

1011
export default createDevTools(
1112
<DockMonitor toggleVisibilityKey='ctrl-h'
1213
changePositionKey='ctrl-q'>
1314
<MultipleMonitors>
1415
<LogMonitor />
15-
<Dispatcher actionCreators={actionCreators} initEmpty />
16+
<Dispatcher actionCreators={{...actionCreators, reduxActions: reduxActionCreators}} initEmpty />
1617
</MultipleMonitors>
1718
</DockMonitor>
1819
);

src/Dispatcher.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,16 @@ export default class Dispatcher extends Component {
102102
let actionCreator = () => ({}), argsToInject = [];
103103
if(this.state.selectedActionCreator !== 'default') {
104104
actionCreator = this.getSelectedActionCreator().func;
105-
argsToInject = this.state.args.map(
106-
(arg) => (new Function('return ' + arg))()
107-
);
105+
106+
const interpretArg = (arg) => (new Function('return ' + arg))()
107+
argsToInject = this.state.args.map(interpretArg);
108+
const rest = interpretArg(this.refs.restArgs.textContent);
109+
if(rest) {
110+
if(Array.isArray(rest))
111+
argsToInject = argsToInject.concat(...rest);
112+
else
113+
throw new Error('rest must be an array');
114+
}
108115
} else {
109116
actionCreator = new Function('return ' + this.refs.action.textContent);
110117
}
@@ -186,12 +193,19 @@ export default class Dispatcher extends Component {
186193

187194
let fields = <div contentEditable style={contentEditableStyle} ref='action'></div>;
188195
if(this.state.selectedActionCreator !== 'default') {
196+
const fieldStyles = {...styles.label, color: theme.base06};
189197
fields = this.getSelectedActionCreator().args.map((param, i) => (
190198
<div key={i} style={{display: 'flex'}}>
191-
<span style={{...styles.label, color: theme.base06}}>{param}</span>
192-
<div contentEditable style={contentEditableStyle} ref={'arg' + i} onInput={(e) => this.handleArg(e, i)}></div>
199+
<span style={fieldStyles}>{param}</span>
200+
<div contentEditable style={contentEditableStyle} ref={'arg' + i} onInput={(e) => this.handleArg(e, i)} />
193201
</div>
194202
));
203+
fields.push(
204+
<div key="action" style={{display: 'flex'}}>
205+
<span style={fieldStyles}>rest...</span>
206+
<div contentEditable style={contentEditableStyle} ref="restArgs" />
207+
</div>
208+
);
195209
}
196210

197211
let error = '';

0 commit comments

Comments
 (0)