plugins.ts
This file
Path
src/utils/plugins.ts
114 lines · 2.4 KiB · TypeScript
Last changed by
Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
History of this fileTake it
Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 1–114why, one step back1import {2 ImmerState,3 Patch,4 ImmerScope,5 Drafted,6 AnyObject,7 ImmerBaseState,8 AnyArray,9 AnyMap,10 AnySet,11 ProxyTypeES5Array,12 ProxyTypeES5Object,13 ProxyTypeMap,14 ProxyTypeSet,15 die16} from "../internal"1718/** Plugin utilities */19const plugins: {20 Patches?: {21 generatePatches_(22 state: ImmerState,23 basePath: PatchPath,24 patches: Patch[],25 inversePatches: Patch[]26 ): void27 generateReplacementPatches_(28 rootState: ImmerState,29 replacement: any,30 patches: Patch[],31 inversePatches: Patch[]32 ): void33 applyPatches_<T>(draft: T, patches: Patch[]): T34 }35 ES5?: {36 willFinalizeES5_(scope: ImmerScope, result: any, isReplaced: boolean): void37 createES5Proxy_<T>(38 base: T,39 parent?: ImmerState40 ): Drafted<T, ES5ObjectState | ES5ArrayState>41 markChangedES5_(state: ImmerState): void42 }43 MapSet?: {44 proxyMap_<T extends AnyMap>(target: T, parent?: ImmerState): T45 proxySet_<T extends AnySet>(target: T, parent?: ImmerState): T46 }47} = {}4849type Plugins = typeof plugins5051export function getPlugin<K extends keyof Plugins>(52 pluginKey: K53): Exclude<Plugins[K], undefined> {54 const plugin = plugins[pluginKey]55 if (!plugin) {56 die(__DEV__ ? 18 : 19, pluginKey)57 }58 // @ts-ignore59 return plugin60}6162export function loadPlugin<K extends keyof Plugins>(63 pluginKey: K,64 implementation: Plugins[K]65): void {66 plugins[pluginKey] = implementation67}6869/** ES5 Plugin */7071interface ES5BaseState extends ImmerBaseState {72 finalizing_: boolean73 assigned_: {[key: string]: any}74 parent_?: ImmerState75 revoked_: boolean76}7778export interface ES5ObjectState extends ES5BaseState {79 type_: typeof ProxyTypeES5Object80 draft_: Drafted<AnyObject, ES5ObjectState>81 base_: AnyObject82 copy_: AnyObject | null83}8485export interface ES5ArrayState extends ES5BaseState {86 type_: typeof ProxyTypeES5Array87 draft_: Drafted<AnyObject, ES5ArrayState>88 base_: AnyArray89 copy_: AnyArray | null90}9192/** Map / Set plugin */9394export interface MapState extends ImmerBaseState {95 type_: typeof ProxyTypeMap96 copy_: AnyMap | undefined97 assigned_: Map<any, boolean> | undefined98 base_: AnyMap99 revoked_: boolean100 draft_: Drafted<AnyMap, MapState>101}102103export interface SetState extends ImmerBaseState {104 type_: typeof ProxyTypeSet105 copy_: AnySet | undefined106 base_: AnySet107 drafts_: Map<any, Drafted> // maps the original value to the draft value in the new set108 revoked_: boolean109 draft_: Drafted<AnySet, SetState>110}111112/** Patches plugin */113114export type PatchPath = (string | number)[]