plugins.ts
This file
Path
src/utils/plugins.ts
112 lines · 2.4 KiB · TypeScript
Last changed by
Processed some own review commentsMichel Weststratebefore adoption7bb29d5
History of this fileTake it
Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 1–23why, one step back1import {2 ImmerState,3 Patch,4 ImmerScope,5 Drafted,6 AnyObject,7 ImmerBaseState,8 AnyMap,9 AnySet,10 ProxyTypeES5Array,11 ProxyTypeES5Object,12 ProxyTypeMap,13 ProxyTypeSet,14 die15} from "../internal"1617/** Plugin utilities */18const plugins: {19 Patches?: {20 generatePatches_(21 state: ImmerState,22 basePath: PatchPath,23 patches: Patch[],Processed some own review commentsMichel Weststratebefore adoption7bb29d5
line 24why, one step back24 inversePatches: Patch[]Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 25–39why, one step back25 ): void26 generateReplacementPatches_(27 rootState: ImmerState,28 replacement: any,29 patches: Patch[],30 inversePatches: Patch[]31 ): void32 applyPatches_<T>(draft: T, patches: Patch[]): T33 }34 ES5?: {35 willFinalizeES5_(scope: ImmerScope, result: any, isReplaced: boolean): void36 createES5Proxy_<T>(37 base: T,38 parent?: ImmerState39 ): Drafted<T, ES5ObjectState | ES5ArrayState>Made ES5 version workMichel Weststratebefore adoption870f44e
line 40why, one step back40 hasChanges_(state: ES5ArrayState | ES5ObjectState): booleanMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 41–85why, one step back41 }42 MapSet?: {43 proxyMap_<T extends AnyMap>(target: T, parent?: ImmerState): T44 proxySet_<T extends AnySet>(target: T, parent?: ImmerState): T45 }46} = {}4748type Plugins = typeof plugins4950export function getPlugin<K extends keyof Plugins>(51 pluginKey: K52): Exclude<Plugins[K], undefined> {53 const plugin = plugins[pluginKey]54 if (!plugin) {55 die(__DEV__ ? 18 : 19, pluginKey)56 }57 // @ts-ignore58 return plugin59}6061export function loadPlugin<K extends keyof Plugins>(62 pluginKey: K,63 implementation: Plugins[K]64): void {65 plugins[pluginKey] = implementation66}6768/** ES5 Plugin */6970interface ES5BaseState extends ImmerBaseState {71 assigned_: {[key: string]: any}72 parent_?: ImmerState73 revoked_: boolean74}7576export interface ES5ObjectState extends ES5BaseState {77 type_: typeof ProxyTypeES5Object78 draft_: Drafted<AnyObject, ES5ObjectState>79 base_: AnyObject80 copy_: AnyObject | null81}8283export interface ES5ArrayState extends ES5BaseState {84 type_: typeof ProxyTypeES5Array85 draft_: Drafted<AnyObject, ES5ArrayState>Code cleanup, bug fixesMichel Weststratebefore adoption0592d3c
lines 86–87why, one step back86 base_: any87 copy_: anyMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 88–112why, one step back88}8990/** Map / Set plugin */9192export interface MapState extends ImmerBaseState {93 type_: typeof ProxyTypeMap94 copy_: AnyMap | undefined95 assigned_: Map<any, boolean> | undefined96 base_: AnyMap97 revoked_: boolean98 draft_: Drafted<AnyMap, MapState>99}100101export interface SetState extends ImmerBaseState {102 type_: typeof ProxyTypeSet103 copy_: AnySet | undefined104 base_: AnySet105 drafts_: Map<any, Drafted> // maps the original value to the draft value in the new set106 revoked_: boolean107 draft_: Drafted<AnySet, SetState>108}109110/** Patches plugin */111112export type PatchPath = (string | number)[]