2020-10-26 02:32:04 +03:00
|
|
|
import jsan from 'jsan';
|
2020-12-21 23:46:28 +03:00
|
|
|
import { immutableSerialize } from '@redux-devtools/serialize';
|
2020-10-26 02:32:04 +03:00
|
|
|
import { Action } from 'redux';
|
|
|
|
import Immutable from 'immutable';
|
2021-09-20 00:59:01 +03:00
|
|
|
import { PerformAction } from '@redux-devtools/core';
|
2020-12-22 20:02:14 +03:00
|
|
|
|
|
|
|
interface State {
|
|
|
|
actionsById: { [actionId: number]: PerformAction<Action<unknown>> };
|
|
|
|
computedStates: { state: unknown; error?: string }[];
|
|
|
|
committedState?: unknown;
|
|
|
|
}
|
2020-10-26 02:32:04 +03:00
|
|
|
|
2022-01-10 18:41:53 +03:00
|
|
|
export function importState(
|
2020-10-26 02:32:04 +03:00
|
|
|
state: string,
|
|
|
|
{
|
|
|
|
serialize,
|
|
|
|
}: {
|
|
|
|
serialize?: {
|
|
|
|
immutable?: typeof Immutable;
|
|
|
|
refs?: (new (data: any) => unknown)[] | null;
|
|
|
|
reviver?: (key: string, value: unknown) => unknown;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
) {
|
|
|
|
if (!state) return undefined;
|
|
|
|
let parse = jsan.parse;
|
|
|
|
if (serialize) {
|
|
|
|
if (serialize.immutable) {
|
|
|
|
parse = (v) =>
|
|
|
|
jsan.parse(
|
|
|
|
v,
|
|
|
|
immutableSerialize(serialize.immutable!, serialize.refs).reviver
|
|
|
|
);
|
|
|
|
} else if (serialize.reviver) {
|
|
|
|
parse = (v) => jsan.parse(v, serialize.reviver);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let preloadedState: State | undefined;
|
|
|
|
let nextLiftedState: State = parse(state) as State;
|
|
|
|
if (
|
2021-06-18 06:56:36 +03:00
|
|
|
(
|
|
|
|
nextLiftedState as unknown as {
|
|
|
|
payload?: string;
|
|
|
|
preloadedState?: string;
|
|
|
|
}
|
|
|
|
).payload
|
2020-10-26 02:32:04 +03:00
|
|
|
) {
|
|
|
|
if (
|
2021-06-18 06:56:36 +03:00
|
|
|
(
|
|
|
|
nextLiftedState as unknown as {
|
|
|
|
payload: string;
|
|
|
|
preloadedState?: string;
|
|
|
|
}
|
|
|
|
).preloadedState
|
2020-10-26 02:32:04 +03:00
|
|
|
)
|
|
|
|
preloadedState = parse(
|
2021-06-18 06:56:36 +03:00
|
|
|
(
|
|
|
|
nextLiftedState as unknown as {
|
|
|
|
payload: string;
|
|
|
|
preloadedState: string;
|
|
|
|
}
|
|
|
|
).preloadedState
|
2020-10-26 02:32:04 +03:00
|
|
|
) as State;
|
|
|
|
nextLiftedState = parse(
|
2021-06-18 06:56:36 +03:00
|
|
|
(
|
|
|
|
nextLiftedState as unknown as {
|
|
|
|
payload: string;
|
|
|
|
}
|
|
|
|
).payload
|
2020-10-26 02:32:04 +03:00
|
|
|
) as State;
|
|
|
|
}
|
|
|
|
|
|
|
|
return { nextLiftedState, preloadedState };
|
|
|
|
}
|