From 6b76c98a25e05ae5c2636b41d84b511532388052 Mon Sep 17 00:00:00 2001 From: Gregory Beaver <greg.beaver.cello@gmail.com> Date: Fri, 16 Aug 2019 07:45:22 -0400 Subject: [PATCH 1/4] fix replaceReducer type --- index.d.ts | 10 +++++++++- src/createStore.js | 4 +++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index 50e8d018fa..d31a15ffea 100644 --- a/index.d.ts +++ b/index.d.ts @@ -326,7 +326,9 @@ export interface Store<S = any, A extends Action = AnyAction> { * * @param nextReducer The reducer for the store to use instead. */ - replaceReducer(nextReducer: Reducer<S, A>): void + replaceReducer<NewState = S, NewActions extends A = A>( + nextReducer: Reducer<NewState, NewActions> + ): Store<NewState, NewActions> /** * Interoperability point for observable/reactive libraries. @@ -667,3 +669,9 @@ export function compose<R>( ): (...args: any[]) => R export function compose<R>(...funcs: Function[]): (...args: any[]) => R + +export const __DO_NOT_USE__ActionTypes: { + INIT: string + REPLACE: string + PROBE_UNKNOWN_ACTION: () => string +} diff --git a/src/createStore.js b/src/createStore.js index cef9a2ecae..1f4a09c5c0 100644 --- a/src/createStore.js +++ b/src/createStore.js @@ -238,6 +238,7 @@ export default function createStore(reducer, preloadedState, enhancer) { // will receive the previous state. This effectively populates // the new state tree with any relevant data from the old one. dispatch({ type: ActionTypes.REPLACE }) + return store } /** @@ -284,11 +285,12 @@ export default function createStore(reducer, preloadedState, enhancer) { // the initial state tree. dispatch({ type: ActionTypes.INIT }) - return { + const store = { dispatch, subscribe, getState, replaceReducer, [$$observable]: observable } + return store } From 38df33bc092d188e4db87e00f5fc5e89fd9ab738 Mon Sep 17 00:00:00 2001 From: Gregory Beaver <greg.beaver.cello@gmail.com> Date: Fri, 16 Aug 2019 09:29:58 -0400 Subject: [PATCH 2/4] remove extraneous type --- index.d.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/index.d.ts b/index.d.ts index d31a15ffea..2fc2af4c9e 100644 --- a/index.d.ts +++ b/index.d.ts @@ -669,9 +669,3 @@ export function compose<R>( ): (...args: any[]) => R export function compose<R>(...funcs: Function[]): (...args: any[]) => R - -export const __DO_NOT_USE__ActionTypes: { - INIT: string - REPLACE: string - PROBE_UNKNOWN_ACTION: () => string -} From 8d3ed932b4fffcc1eec7be4a4907b991ce39a4aa Mon Sep 17 00:00:00 2001 From: Tim Dorr <timdorr@users.noreply.github.com> Date: Fri, 16 Aug 2019 11:27:44 -0400 Subject: [PATCH 3/4] Update the JSDoc so IDEs don't complain. --- src/createStore.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/createStore.js b/src/createStore.js index 1f4a09c5c0..57c57d7636 100644 --- a/src/createStore.js +++ b/src/createStore.js @@ -224,7 +224,7 @@ export default function createStore(reducer, preloadedState, enhancer) { * implement a hot reloading mechanism for Redux. * * @param {Function} nextReducer The reducer for the store to use instead. - * @returns {void} + * @returns {Store} The same store instance with a new reducer in place. */ function replaceReducer(nextReducer) { if (typeof nextReducer !== 'function') { From 500265fd43fe8affb7b1f9ee48a22a2a826a9b91 Mon Sep 17 00:00:00 2001 From: Gregory Beaver <greg.beaver.cello@gmail.com> Date: Fri, 16 Aug 2019 11:56:01 -0400 Subject: [PATCH 4/4] new test for replaceReducers --- test/replaceReducers.spec.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 test/replaceReducers.spec.ts diff --git a/test/replaceReducers.spec.ts b/test/replaceReducers.spec.ts new file mode 100644 index 0000000000..241171dbf8 --- /dev/null +++ b/test/replaceReducers.spec.ts @@ -0,0 +1,18 @@ +import { createStore, combineReducers } from '..' + +describe('replaceReducers test', () => { + it('returns the original store', () => { + const nextReducer = combineReducers({ + foo: (state = 1, _action) => state, + bar: (state = 2, _action) => state + }) + const store = createStore((state, action) => { + if (state === undefined) return 5 + return action + }) + + const nextStore = store.replaceReducer(nextReducer) + + expect(nextStore).toBe(store) + }) +})