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
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–38why, 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(New build set. Current statsMichel Weststratebefore adoption9aec0fe
lines 39–41why, one step back39 `The plugin ${pluginKey} has not been loaded into Immer. Make sure to call "enable${pluginKey[0].toUpperCase()}${pluginKey.substr(40 141 )}()" when initializing your application, just after requiring immer itself.`separating plugins from coreMichel Weststratebefore adoption124518b
lines 42–47why, one step back42 )43 }44 // @ts-ignore45 return plugin46}47TSDXMichel Weststratebefore adoption4c4fecc
line 48why, one step back48export function loadPlugin<K extends keyof Plugins>(separating plugins from coreMichel Weststratebefore adoption124518b
line 49why, one step back49 pluginKey: K,TSDXMichel Weststratebefore adoption4c4fecc
line 50why, one step back50 implementation: Plugins[K]Removed some couplingMichel Weststratebefore adoption70e77aa
line 51why, one step back51): void {TSDXMichel Weststratebefore adoption4c4fecc
line 52why, one step back52 plugins[pluginKey] = implementationseparating plugins from coreMichel Weststratebefore adoption124518b
lines 53–116why, one step back53}5455/** ES5 Plugin */5657interface ES5BaseState extends ImmerBaseState {58 finalizing: boolean59 assigned: {[key: string]: any}60 parent?: ImmerState61 revoked: boolean62}6364export interface ES5ObjectState extends ES5BaseState {65 type: ProxyType.ES5Object66 draft: Drafted<AnyObject, ES5ObjectState>67 base: AnyObject68 copy: AnyObject | null69}7071export interface ES5ArrayState extends ES5BaseState {72 type: ProxyType.ES5Array73 draft: Drafted<AnyObject, ES5ArrayState>74 base: AnyArray75 copy: AnyArray | null76}7778export function willFinalizeES5(79 scope: ImmerScope,80 result: any,81 isReplaced: boolean82) {83 getPlugin("es5").willFinalizeES5(scope, result, isReplaced)84}8586export function createES5Proxy<T>(87 base: T,88 parent?: ImmerState89): Drafted<T, ES5ObjectState | ES5ArrayState> {90 return getPlugin("es5").createES5Proxy(base, parent)91}9293export function markChangedES5(state: ImmerState) {94 getPlugin("es5").markChangedES5(state)95}9697/** Map / Set plugin */9899export interface MapState extends ImmerBaseState {100 type: ProxyType.Map101 copy: AnyMap | undefined102 assigned: Map<any, boolean> | undefined103 base: AnyMap104 revoked: boolean105 draft: Drafted<AnyMap, MapState>106}107108export interface SetState extends ImmerBaseState {109 type: ProxyType.Set110 copy: AnySet | undefined111 base: AnySet112 drafts: Map<any, Drafted> // maps the original value to the draft value in the new set113 revoked: boolean114 draft: Drafted<AnySet, SetState>115}116TSDXMichel Weststratebefore adoption4c4fecc
line 117why, one step back117export function proxyMap<T extends AnyMap>(target: T, parent?: ImmerState): T {separating plugins from coreMichel Weststratebefore adoption124518b
lines 118–120why, one step back118 return getPlugin("mapset").proxyMap(target, parent)119}120TSDXMichel Weststratebefore adoption4c4fecc
line 121why, one step back121export function proxySet<T extends AnySet>(target: T, parent?: ImmerState): T {separating plugins from coreMichel Weststratebefore adoption124518b
lines 122–140why, one step back122 return getPlugin("mapset").proxySet(target, parent)123}124125/** Patches plugin */126127export type PatchPath = (string | number)[]128129export function generatePatches(130 state: ImmerState,131 basePath: PatchPath,132 patches: Patch[],133 inversePatches: Patch[]134): void {135 getPlugin("patches").generatePatches(state, basePath, patches, inversePatches)136}137138export function applyPatches<T>(draft: T, patches: Patch[]): T {139 return getPlugin("patches").applyPatches(draft, patches)140}