Skip to content

Commit

Permalink
Fix the remaining dependency on the Store function name (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Jun 3, 2015
1 parent eb80a2b commit ea8eccc
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
3 changes: 2 additions & 1 deletion examples/counter/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import Counter from './Counter';
export default class CounterApp extends Component {
render() {
return (
<Container stores={[stores.counterStore]} actions={{ increment, decrement }}>
<Container stores={[stores.counterStore]}
actions={{ increment, decrement }}>
{props => <Counter {...props} />}
</Container>
);
Expand Down
2 changes: 1 addition & 1 deletion examples/counter/stores/index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export counterStore from './counterStore';
export counterStore from './counterStore';
23 changes: 18 additions & 5 deletions src/Container.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Component, PropTypes } from 'react';
import mapValues from 'lodash/object/mapValues';
import identity from 'lodash/utility/identity';
import invariant from 'invariant';
import isPlainObject from 'lodash/lang/isPlainObject';

export default class ReduxContainer extends Component {
static contextTypes = {
Expand Down Expand Up @@ -33,18 +35,29 @@ export default class ReduxContainer extends Component {
}

update(props) {
const { wrapActionCreator, observeStores } = this.context.redux;
const { stores, actions } = props;
invariant(
isPlainObject(actions) &&
Object.keys(actions).every(key => typeof actions[key] === 'function'),
'"actions" must be a plain object with functions as values. Did you misspell an import?'
);
invariant(
Array.isArray(stores) &&
stores.every(s => typeof s === 'function'),
'"stores" must be an array of functions. Did you misspell an import?'
);

const { wrapActionCreator, observeStores, getStoreKey } = this.context.redux;
this.actions = mapValues(props.actions, wrapActionCreator);

if (this.unsubscribe) {
this.unsubscribe();
}

const { stores } = props;
const mapState = (stores.length === 1) ?
state => state[stores[0].name] :
this.mapState = (stores.length === 1) ?
state => state[getStoreKey(stores[0])] :
identity;

this.mapState = mapState;
this.unsubscribe = observeStores(stores, this.handleChange);
}

Expand Down
16 changes: 10 additions & 6 deletions src/createDispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,16 @@ export default function createDispatcher() {
dispatch(BOOTSTRAP_STORE);
}

// Get the key a store was registered with
function getStoreKey(store) {
const key = storeKeys[store];
invariant(key, 'This store is not registered with the Redux root: %s', store);
return key;
}

// Provide subscription and unsubscription
function observeStores(observedStores, onChange) {
const observedKeys = observedStores.map(store => {
const key = storeKeys[store];
invariant(key, 'This store is not registered with the Redux root: %s', store);
return key;
});
const observedKeys = observedStores.map(getStoreKey);

// Emit the state update
function handleChange() {
Expand Down Expand Up @@ -128,6 +131,7 @@ export default function createDispatcher() {
return {
wrapActionCreator,
observeStores,
receiveStores
receiveStores,
getStoreKey
};
}

0 comments on commit ea8eccc

Please sign in to comment.