git.fiddlerwoaroof.com
Browse code

Deprecate external middleware, apply it in the enhancer

Ed L authored on 09/07/2017 18:35:43
Showing 2 changed files
... ...
@@ -289,10 +289,12 @@ function createActionDispatcher(routesConfig, window) {
289 289
       this.store = store;
290 290
     },
291 291
     enhanceStore(nextStoreCreator) {
292
+      let middleware = buildMiddleware(this);
292 293
       return (reducer, finalInitialState, enhancer) => {
293 294
         let theStore = nextStoreCreator(reducer, finalInitialState, enhancer);
294 295
         this.activateDispatcher(theStore);
295 296
         theStore.pathForAction = pathForAction;
297
+        theStore.dispatch = middleware(theStore)(theStore.dispatch.bind(theStore));
296 298
         return theStore;
297 299
       };
298 300
     },
... ...
@@ -307,10 +309,14 @@ function createActionDispatcher(routesConfig, window) {
307 309
     },
308 310
 
309 311
     onLocationChanged(newLoc, cb) {
312
+      let result = undefined;
313
+
310 314
       if (this.currentLocation !== newLoc) {
311 315
         this.currentLocation = newLoc;
312
-        return cb();
316
+        result = cb();
313 317
       }
318
+
319
+      return result;
314 320
     },
315 321
 
316 322
     receiveLocation(location) {
... ...
@@ -357,7 +363,13 @@ export default function installBrowserRouter(routesConfig, window) {
357 363
 
358 364
   const actionDispatcher = createActionDispatcher(routesConfig, window);
359 365
 
360
-  const middleware = buildMiddleware(actionDispatcher);
366
+  const middleware = x => {
367
+    console.warn(
368
+      'Using the routedux middleware directly is deprecated, the enhancer now applies it automatically'
369
+      + ' and the middleware is now a no-op that will be removed in later versions.'
370
+    );
371
+    return y => y;
372
+  };
361 373
 
362 374
   return {middleware, enhancer: actionDispatcher.enhanceStore, init: actionDispatcher.receiveLocation.bind(actionDispatcher, window.location)};
363 375
 }
... ...
@@ -83,15 +83,12 @@ function setupTest(routesConfig, path='/path/to/thing') {
83 83
   addMissingHistoryEvents(window, window.history);
84 84
   addChangeUrlEvent(window, window.history);
85 85
 
86
-  const {middleware, enhancer, init} = installBrowserRouter(routesConfig, window);
86
+  const {enhancer, init} = installBrowserRouter(routesConfig, window);
87 87
   const reduce = jest.fn();
88 88
 
89 89
   const store = createStore(
90 90
     reduce,
91
-    compose(
92
-      enhancer,
93
-      applyMiddleware(
94
-        middleware)));
91
+    enhancer);
95 92
 
96 93
   function urlChanges() {
97 94
     return mockPushState.mock.calls.map(item => item[2]);