import {
type Options as MutativeOptions,
type Patches,
type Draft,
apply,
create,
isDraft,
rawReturn,
} from 'mutative';
import type {
PatchesOption,
RebasableManualTravelsControls,
RebasableTravelsControls,
TravelHistoryEntry,
TravelMetadata,
TravelPatches,
TravelsBranchDiscardEvent,
TravelsDeserializeOptions,
TravelsDevtoolsEvent,
TravelsOptions,
TravelsSerializedHistory,
Updater,
Value,
} from './type';
import {
deserializeTravelsHistory,
TRAVELS_HISTORY_SCHEMA_VERSION,
} from './persistence';
import { findStateCompatibilityIssues } from './compatibility';
import { TravelsError } from './errors';
import { isObjectLike, isPlainObject } from './utils';
/**
* Listener callback for state changes
*/
type Listener = (
state: S,
patches: TravelPatches
,
position: number
) => void;
type TransactionSnapshot = {
state: S;
position: number;
allPatches: TravelPatches
;
allMetadata: Array;
tempPatches: TravelPatches;
tempMetadata?: TravelMetadata;
initialState: S;
initialPosition: number;
initialPatches?: TravelPatches
;
initialMetadata?: Array;
pendingState: S | null;
pendingStateVersion: number;
};
const tryStructuredClone = (value: T): T | undefined => {
if (typeof (globalThis as any).structuredClone !== 'function') {
return undefined;
}
try {
return (globalThis as any).structuredClone(value) as T;
} catch {
return undefined;
}
};
const deepCloneValue = (value: any, seen = new WeakMap