plugins.ts
This file
Path
src/utils/plugins.ts
115 lines · 2.5 KiB · TypeScript
Last changed by
Reimplement ES5 to work with descriptors rather than valuesMichel Weststratebefore adoptiond0f3a71
History of this fileTake it
Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 1–41why, 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): voidReimplement ES5 to work with descriptors rather than valuesMichel Weststratebefore adoptiond0f3a71
line 42why, one step back42 createFinalCopy_(state: ES5ArrayState | ES5ObjectState): anyMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 43–115why, one step back43 }44 MapSet?: {45 proxyMap_<T extends AnyMap>(target: T, parent?: ImmerState): T46 proxySet_<T extends AnySet>(target: T, parent?: ImmerState): T47 }48} = {}4950type Plugins = typeof plugins5152export function getPlugin<K extends keyof Plugins>(53 pluginKey: K54): Exclude<Plugins[K], undefined> {55 const plugin = plugins[pluginKey]56 if (!plugin) {57 die(__DEV__ ? 18 : 19, pluginKey)58 }59 // @ts-ignore60 return plugin61}6263export function loadPlugin<K extends keyof Plugins>(64 pluginKey: K,65 implementation: Plugins[K]66): void {67 plugins[pluginKey] = implementation68}6970/** ES5 Plugin */7172interface ES5BaseState extends ImmerBaseState {73 finalizing_: boolean74 assigned_: {[key: string]: any}75 parent_?: ImmerState76 revoked_: boolean77}7879export interface ES5ObjectState extends ES5BaseState {80 type_: typeof ProxyTypeES5Object81 draft_: Drafted<AnyObject, ES5ObjectState>82 base_: AnyObject83 copy_: AnyObject | null84}8586export interface ES5ArrayState extends ES5BaseState {87 type_: typeof ProxyTypeES5Array88 draft_: Drafted<AnyObject, ES5ArrayState>89 base_: AnyArray90 copy_: AnyArray | null91}9293/** Map / Set plugin */9495export interface MapState extends ImmerBaseState {96 type_: typeof ProxyTypeMap97 copy_: AnyMap | undefined98 assigned_: Map<any, boolean> | undefined99 base_: AnyMap100 revoked_: boolean101 draft_: Drafted<AnyMap, MapState>102}103104export interface SetState extends ImmerBaseState {105 type_: typeof ProxyTypeSet106 copy_: AnySet | undefined107 base_: AnySet108 drafts_: Map<any, Drafted> // maps the original value to the draft value in the new set109 revoked_: boolean110 draft_: Drafted<AnySet, SetState>111}112113/** Patches plugin */114115export type PatchPath = (string | number)[]