mirror of
https://github.com/reduxjs/redux-devtools.git
synced 2024-11-22 09:36:43 +03:00
Remove React from page bundle (#1031)
* Remove React from page bundle * Import individual lodash functions
This commit is contained in:
parent
757abaf36d
commit
fdfbc1942e
|
@ -1,6 +1,6 @@
|
||||||
import { Action, compose, Reducer, StoreEnhancerStoreCreator } from 'redux';
|
import { Action, compose, Reducer, StoreEnhancerStoreCreator } from 'redux';
|
||||||
import { instrument } from '@redux-devtools/instrument';
|
import { instrument } from '@redux-devtools/instrument';
|
||||||
import { persistState } from '@redux-devtools/core';
|
import persistState from './persistState';
|
||||||
import { ConfigWithExpandedMaxAge } from '../../browser/extension/inject/pageScript';
|
import { ConfigWithExpandedMaxAge } from '../../browser/extension/inject/pageScript';
|
||||||
|
|
||||||
export function getUrlParam(key: string) {
|
export function getUrlParam(key: string) {
|
||||||
|
|
83
extension/src/app/stores/persistState.ts
Normal file
83
extension/src/app/stores/persistState.ts
Normal file
|
@ -0,0 +1,83 @@
|
||||||
|
import mapValues from 'lodash/mapValues';
|
||||||
|
import identity from 'lodash/identity';
|
||||||
|
import { Action, PreloadedState, Reducer, StoreEnhancer } from 'redux';
|
||||||
|
import { LiftedState } from '@redux-devtools/instrument';
|
||||||
|
|
||||||
|
export default function persistState<
|
||||||
|
S,
|
||||||
|
A extends Action<unknown>,
|
||||||
|
MonitorState
|
||||||
|
>(
|
||||||
|
sessionId?: string | null,
|
||||||
|
deserializeState: (state: S) => S = identity,
|
||||||
|
deserializeAction: (action: A) => A = identity
|
||||||
|
): StoreEnhancer {
|
||||||
|
if (!sessionId) {
|
||||||
|
return (next) =>
|
||||||
|
(...args) =>
|
||||||
|
next(...args);
|
||||||
|
}
|
||||||
|
|
||||||
|
function deserialize(
|
||||||
|
state: LiftedState<S, A, MonitorState>
|
||||||
|
): LiftedState<S, A, MonitorState> {
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
actionsById: mapValues(state.actionsById, (liftedAction) => ({
|
||||||
|
...liftedAction,
|
||||||
|
action: deserializeAction(liftedAction.action),
|
||||||
|
})),
|
||||||
|
committedState: deserializeState(state.committedState),
|
||||||
|
computedStates: state.computedStates.map((computedState) => ({
|
||||||
|
...computedState,
|
||||||
|
state: deserializeState(computedState.state),
|
||||||
|
})),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return (next) =>
|
||||||
|
<S2, A2 extends Action<unknown>>(
|
||||||
|
reducer: Reducer<S2, A2>,
|
||||||
|
initialState?: PreloadedState<S2>
|
||||||
|
) => {
|
||||||
|
const key = `redux-dev-session-${sessionId}`;
|
||||||
|
|
||||||
|
let finalInitialState;
|
||||||
|
try {
|
||||||
|
const json = localStorage.getItem(key);
|
||||||
|
if (json) {
|
||||||
|
finalInitialState =
|
||||||
|
deserialize(JSON.parse(json) as LiftedState<S, A, MonitorState>) ||
|
||||||
|
initialState;
|
||||||
|
next(reducer, initialState);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.warn('Could not read debug session from localStorage:', e); // eslint-disable-line no-console
|
||||||
|
try {
|
||||||
|
localStorage.removeItem(key);
|
||||||
|
} finally {
|
||||||
|
finalInitialState = undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const store = next(
|
||||||
|
reducer,
|
||||||
|
finalInitialState as PreloadedState<S2> | undefined
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
...store,
|
||||||
|
dispatch<T extends A2>(action: T) {
|
||||||
|
store.dispatch(action);
|
||||||
|
|
||||||
|
try {
|
||||||
|
localStorage.setItem(key, JSON.stringify(store.getState()));
|
||||||
|
} catch (e) {
|
||||||
|
console.warn('Could not write debug session to localStorage:', e); // eslint-disable-line no-console
|
||||||
|
}
|
||||||
|
|
||||||
|
return action;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user