import { ImmerState, Patch, ImmerScope, Drafted, AnyObject, ImmerBaseState, AnyArray, AnyMap, AnySet, ProxyTypeES5Array, ProxyTypeES5Object, ProxyTypeMap, ProxyTypeSet } from "./internal" /** Plugin utilities */ const plugins: { patches?: { generatePatches_( state: ImmerState, basePath: PatchPath, patches: Patch[], inversePatches: Patch[] ): void applyPatches_(draft: T, patches: Patch[]): T } es5?: { willFinalizeES5_(scope: ImmerScope, result: any, isReplaced: boolean): void createES5Proxy_( base: T, parent?: ImmerState ): Drafted markChangedES5_(state: ImmerState): void } mapset?: { proxyMap_(target: T, parent?: ImmerState): T proxySet_(target: T, parent?: ImmerState): T } } = {} type Plugins = typeof plugins export function getPlugin( pluginKey: K ): Exclude { const plugin = plugins[pluginKey] if (!plugin) { throw new Error( `The plugin ${pluginKey} has not been loaded into Immer. Make sure to call "enable${pluginKey[0].toUpperCase()}${pluginKey.substr( 1 )}()" when initializing your application, just after requiring immer itself.` ) } // @ts-ignore return plugin } export function loadPlugin( pluginKey: K, implementation: Plugins[K] ): void { plugins[pluginKey] = implementation } /** ES5 Plugin */ interface ES5BaseState extends ImmerBaseState { finalizing_: boolean assigned_: {[key: string]: any} parent_?: ImmerState revoked_: boolean } export interface ES5ObjectState extends ES5BaseState { type_: typeof ProxyTypeES5Object draft_: Drafted base_: AnyObject copy_: AnyObject | null } export interface ES5ArrayState extends ES5BaseState { type_: typeof ProxyTypeES5Array draft_: Drafted base_: AnyArray copy_: AnyArray | null } /** Map / Set plugin */ export interface MapState extends ImmerBaseState { type_: typeof ProxyTypeMap copy_: AnyMap | undefined assigned_: Map | undefined base_: AnyMap revoked_: boolean draft_: Drafted } export interface SetState extends ImmerBaseState { type_: typeof ProxyTypeSet copy_: AnySet | undefined base_: AnySet drafts_: Map // maps the original value to the draft value in the new set revoked_: boolean draft_: Drafted } /** Patches plugin */ export type PatchPath = (string | number)[]