Skip to content

Commit 5e4a75d

Browse files
committed
Optimization: use extension enhancer
1 parent c5fccd6 commit 5e4a75d

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

src/app/store/configureStore.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@ import { compose } from 'redux';
22
import instrument from 'redux-devtools/lib/instrument';
33
import persistState from 'redux-devtools/lib/persistState';
44

5-
export default function configureStore(next, subscriber = () => ({}), options = {}) {
5+
function getPersistSession() {
6+
const matches = window.location.href.match(/[?&]debug_session=([^&#]+)\b/);
7+
return (matches && matches.length > 0) ? matches[1] : null;
8+
}
9+
10+
export default function configureStore(extEnhancer, subscriber = () => ({}), options = {}) {
611
const { deserializeState, deserializeAction } = options;
712
return compose(
13+
extEnhancer,
814
instrument(subscriber, window.devToolsOptions),
915
persistState(
1016
getPersistSession(),
1117
deserializeState,
1218
deserializeAction
1319
)
14-
)(next);
15-
}
16-
function getPersistSession() {
17-
const matches = window.location.href.match(/[?&]debug_session=([^&#]+)\b/);
18-
return (matches && matches.length > 0)? matches[1] : null;
20+
);
1921
}

src/browser/extension/inject/pageScript.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const monitorActions = [
1010
];
1111

1212
window.devToolsExtension = function(config = {}) {
13-
let store = {};
13+
let liftedStore;
1414
if (!window.devToolsOptions) window.devToolsOptions = {};
1515

1616
let localFilter;
@@ -78,12 +78,12 @@ window.devToolsExtension = function(config = {}) {
7878
}
7979

8080
if (message.type === 'DISPATCH') {
81-
store.liftedStore.dispatch(message.payload);
81+
liftedStore.dispatch(message.payload);
8282
} else if (message.type === 'UPDATE') {
83-
relay('STATE', store.liftedStore.getState());
83+
relay('STATE', liftedStore.getState());
8484
} else if (message.type === 'START') {
8585
isMonitored = true;
86-
relay('STATE', store.liftedStore.getState());
86+
relay('STATE', liftedStore.getState());
8787
} else if (message.type === 'STOP') {
8888
isMonitored = false;
8989
}
@@ -119,7 +119,7 @@ window.devToolsExtension = function(config = {}) {
119119
relay('INIT_INSTANCE');
120120
notifyErrors(() => {
121121
errorOccurred = true;
122-
const state = store.liftedStore.getState();
122+
const state = liftedStore.getState();
123123
if (state.computedStates[state.currentStateIndex].error) {
124124
relay('STATE', state);
125125
return false;
@@ -130,18 +130,18 @@ window.devToolsExtension = function(config = {}) {
130130
// Detect when the tab is reactivated
131131
document.addEventListener('visibilitychange', function() {
132132
if (document.visibilityState === 'visible' && isMonitored) {
133-
relay('STATE', store.liftedStore.getState());
133+
relay('STATE', liftedStore.getState());
134134
}
135135
}, false);
136136
}
137137

138138
function monitorReducer(state = {}, action) {
139139
if (!isMonitored) return state;
140140
lastAction = action.type;
141-
if (lastAction === '@@redux/INIT' && store.liftedStore) {
141+
if (lastAction === '@@redux/INIT' && liftedStore) {
142142
// Send new lifted state on hot-reloading
143143
setTimeout(() => {
144-
relay('STATE', store.liftedStore.getState());
144+
relay('STATE', liftedStore.getState());
145145
}, 0);
146146
}
147147
return logMonitorReducer({}, state, action);
@@ -165,20 +165,24 @@ window.devToolsExtension = function(config = {}) {
165165
}
166166
}
167167

168-
return (next) => {
168+
function extEnhancer(next) {
169169
return (reducer, initialState, enhancer) => {
170-
if (!isAllowed(window.devToolsOptions)) return next(reducer, initialState, enhancer);
171-
172-
const { deserializeState, deserializeAction } = config;
173-
store = configureStore(next, monitorReducer, {
174-
deserializeState,
175-
deserializeAction
176-
})(reducer, initialState, enhancer);
177170
init();
178-
store.subscribe(() => { handleChange(store.getState(), store.liftedStore.getState()); });
171+
const store = next(reducer, initialState, enhancer);
172+
liftedStore = store.liftedStore;
173+
store.subscribe(() => {
174+
handleChange(store.getState(), liftedStore.getState());
175+
});
179176
return store;
180177
};
181-
};
178+
}
179+
180+
if (!isAllowed(window.devToolsOptions)) return f => f;
181+
const { deserializeState, deserializeAction } = config;
182+
return configureStore(extEnhancer, monitorReducer, {
183+
deserializeState,
184+
deserializeAction
185+
});
182186
};
183187

184188
window.devToolsExtension.open = function(position) {

0 commit comments

Comments
 (0)