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 {
|
2023-11-05 00:04:23 +03:00
|
|
|
actionsById: { [actionId: number]: PerformAction<Action<string>> };
|
2020-12-22 20:02:14 +03:00
|
|
|
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;
|
|
|
|
};
|
2023-07-12 21:03:20 +03:00
|
|
|
},
|
2020-10-26 02:32:04 +03:00
|
|
|
) {
|
|
|
|
if (!state) return undefined;
|
|
|
|
let parse = jsan.parse;
|
|
|
|
if (serialize) {
|
|
|
|
if (serialize.immutable) {
|
|
|
|
parse = (v) =>
|
|
|
|
jsan.parse(
|
|
|
|
v,
|
2023-07-12 21:03:20 +03:00
|
|
|
immutableSerialize(serialize.immutable!, serialize.refs).reviver,
|
2020-10-26 02:32:04 +03:00
|
|
|
);
|
|
|
|
} 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;
|
|
|
|
}
|
2023-07-12 21:03:20 +03:00
|
|
|
).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;
|
|
|
|
}
|
2023-07-12 21:03:20 +03:00
|
|
|
).payload,
|
2020-10-26 02:32:04 +03:00
|
|
|
) as State;
|
|
|
|
}
|
|
|
|
|
|
|
|
return { nextLiftedState, preloadedState };
|
|
|
|
}
|