Skip to content

Commit ea8eccc

Browse files
committed
Fix the remaining dependency on the Store function name (#16)
1 parent eb80a2b commit ea8eccc

File tree

4 files changed

+31
-13
lines changed

4 files changed

+31
-13
lines changed

examples/counter/App.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import Counter from './Counter';
88
export default class CounterApp extends Component {
99
render() {
1010
return (
11-
<Container stores={[stores.counterStore]} actions={{ increment, decrement }}>
11+
<Container stores={[stores.counterStore]}
12+
actions={{ increment, decrement }}>
1213
{props => <Counter {...props} />}
1314
</Container>
1415
);

examples/counter/stores/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export counterStore from './counterStore';
1+
export counterStore from './counterStore';

src/Container.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Component, PropTypes } from 'react';
22
import mapValues from 'lodash/object/mapValues';
33
import identity from 'lodash/utility/identity';
4+
import invariant from 'invariant';
5+
import isPlainObject from 'lodash/lang/isPlainObject';
46

57
export default class ReduxContainer extends Component {
68
static contextTypes = {
@@ -33,18 +35,29 @@ export default class ReduxContainer extends Component {
3335
}
3436

3537
update(props) {
36-
const { wrapActionCreator, observeStores } = this.context.redux;
38+
const { stores, actions } = props;
39+
invariant(
40+
isPlainObject(actions) &&
41+
Object.keys(actions).every(key => typeof actions[key] === 'function'),
42+
'"actions" must be a plain object with functions as values. Did you misspell an import?'
43+
);
44+
invariant(
45+
Array.isArray(stores) &&
46+
stores.every(s => typeof s === 'function'),
47+
'"stores" must be an array of functions. Did you misspell an import?'
48+
);
49+
50+
const { wrapActionCreator, observeStores, getStoreKey } = this.context.redux;
3751
this.actions = mapValues(props.actions, wrapActionCreator);
52+
3853
if (this.unsubscribe) {
3954
this.unsubscribe();
4055
}
4156

42-
const { stores } = props;
43-
const mapState = (stores.length === 1) ?
44-
state => state[stores[0].name] :
57+
this.mapState = (stores.length === 1) ?
58+
state => state[getStoreKey(stores[0])] :
4559
identity;
4660

47-
this.mapState = mapState;
4861
this.unsubscribe = observeStores(stores, this.handleChange);
4962
}
5063

src/createDispatcher.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,16 @@ export default function createDispatcher() {
8181
dispatch(BOOTSTRAP_STORE);
8282
}
8383

84+
// Get the key a store was registered with
85+
function getStoreKey(store) {
86+
const key = storeKeys[store];
87+
invariant(key, 'This store is not registered with the Redux root: %s', store);
88+
return key;
89+
}
90+
8491
// Provide subscription and unsubscription
8592
function observeStores(observedStores, onChange) {
86-
const observedKeys = observedStores.map(store => {
87-
const key = storeKeys[store];
88-
invariant(key, 'This store is not registered with the Redux root: %s', store);
89-
return key;
90-
});
93+
const observedKeys = observedStores.map(getStoreKey);
9194

9295
// Emit the state update
9396
function handleChange() {
@@ -128,6 +131,7 @@ export default function createDispatcher() {
128131
return {
129132
wrapActionCreator,
130133
observeStores,
131-
receiveStores
134+
receiveStores,
135+
getStoreKey
132136
};
133137
}

0 commit comments

Comments
 (0)