plugins.ts
This file
Path
src/utils/plugins.ts
76 lines · 1.6 KiB · TypeScript
Last changed by
Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
History of this fileTake it
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677import { ImmerState, Patch, Drafted, ImmerBaseState, AnyMap, AnySet, ArchType, die} from "../internal"
/** Plugin utilities */const plugins: { Patches?: { generatePatches_( state: ImmerState, basePath: PatchPath, patches: Patch[], inversePatches: Patch[] ): void generateReplacementPatches_( base: any, replacement: any, patches: Patch[], inversePatches: Patch[] ): void applyPatches_<T>(draft: T, patches: Patch[]): T } MapSet?: { proxyMap_<T extends AnyMap>(target: T, parent?: ImmerState): T proxySet_<T extends AnySet>(target: T, parent?: ImmerState): T }} = {}
type Plugins = typeof plugins
export function getPlugin<K extends keyof Plugins>( pluginKey: K): Exclude<Plugins[K], undefined> { const plugin = plugins[pluginKey] if (!plugin) { die(0, pluginKey) } // @ts-ignore return plugin}
export function loadPlugin<K extends keyof Plugins>( pluginKey: K, implementation: Plugins[K]): void { if (!plugins[pluginKey]) plugins[pluginKey] = implementation}/** Map / Set plugin */
export interface MapState extends ImmerBaseState { type_: ArchType.Map copy_: AnyMap | undefined assigned_: Map<any, boolean> | undefined base_: AnyMap revoked_: boolean draft_: Drafted<AnyMap, MapState>}
export interface SetState extends ImmerBaseState { type_: ArchType.Set copy_: AnySet | undefined base_: AnySet drafts_: Map<any, Drafted> // maps the original value to the draft value in the new set revoked_: boolean draft_: Drafted<AnySet, SetState>}
/** Patches plugin */
export type PatchPath = (string | number)[]