Skip to content

Commit 557a40b

Browse files
committed
Add debug session persistance
1 parent 72aa9a3 commit 557a40b

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

examples/counter/containers/App.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import { Provider } from 'redux/react';
55
import * as reducers from '../reducers';
66

77
import devTools from '../redux-devtools/index';
8+
import persistState from '../redux-devtools/persistState';
89
import DebugPanel from '../redux-devtools/DebugPanel';
910
import ReduxMonitor from '../redux-devtools/ReduxMonitor';
1011

1112
const finalCreateStore = compose(
1213
applyMiddleware(),
1314
devTools(),
15+
persistState(window.location.href.match(/[?&]debug_session=([^&]+)\b/)),
1416
createStore
1517
);
1618

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
export default function persistState(sessionId) {
2+
if (!sessionId) {
3+
return next => (...args) => next(...args);
4+
}
5+
6+
return next => (reducer, initialState) => {
7+
const key = `redux-dev-session-${sessionId}`;
8+
9+
try {
10+
initialState = JSON.parse(localStorage.getItem(key)) || initialState;
11+
next(reducer, initialState);
12+
} catch (e) {
13+
try {
14+
localStorage.removeItem(key);
15+
} finally {
16+
initialState = undefined;
17+
}
18+
}
19+
20+
const store = next(reducer, initialState);
21+
22+
return Object.assign(Object.create(store), {
23+
dispatch(action) {
24+
store.dispatch(action);
25+
localStorage.setItem(key, JSON.stringify(store.getState()));
26+
return action;
27+
}
28+
});
29+
};
30+
}

0 commit comments

Comments
 (0)