plugins.ts
This file
Path
src/plugins.ts
143 lines · 3.2 KiB · TypeScript
Last changed by
Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
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,Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
lines 11–14why, one step back11 ProxyTypeES5Array,12 ProxyTypeES5Object,13 ProxyTypeMap,14 ProxyTypeSetseparating plugins from coreMichel Weststratebefore adoption124518b
lines 15–41why, one step back15} from "./internal"1617/** Plugin utilities */18const plugins: {19 patches?: {20 generatePatches: typeof generatePatches21 applyPatches: typeof applyPatches22 }23 es5?: {24 willFinalizeES5: typeof willFinalizeES525 createES5Proxy: typeof createES5Proxy26 markChangedES5: typeof markChangedES527 }28 mapset?: {29 proxyMap: typeof proxyMap30 proxySet: typeof proxySet31 }32} = {}3334type Plugins = typeof plugins3536export function getPlugin<K extends keyof Plugins>(37 pluginKey: K38): Exclude<Plugins[K], undefined> {39 const plugin = plugins[pluginKey]40 if (!plugin) {41 throw new Error(New build set. Current statsMichel Weststratebefore adoption9aec0fe
lines 42–44why, one step back42 `The plugin ${pluginKey} has not been loaded into Immer. Make sure to call "enable${pluginKey[0].toUpperCase()}${pluginKey.substr(43 144 )}()" when initializing your application, just after requiring immer itself.`separating plugins from coreMichel Weststratebefore adoption124518b
lines 45–50why, one step back45 )46 }47 // @ts-ignore48 return plugin49}50TSDXMichel Weststratebefore adoption4c4fecc
line 51why, one step back51export function loadPlugin<K extends keyof Plugins>(separating plugins from coreMichel Weststratebefore adoption124518b
line 52why, one step back52 pluginKey: K,TSDXMichel Weststratebefore adoption4c4fecc
line 53why, one step back53 implementation: Plugins[K]Removed some couplingMichel Weststratebefore adoption70e77aa
line 54why, one step back54): void {TSDXMichel Weststratebefore adoption4c4fecc
line 55why, one step back55 plugins[pluginKey] = implementationseparating plugins from coreMichel Weststratebefore adoption124518b
lines 56–60why, one step back56}5758/** ES5 Plugin */5960interface ES5BaseState extends ImmerBaseState {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 61–64why, one step back61 finalizing_: boolean62 assigned_: {[key: string]: any}63 parent_?: ImmerState64 revoked_: booleanseparating plugins from coreMichel Weststratebefore adoption124518b
lines 65–67why, one step back65}6667export interface ES5ObjectState extends ES5BaseState {Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 68why, one step back68 type_: typeof ProxyTypeES5ObjectMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 69–71why, one step back69 draft_: Drafted<AnyObject, ES5ObjectState>70 base_: AnyObject71 copy_: AnyObject | nullseparating plugins from coreMichel Weststratebefore adoption124518b
lines 72–74why, one step back72}7374export interface ES5ArrayState extends ES5BaseState {Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 75why, one step back75 type_: typeof ProxyTypeES5ArrayMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 76–78why, one step back76 draft_: Drafted<AnyObject, ES5ArrayState>77 base_: AnyArray78 copy_: AnyArray | nullseparating plugins from coreMichel Weststratebefore adoption124518b
lines 79–102why, one step back79}8081export function willFinalizeES5(82 scope: ImmerScope,83 result: any,84 isReplaced: boolean85) {86 getPlugin("es5").willFinalizeES5(scope, result, isReplaced)87}8889export function createES5Proxy<T>(90 base: T,91 parent?: ImmerState92): Drafted<T, ES5ObjectState | ES5ArrayState> {93 return getPlugin("es5").createES5Proxy(base, parent)94}9596export function markChangedES5(state: ImmerState) {97 getPlugin("es5").markChangedES5(state)98}99100/** Map / Set plugin */101102export interface MapState extends ImmerBaseState {Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 103why, one step back103 type_: typeof ProxyTypeMapMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 104–108why, one step back104 copy_: AnyMap | undefined105 assigned_: Map<any, boolean> | undefined106 base_: AnyMap107 revoked_: boolean108 draft_: Drafted<AnyMap, MapState>separating plugins from coreMichel Weststratebefore adoption124518b
lines 109–111why, one step back109}110111export interface SetState extends ImmerBaseState {Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 112why, one step back112 type_: typeof ProxyTypeSetMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 113–117why, one step back113 copy_: AnySet | undefined114 base_: AnySet115 drafts_: Map<any, Drafted> // maps the original value to the draft value in the new set116 revoked_: boolean117 draft_: Drafted<AnySet, SetState>separating plugins from coreMichel Weststratebefore adoption124518b
lines 118–119why, one step back118}119TSDXMichel Weststratebefore adoption4c4fecc
line 120why, one step back120export function proxyMap<T extends AnyMap>(target: T, parent?: ImmerState): T {separating plugins from coreMichel Weststratebefore adoption124518b
lines 121–123why, one step back121 return getPlugin("mapset").proxyMap(target, parent)122}123TSDXMichel Weststratebefore adoption4c4fecc
line 124why, one step back124export function proxySet<T extends AnySet>(target: T, parent?: ImmerState): T {separating plugins from coreMichel Weststratebefore adoption124518b
lines 125–143why, one step back125 return getPlugin("mapset").proxySet(target, parent)126}127128/** Patches plugin */129130export type PatchPath = (string | number)[]131132export function generatePatches(133 state: ImmerState,134 basePath: PatchPath,135 patches: Patch[],136 inversePatches: Patch[]137): void {138 getPlugin("patches").generatePatches(state, basePath, patches, inversePatches)139}140141export function applyPatches<T>(draft: T, patches: Patch[]): T {142 return getPlugin("patches").applyPatches(draft, patches)143}