From 96313a71e0c33227f3d8933197d1829a117afd4f Mon Sep 17 00:00:00 2001 From: Aaron Jensen Date: Sun, 14 Jun 2015 11:06:55 -0700 Subject: [PATCH 1/4] Add failing test for #90 --- test/createRedux.spec.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/createRedux.spec.js b/test/createRedux.spec.js index 977f5f6289..d407188fd1 100644 --- a/test/createRedux.spec.js +++ b/test/createRedux.spec.js @@ -53,4 +53,17 @@ describe('createRedux', () => { ]); expect(changeListenerSpy.calls.length).toBe(1); }); + + it('should use existing state when replacing the dispatcher', () => { + redux.dispatch(addTodo('Hello')); + + let nextRedux = createRedux({ todoStore }); + redux.replaceDispatcher(nextRedux.getDispatcher()); + + let action = (_, getState) => { + expect(getState().todoStore).toEqual(redux.getState().todoStore); + }; + + nextRedux.dispatch(action); + }); }); From 22180bd693175ee83d9e6d97fe4dc1f3b467d2b3 Mon Sep 17 00:00:00 2001 From: Aaron Jensen Date: Sun, 14 Jun 2015 11:10:24 -0700 Subject: [PATCH 2/4] Make test fail even if dispatch fails --- test/createRedux.spec.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/createRedux.spec.js b/test/createRedux.spec.js index d407188fd1..6dfbbe6ae7 100644 --- a/test/createRedux.spec.js +++ b/test/createRedux.spec.js @@ -60,10 +60,13 @@ describe('createRedux', () => { let nextRedux = createRedux({ todoStore }); redux.replaceDispatcher(nextRedux.getDispatcher()); + let state; let action = (_, getState) => { - expect(getState().todoStore).toEqual(redux.getState().todoStore); + state = getState().todoStore; }; nextRedux.dispatch(action); + + expect(state).toEqual(redux.getState().todoStore); }); }); From 54f8e3c0c7176a3042ec346d80ac55fbb6c9928e Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Sun, 14 Jun 2015 21:47:06 +0300 Subject: [PATCH 3/4] The old Redux instance should be used for dispatching --- test/createRedux.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/createRedux.spec.js b/test/createRedux.spec.js index 6dfbbe6ae7..1900de351c 100644 --- a/test/createRedux.spec.js +++ b/test/createRedux.spec.js @@ -65,7 +65,7 @@ describe('createRedux', () => { state = getState().todoStore; }; - nextRedux.dispatch(action); + redux.dispatch(action); expect(state).toEqual(redux.getState().todoStore); }); From 4acf88a0354a8a04364826ca76a0777bb6b58a32 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Sun, 14 Jun 2015 22:08:52 +0300 Subject: [PATCH 4/4] Fix stale getState on hot reloading (#90) --- src/createDispatcher.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/createDispatcher.js b/src/createDispatcher.js index fee186f062..5b1b36bac9 100644 --- a/src/createDispatcher.js +++ b/src/createDispatcher.js @@ -13,10 +13,10 @@ export default function createDispatcher(store, middlewares = []) { return state; } - if (typeof middlewares === 'function') { - middlewares = middlewares(getState); - } + const finalMiddlewares = typeof middlewares === 'function' ? + middlewares(getState) : + middlewares; - return composeMiddleware(...middlewares, dispatch); + return composeMiddleware(...finalMiddlewares, dispatch); }; }