@@ -7,8 +7,9 @@ const BOOTSTRAP_STORE = {
77} ;
88
99export 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