Skip to content

Commit 74dcd85

Browse files
committed
Make function name insignificant by binding Stores early. Fixes #16
1 parent 1830db3 commit 74dcd85

File tree

7 files changed

+35
-22
lines changed

7 files changed

+35
-22
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,11 @@ export default class Counter {
204204

205205
```js
206206
import React from 'react';
207-
import { Root } from 'redux';
207+
import { root } from 'redux';
208+
import * from './stores/index';
208209

209-
@Root
210+
// Let it know about all the stores
211+
@root(stores)
210212
export default class App {
211213
/* ... */
212214
}

examples/counter/App.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import React, { Component } from 'react';
22
import { root, Container } from 'redux';
33
import { increment, decrement } from './actions/CounterActions';
4-
import counterStore from './stores/counterStore';
4+
import * as stores from './stores/index';
55
import Counter from './Counter';
66

7-
@root
7+
@root(stores)
88
export default class CounterApp extends Component {
99
render() {
1010
return (
11-
<Container stores={[counterStore]} actions={{ increment, decrement }}>
11+
<Container stores={[stores.counterStore]} actions={{ increment, decrement }}>
1212
{props => <Counter {...props} />}
1313
</Container>
1414
);

examples/counter/stores/index.js

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

examples/todo/App.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import React from 'react';
22
import Header from './Header';
33
import Body from './Body';
44
import { root } from 'redux';
5+
import * as stores from './stores/index';
56

6-
@root
7+
@root(stores)
78
export default class TodoApp {
89
render() {
910
return (

src/Root.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,28 @@ import createDispatcher from './createDispatcher';
33

44
export default class ReduxRoot {
55
static propTypes = {
6+
stores: PropTypes.object.isRequired,
67
children: PropTypes.func.isRequired
78
};
89

910
static childContextTypes = {
1011
redux: PropTypes.object.isRequired
1112
};
1213

13-
constructor() {
14+
constructor(props) {
1415
this.dispatcher = createDispatcher();
16+
this.dispatcher.receiveStores(props.stores);
17+
}
18+
19+
componentWillReceiveProps(nextProps) {
20+
this.dispatcher.receiveStores(nextProps.stores);
1521
}
1622

1723
getChildContext() {
1824
return { redux: this.dispatcher };
1925
}
2026

2127
render() {
22-
return this.props.children({
23-
...this.props
24-
});
28+
return this.props.children();
2529
}
2630
}

src/addons/root.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import React from 'react';
22
import Root from '../Root';
33
import getDisplayName from './getDisplayName';
44

5-
export default function root(DecoratedComponent) {
6-
return class ReduxRootDecorator {
5+
export default function root(stores) {
6+
return DecoratedComponent => class ReduxRootDecorator {
77
static displayName = `ReduxRoot(${getDisplayName(DecoratedComponent)})`;
88

99
render() {
1010
return (
11-
<Root>
11+
<Root stores={stores}>
1212
{props => <DecoratedComponent {...this.props} {...props} />}
1313
</Root>
1414
);

src/createDispatcher.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ const BOOTSTRAP_STORE = {
77
};
88

99
export default function createDispatcher() {
10-
let observers = {};
11-
let stores = {};
10+
const observers = {};
11+
const stores = {};
12+
const storeKeys = new Map();
1213
let currentState = {};
1314

1415
// To compute the next state, combine the next states of every store
@@ -71,19 +72,22 @@ export default function createDispatcher() {
7172
}
7273

7374
// Merge the newly added stores
74-
function mergeStores(newStores) {
75-
newStores.forEach(newStore => {
76-
const key = newStore.name;
77-
stores[key] = newStore;
75+
function receiveStores(nextStores) {
76+
Object.keys(nextStores).forEach(key => {
77+
stores[key] = nextStores[key];
7878
observers[key] = observers[key] || [];
79+
storeKeys[stores[key]] = key;
7980
});
8081
dispatch(BOOTSTRAP_STORE);
8182
}
8283

8384
// Provide subscription and unsubscription
8485
function observeStores(observedStores, onChange) {
85-
mergeStores(observedStores);
86-
const observedKeys = observedStores.map(s => s.name);
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+
});
8791

8892
// Emit the state update
8993
function handleChange() {
@@ -123,6 +127,7 @@ export default function createDispatcher() {
123127

124128
return {
125129
wrapActionCreator,
126-
observeStores
130+
observeStores,
131+
receiveStores
127132
};
128133
}

0 commit comments

Comments
 (0)