mirror of
https://github.com/reduxjs/redux-devtools.git
synced 2025-07-19 12:42:26 +03:00
* Update Redux packages * Fix instrument build * Fix some test type errors * Fix redux-devtools build * Fix rtk-query-monitor build * Fix redux-devtools-app build * Fix redux-devtools-extension build * Fix redux-devtools-remote build * Fix extension build * slider-monitor-example * test-tab-demo * inspector-monitor-demo * rtk-query-monitor-demo * counter-example * todomvc-example * Fix lint * Fix instrument test types * Fix core tests * Fix rtk-query-monitor tests * Updates
67 lines
2.0 KiB
TypeScript
67 lines
2.0 KiB
TypeScript
import { stringifyJSON } from '../utils/stringifyJSON';
|
|
import { UPDATE_STATE, LIFTED_ACTION, EXPORT } from '../constants/actionTypes';
|
|
import { getActiveInstance } from '../reducers/instances';
|
|
import { Dispatch, Middleware } from 'redux';
|
|
import { CoreStoreAction } from '../actions';
|
|
import { CoreStoreState } from '../reducers';
|
|
|
|
let toExport: string | number | undefined;
|
|
|
|
function download(state: string) {
|
|
const blob = new Blob([state], { type: 'octet/stream' });
|
|
const href = window.URL.createObjectURL(blob);
|
|
const a = document.createElement('a');
|
|
a.style.display = 'none';
|
|
a.download = 'state.json';
|
|
a.href = href;
|
|
document.body.appendChild(a);
|
|
a.click();
|
|
setTimeout(() => {
|
|
document.body.removeChild(a);
|
|
window.URL.revokeObjectURL(href);
|
|
}, 0);
|
|
}
|
|
|
|
export const exportStateMiddleware: Middleware<
|
|
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
|
|
{},
|
|
CoreStoreState,
|
|
Dispatch<CoreStoreAction>
|
|
> = (store) => (next) => (untypedAction) => {
|
|
const result = next(untypedAction);
|
|
|
|
const action = untypedAction as CoreStoreAction;
|
|
if (
|
|
toExport &&
|
|
action.type === UPDATE_STATE &&
|
|
action.request!.type === 'EXPORT'
|
|
) {
|
|
const request = action.request!;
|
|
const id = request.instanceId || request.id;
|
|
if (id === toExport) {
|
|
toExport = undefined;
|
|
download(
|
|
JSON.stringify(
|
|
{
|
|
payload: request.payload,
|
|
preloadedState: request.committedState,
|
|
},
|
|
null,
|
|
'\t',
|
|
),
|
|
);
|
|
}
|
|
} else if (action.type === EXPORT) {
|
|
const instances = store.getState().instances;
|
|
const instanceId = getActiveInstance(instances);
|
|
const options = instances.options[instanceId];
|
|
if (options.features.export === true) {
|
|
download(stringifyJSON(instances.states[instanceId], options.serialize));
|
|
} else {
|
|
toExport = instanceId;
|
|
next({ type: LIFTED_ACTION, message: 'EXPORT', toExport: true });
|
|
}
|
|
}
|
|
return result;
|
|
};
|