redux-devtools/src/persistState.js

76 lines
1.9 KiB
JavaScript
Raw Normal View History

export default function persistState(sessionId, stateDeserializer = null, actionDeserializer = null) {
if (!sessionId) {
return next => (...args) => next(...args);
}
function deserializeState(fullState) {
2015-08-19 22:58:25 +03:00
return {
...fullState,
committedState: stateDeserializer(fullState.committedState),
2015-08-19 22:58:25 +03:00
computedStates: fullState.computedStates.map((computedState) => {
return {
...computedState,
state: stateDeserializer(computedState.state)
2015-08-19 22:58:25 +03:00
};
})
};
}
function deserializeActions(fullState) {
return {
...fullState,
stagedActions: fullState.stagedActions.map((action) => {
return actionDeserializer(action);
})
};
}
function deserialize(fullState) {
if (!fullState) {
return fullState;
}
let deserializedState = fullState;
if (typeof stateDeserializer === 'function') {
deserializedState = deserializeState(deserializedState);
}
if (typeof actionDeserializer === 'function') {
deserializedState = deserializeActions(deserializedState);
}
return deserializedState;
}
return next => (reducer, initialState) => {
const key = `redux-dev-session-${sessionId}`;
let finalInitialState;
try {
finalInitialState = deserialize(JSON.parse(localStorage.getItem(key))) || initialState;
next(reducer, initialState);
} catch (e) {
console.warn('Could not read debug session from localStorage:', e);
try {
localStorage.removeItem(key);
} finally {
finalInitialState = undefined;
}
}
const store = next(reducer, finalInitialState);
return {
...store,
dispatch(action) {
store.dispatch(action);
try {
localStorage.setItem(key, JSON.stringify(store.getState()));
} catch (e) {
2015-09-24 21:20:02 +03:00
console.warn('Could not write debug session to localStorage:', e);
}
return action;
}
};
};
}