Skip to content

Commit 9043a1d

Browse files
Clean up store initialization (#1409)
1 parent 06a4178 commit 9043a1d

File tree

2 files changed

+71
-62
lines changed

2 files changed

+71
-62
lines changed

dash-renderer/src/AppProvider.react.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import PropTypes from 'prop-types';
2-
import React from 'react';
2+
import React, {useState} from 'react';
33
import {Provider} from 'react-redux';
44

5-
import initializeStore from './store';
5+
import Store from './store';
66
import AppContainer from './AppContainer.react';
77

8-
const store = initializeStore();
9-
108
const AppProvider = ({hooks}: any) => {
9+
const [{store}] = useState(() => new Store());
10+
1111
return (
1212
<Provider store={store}>
1313
<AppContainer hooks={hooks} />

dash-renderer/src/store.ts

Lines changed: 67 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -28,71 +28,80 @@ export interface IStoreState {
2828
[key: string]: any;
2929
}
3030

31-
let store: Store<IStoreState>;
32-
const storeObserver = new StoreObserver<IStoreState>();
33-
34-
const setObservers = once(() => {
35-
const observe = storeObserver.observe;
36-
37-
observe(documentTitle);
38-
observe(isLoading);
39-
observe(loadingMap);
40-
observe(requestedCallbacks);
41-
observe(prioritizedCallbacks);
42-
observe(executingCallbacks);
43-
observe(executedCallbacks);
44-
observe(storedCallbacks);
45-
});
46-
47-
function createAppStore(reducer: any, middleware: any) {
48-
store = createStore(reducer, middleware);
49-
storeObserver.setStore(store);
50-
setObservers();
51-
}
31+
export default class RendererStore {
32+
constructor() {
33+
this.__store = this.initializeStore();
34+
}
5235

53-
/**
54-
* Initialize a Redux store with thunk, plus logging (only in development mode) middleware
55-
*
56-
* @param {bool} reset: discard any previous store
57-
*
58-
* @returns {Store<GenericStoreEnhancer>}
59-
* An initialized redux store with middleware and possible hot reloading of reducers
60-
*/
61-
const initializeStore = (reset?: boolean): Store<IStoreState> => {
62-
if (store && !reset) {
63-
return store;
36+
private __store: Store<IStoreState>;
37+
public get store(): Store<IStoreState> {
38+
return this.__store;
6439
}
6540

66-
const reducer = createReducer();
41+
private readonly storeObserver = new StoreObserver<IStoreState>();
6742

68-
// eslint-disable-next-line no-process-env
69-
if (process.env.NODE_ENV === 'production') {
70-
createAppStore(reducer, applyMiddleware(thunk));
71-
} else {
72-
// only attach logger to middleware in non-production mode
73-
const reduxDTEC = (window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__;
74-
if (reduxDTEC) {
75-
createAppStore(reducer, reduxDTEC(applyMiddleware(thunk)));
76-
} else {
77-
createAppStore(reducer, applyMiddleware(thunk));
43+
private setObservers = once(() => {
44+
const observe = this.storeObserver.observe;
45+
46+
observe(documentTitle);
47+
observe(isLoading);
48+
observe(loadingMap);
49+
observe(requestedCallbacks);
50+
observe(prioritizedCallbacks);
51+
observe(executingCallbacks);
52+
observe(executedCallbacks);
53+
observe(storedCallbacks);
54+
});
55+
56+
private createAppStore = (reducer: any, middleware: any) => {
57+
this.__store = createStore(reducer, middleware);
58+
this.storeObserver.setStore(this.__store);
59+
this.setObservers();
60+
};
61+
62+
/**
63+
* Initialize a Redux store with thunk, plus logging (only in development mode) middleware
64+
*
65+
* @param {bool} reset: discard any previous store
66+
*
67+
* @returns {Store<GenericStoreEnhancer>}
68+
* An initialized redux store with middleware and possible hot reloading of reducers
69+
*/
70+
initializeStore = (reset?: boolean): Store<IStoreState> => {
71+
if (this.__store && !reset) {
72+
return this.__store;
7873
}
79-
}
8074

81-
if (!reset) {
82-
// TODO - Protect this under a debug mode?
83-
(window as any).store = store;
84-
}
75+
const reducer = createReducer();
8576

86-
if ((module as any).hot) {
87-
// Enable hot module replacement for reducers
88-
(module as any).hot.accept('./reducers/reducer', () => {
89-
const nextRootReducer = require('./reducers/reducer').createReducer();
77+
// eslint-disable-next-line no-process-env
78+
if (process.env.NODE_ENV === 'production') {
79+
this.createAppStore(reducer, applyMiddleware(thunk));
80+
} else {
81+
// only attach logger to middleware in non-production mode
82+
const reduxDTEC = (window as any)
83+
.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__;
84+
if (reduxDTEC) {
85+
this.createAppStore(reducer, reduxDTEC(applyMiddleware(thunk)));
86+
} else {
87+
this.createAppStore(reducer, applyMiddleware(thunk));
88+
}
89+
}
9090

91-
store.replaceReducer(nextRootReducer);
92-
});
93-
}
91+
if (!reset) {
92+
// TODO - Protect this under a debug mode?
93+
(window as any).store = this.__store;
94+
}
9495

95-
return store;
96-
};
96+
if ((module as any).hot) {
97+
// Enable hot module replacement for reducers
98+
(module as any).hot.accept('./reducers/reducer', () => {
99+
const nextRootReducer = require('./reducers/reducer').createReducer();
97100

98-
export default initializeStore;
101+
this.__store.replaceReducer(nextRootReducer);
102+
});
103+
}
104+
105+
return this.__store;
106+
};
107+
}

0 commit comments

Comments
 (0)