Skip to content

Commit 0bbabf7

Browse files
authored
Merge pull request #71 from mofojed/redux-registry
Implement react redux registry
2 parents 35f1590 + 4362b8f commit 0bbabf7

File tree

6 files changed

+59
-21
lines changed

6 files changed

+59
-21
lines changed

packages/redux/package-lock.json

Lines changed: 6 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/redux/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
},
3636
"dependencies": {
3737
"deep-equal": "^2.0.4",
38-
"reduce-reducers": "^1.0.1",
3938
"redux-thunk": "^2.3.0"
4039
},
4140
"devDependencies": {
@@ -50,7 +49,8 @@
5049
"npm-run-all": "^4.1.5",
5150
"redux": "^4.0.5",
5251
"rimraf": "^3.0.2",
53-
"ts-jest": "^26.5.6"
52+
"ts-jest": "^26.5.6",
53+
"typescript": "^4.3.2"
5454
},
5555
"peerDependencies": {
5656
"@deephaven/log": "^0.1.0",

packages/redux/src/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
import reducers from './reducers';
2+
import reducerRegistry from './reducerRegistry';
3+
4+
// TODO #70: Separate all reducers into their respective modules, register from there
5+
Object.entries(reducers).map(([name, reducer]) =>
6+
reducerRegistry.register(name, reducer)
7+
);
8+
19
export {
210
getActiveTool,
311
getClosedPanelsForDashboard,
@@ -63,4 +71,6 @@ export {
6371
setWorkspaceStorage,
6472
updateWorkspaceData,
6573
} from './actions';
74+
export { default as reducers } from './reducers';
75+
export { default as reducerRegistry } from './reducerRegistry';
6676
export { default as store } from './store';
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Reducer, ReducersMapObject } from 'redux';
2+
3+
export type ReducerRegistryListener = (reducers: ReducersMapObject) => void;
4+
5+
/**
6+
* A registry for registering reducers. Whenever a new reducer is added,
7+
* the reducers should be replaced in the store.
8+
*
9+
* See this blog post for some notes on redux modules/code splitting
10+
* http://nicolasgallagher.com/redux-modules-and-code-splitting/
11+
*/
12+
export class ReducerRegistry {
13+
reducers = {} as ReducersMapObject;
14+
15+
listener: ReducerRegistryListener | null = null;
16+
17+
register(name: string, reducer: Reducer): void {
18+
this.reducers = { ...this.reducers, [name]: reducer };
19+
this.listener?.(this.reducers);
20+
}
21+
22+
setListener(listener: ReducerRegistryListener): void {
23+
this.listener = listener;
24+
}
25+
}
26+
27+
const reducerRegistry = new ReducerRegistry();
28+
export default reducerRegistry;

packages/redux/src/reducers/index.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import { combineReducers } from 'redux';
2-
import reduceReducer from 'reduce-reducers';
3-
41
import activeTool from './activeTool';
52
import dashboardClosedPanels from './dashboardClosedPanels';
63
import dashboardOpenedPanelMaps from './dashboardOpenedPanelMaps';
@@ -19,8 +16,7 @@ import controllerConfiguration from './controllerConfiguration';
1916
import draftManager from './draftManager';
2017
import serverConfigValues from './serverConfigValues';
2118

22-
// Reducers that work on one part of the state
23-
const childReducers = combineReducers({
19+
const reducers = {
2420
activeTool,
2521
dashboardClosedPanels,
2622
dashboardOpenedPanelMaps,
@@ -38,9 +34,6 @@ const childReducers = combineReducers({
3834
controllerConfiguration,
3935
draftManager,
4036
serverConfigValues,
41-
});
42-
43-
// Reducers that work on multiple parts of the state (get the whole state)
44-
const stateReducers = [];
37+
};
4538

46-
export default reduceReducer(childReducers, ...stateReducers);
39+
export default reducers;

packages/redux/src/store.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
import { applyMiddleware, createStore, compose } from 'redux';
1+
import { applyMiddleware, createStore, compose, combineReducers } from 'redux';
22
import rootMiddleware from './middleware';
3-
import rootReducer from './reducers';
3+
import reducerRegistry from './reducerRegistry';
44

55
/* eslint-disable-next-line no-underscore-dangle */
66
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
77

8-
export default createStore(
9-
rootReducer,
8+
const store = createStore(
9+
combineReducers(reducerRegistry.reducers),
1010
composeEnhancers(applyMiddleware(...rootMiddleware))
1111
);
12+
13+
reducerRegistry.setListener(reducers => {
14+
store.replaceReducer(combineReducers(reducers));
15+
});
16+
17+
export default store;

0 commit comments

Comments
 (0)