plugins.ts
This file
Path
src/plugins.ts
140 lines · 3.1 KiB · TypeScript
Last changed by
New build set. Current statsMichel Weststratebefore adoption9aec0fe
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141import { ImmerState, Patch, ImmerScope, Drafted, AnyObject, ImmerBaseState, AnyArray, AnyMap, AnySet, ProxyType} from "./internal"
/** Plugin utilities */const plugins: { patches?: { generatePatches: typeof generatePatches applyPatches: typeof applyPatches } es5?: { willFinalizeES5: typeof willFinalizeES5 createES5Proxy: typeof createES5Proxy markChangedES5: typeof markChangedES5 } mapset?: { proxyMap: typeof proxyMap proxySet: typeof proxySet }} = {}
type Plugins = typeof plugins
export function getPlugin<K extends keyof Plugins>( pluginKey: K): Exclude<Plugins[K], undefined> { 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<K extends keyof Plugins>( 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: ProxyType.ES5Object draft: Drafted<AnyObject, ES5ObjectState> base: AnyObject copy: AnyObject | null}
export interface ES5ArrayState extends ES5BaseState { type: ProxyType.ES5Array draft: Drafted<AnyObject, ES5ArrayState> base: AnyArray copy: AnyArray | null}
export function willFinalizeES5( scope: ImmerScope, result: any, isReplaced: boolean) { getPlugin("es5").willFinalizeES5(scope, result, isReplaced)}
export function createES5Proxy<T>( base: T, parent?: ImmerState): Drafted<T, ES5ObjectState | ES5ArrayState> { return getPlugin("es5").createES5Proxy(base, parent)}
export function markChangedES5(state: ImmerState) { getPlugin("es5").markChangedES5(state)}
/** Map / Set plugin */
export interface MapState extends ImmerBaseState { type: ProxyType.Map copy: AnyMap | undefined assigned: Map<any, boolean> | undefined base: AnyMap revoked: boolean draft: Drafted<AnyMap, MapState>}
export interface SetState extends ImmerBaseState { type: ProxyType.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>}
export function proxyMap<T extends AnyMap>(target: T, parent?: ImmerState): T { return getPlugin("mapset").proxyMap(target, parent)}
export function proxySet<T extends AnySet>(target: T, parent?: ImmerState): T { return getPlugin("mapset").proxySet(target, parent)}
/** Patches plugin */
export type PatchPath = (string | number)[]
export function generatePatches( state: ImmerState, basePath: PatchPath, patches: Patch[], inversePatches: Patch[]): void { getPlugin("patches").generatePatches(state, basePath, patches, inversePatches)}
export function applyPatches<T>(draft: T, patches: Patch[]): T { return getPlugin("patches").applyPatches(draft, patches)}