Skip to content

Commit ddb8d28

Browse files
committed
Fix support for multiple instances
Fix #144.
1 parent 2f7692f commit ddb8d28

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

docs/API/Methods.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Show notifications for uncaught exceptions.
2424

2525
- [`onError`] *Function* to call when there's an exceptions.
2626

27-
### window.devToolsExtension.updateStore(store)
27+
### window.devToolsExtension.updateStore(store, instanceId)
2828

2929
Specify a new `store` object to be used by the extension. For example, in case of Redux Saga we can use like this:
3030

@@ -41,6 +41,11 @@ sagaMiddleware.run(rootSaga);
4141
if (window.devToolsExtension) window.devToolsExtension.updateStore(store);
4242
```
4343

44+
##### Arguments
45+
46+
- `store` *Object* to update.
47+
- [`instanceId`] *String* - instance id for which to update the store (in case your specified it in the config).
48+
4449
### window.devToolsExtension.send(action, state, [shouldStringify, instanceId])
4550

4651
Send a new action and state manually to be shown on the monitor.

src/browser/extension/inject/pageScript.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import notifyErrors from '../utils/notifyErrors';
66
import importState from '../utils/importState';
77
import openWindow from '../utils/openWindow';
88
import {
9-
toContentScript, sendMessage, setListener, connect, disconnect, generateId
9+
updateStore, toContentScript, sendMessage, setListener, connect, disconnect, generateId
1010
} from '../utils/contentScriptMsg';
1111

12-
let store;
12+
let stores = {};
1313

1414
window.devToolsExtension = function(reducer, preloadedState, config) {
1515
/* eslint-disable no-param-reassign */
@@ -19,6 +19,7 @@ window.devToolsExtension = function(reducer, preloadedState, config) {
1919
/* eslint-enable no-param-reassign */
2020
if (!window.devToolsOptions) window.devToolsOptions = {};
2121

22+
let store;
2223
let shouldSerialize = config.serializeState || config.serializeAction;
2324
let lastAction;
2425
let errorOccurred = false;
@@ -139,7 +140,8 @@ window.devToolsExtension = function(reducer, preloadedState, config) {
139140
return (reducer_, initialState_, enhancer_) => {
140141
if (!isAllowed(window.devToolsOptions)) return next(reducer_, initialState_, enhancer_);
141142

142-
store = configureStore(next, monitorReducer, config)(reducer_, initialState_, enhancer_);
143+
store = stores[instanceId] =
144+
configureStore(next, monitorReducer, config)(reducer_, initialState_, enhancer_);
143145

144146
init();
145147
store.subscribe(() => {
@@ -154,10 +156,9 @@ window.devToolsExtension = function(reducer, preloadedState, config) {
154156
};
155157

156158
window.devToolsExtension.open = openWindow;
159+
window.devToolsExtension.updateStore = updateStore(stores);
157160
window.devToolsExtension.notifyErrors = notifyErrors;
158161
window.devToolsExtension.send = sendMessage;
159162
window.devToolsExtension.listen = setListener;
160163
window.devToolsExtension.connect = connect;
161164
window.devToolsExtension.disconnect = disconnect;
162-
163-
window.devToolsExtension.updateStore = (newStore) => { store = newStore; };

src/browser/extension/utils/contentScriptMsg.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,13 @@ export function connect(config = {}) {
9191
send
9292
};
9393
}
94+
95+
export function updateStore(stores) {
96+
return function(newStore, instanceId) {
97+
const store = stores[instanceId || Object.keys(stores)[0]];
98+
// Mutate the store in order to keep the reference
99+
store.liftedStore = newStore.liftedStore;
100+
store.getState = newStore.getState;
101+
store.dispatch = newStore.dispatch;
102+
};
103+
}

0 commit comments

Comments
 (0)