plugins.ts
This file
Path
src/plugins.ts
138 lines · 3.1 KiB · TypeScript
Last changed by
TSDXMichel Weststratebefore adoption4c4fecc
History of this fileTake it
separating plugins from coreMichel Weststratebefore adoption124518b
lines 1–9why, one step back1import {2 ImmerState,3 Patch,4 ImmerScope,5 Drafted,6 AnyObject,7 ImmerBaseState,8 AnyArray,9 AnyMap,Removed some couplingMichel Weststratebefore adoption70e77aa
line 10why, one step back10 AnySet,TSDXMichel Weststratebefore adoption4c4fecc
line 11why, one step back11 ProxyTypeseparating plugins from coreMichel Weststratebefore adoption124518b
lines 12–45why, one step back12} from "./internal"1314/** Plugin utilities */15const plugins: {16 patches?: {17 generatePatches: typeof generatePatches18 applyPatches: typeof applyPatches19 }20 es5?: {21 willFinalizeES5: typeof willFinalizeES522 createES5Proxy: typeof createES5Proxy23 markChangedES5: typeof markChangedES524 }25 mapset?: {26 proxyMap: typeof proxyMap27 proxySet: typeof proxySet28 }29} = {}3031type Plugins = typeof plugins3233export function getPlugin<K extends keyof Plugins>(34 pluginKey: K35): Exclude<Plugins[K], undefined> {36 const plugin = plugins[pluginKey]37 if (!plugin) {38 throw new Error(39 `The plugin ${pluginKey} has not been loaded into Immer. Make sure the require from "immer/${pluginKey}" when initializing your application, just after requiring immer itself.`40 )41 }42 // @ts-ignore43 return plugin44}45TSDXMichel Weststratebefore adoption4c4fecc
line 46why, one step back46export function loadPlugin<K extends keyof Plugins>(separating plugins from coreMichel Weststratebefore adoption124518b
line 47why, one step back47 pluginKey: K,TSDXMichel Weststratebefore adoption4c4fecc
line 48why, one step back48 implementation: Plugins[K]Removed some couplingMichel Weststratebefore adoption70e77aa
line 49why, one step back49): void {TSDXMichel Weststratebefore adoption4c4fecc
line 50why, one step back50 plugins[pluginKey] = implementationseparating plugins from coreMichel Weststratebefore adoption124518b
lines 51–114why, one step back51}5253/** ES5 Plugin */5455interface ES5BaseState extends ImmerBaseState {56 finalizing: boolean57 assigned: {[key: string]: any}58 parent?: ImmerState59 revoked: boolean60}6162export interface ES5ObjectState extends ES5BaseState {63 type: ProxyType.ES5Object64 draft: Drafted<AnyObject, ES5ObjectState>65 base: AnyObject66 copy: AnyObject | null67}6869export interface ES5ArrayState extends ES5BaseState {70 type: ProxyType.ES5Array71 draft: Drafted<AnyObject, ES5ArrayState>72 base: AnyArray73 copy: AnyArray | null74}7576export function willFinalizeES5(77 scope: ImmerScope,78 result: any,79 isReplaced: boolean80) {81 getPlugin("es5").willFinalizeES5(scope, result, isReplaced)82}8384export function createES5Proxy<T>(85 base: T,86 parent?: ImmerState87): Drafted<T, ES5ObjectState | ES5ArrayState> {88 return getPlugin("es5").createES5Proxy(base, parent)89}9091export function markChangedES5(state: ImmerState) {92 getPlugin("es5").markChangedES5(state)93}9495/** Map / Set plugin */9697export interface MapState extends ImmerBaseState {98 type: ProxyType.Map99 copy: AnyMap | undefined100 assigned: Map<any, boolean> | undefined101 base: AnyMap102 revoked: boolean103 draft: Drafted<AnyMap, MapState>104}105106export interface SetState extends ImmerBaseState {107 type: ProxyType.Set108 copy: AnySet | undefined109 base: AnySet110 drafts: Map<any, Drafted> // maps the original value to the draft value in the new set111 revoked: boolean112 draft: Drafted<AnySet, SetState>113}114TSDXMichel Weststratebefore adoption4c4fecc
line 115why, one step back115export function proxyMap<T extends AnyMap>(target: T, parent?: ImmerState): T {separating plugins from coreMichel Weststratebefore adoption124518b
lines 116–118why, one step back116 return getPlugin("mapset").proxyMap(target, parent)117}118TSDXMichel Weststratebefore adoption4c4fecc
line 119why, one step back119export function proxySet<T extends AnySet>(target: T, parent?: ImmerState): T {separating plugins from coreMichel Weststratebefore adoption124518b
lines 120–138why, one step back120 return getPlugin("mapset").proxySet(target, parent)121}122123/** Patches plugin */124125export type PatchPath = (string | number)[]126127export function generatePatches(128 state: ImmerState,129 basePath: PatchPath,130 patches: Patch[],131 inversePatches: Patch[]132): void {133 getPlugin("patches").generatePatches(state, basePath, patches, inversePatches)134}135136export function applyPatches<T>(draft: T, patches: Patch[]): T {137 return getPlugin("patches").applyPatches(draft, patches)138}