plugins.ts
This file
Path
src/plugins.ts
145 lines · 3.1 KiB · TypeScript
Last changed by
separating plugins from coreMichel Weststratebefore adoption124518b
History of this fileTake it
separating plugins from coreMichel Weststratebefore adoption124518b
lines 1–145why, one step back1import {2 ImmerState,3 Patch,4 ImmerScope,5 Drafted,6 AnyObject,7 ImmerBaseState,8 AnyArray,9 ProxyType,10 AnyMap,11 DRAFT_STATE,12 AnySet13} from "./internal"1415/** Plugin utilities */16const plugins: {17 patches?: {18 generatePatches: typeof generatePatches19 applyPatches: typeof applyPatches20 }21 es5?: {22 willFinalizeES5: typeof willFinalizeES523 createES5Proxy: typeof createES5Proxy24 markChangedES5: typeof markChangedES525 }26 mapset?: {27 proxyMap: typeof proxyMap28 proxySet: typeof proxySet29 }30} = {}3132type Plugins = typeof plugins3334export function getPlugin<K extends keyof Plugins>(35 pluginKey: K36): Exclude<Plugins[K], undefined> {37 const plugin = plugins[pluginKey]38 if (!plugin) {39 throw new Error(40 `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.`41 )42 }43 // @ts-ignore44 return plugin45}4647export function loadPlugin<K extends keyof Plugins>(48 pluginKey: K,49 implementation: Plugins[K]50) {51 plugins[pluginKey] = implementation52}5354/** ES5 Plugin */5556interface ES5BaseState extends ImmerBaseState {57 finalizing: boolean58 assigned: {[key: string]: any}59 parent?: ImmerState60 revoked: boolean61}6263export interface ES5ObjectState extends ES5BaseState {64 type: ProxyType.ES5Object65 draft: Drafted<AnyObject, ES5ObjectState>66 base: AnyObject67 copy: AnyObject | null68}6970export interface ES5ArrayState extends ES5BaseState {71 type: ProxyType.ES5Array72 draft: Drafted<AnyObject, ES5ArrayState>73 base: AnyArray74 copy: AnyArray | null75}7677export function willFinalizeES5(78 scope: ImmerScope,79 result: any,80 isReplaced: boolean81) {82 getPlugin("es5").willFinalizeES5(scope, result, isReplaced)83}8485export function createES5Proxy<T>(86 base: T,87 parent?: ImmerState88): Drafted<T, ES5ObjectState | ES5ArrayState> {89 return getPlugin("es5").createES5Proxy(base, parent)90}9192export function markChangedES5(state: ImmerState) {93 getPlugin("es5").markChangedES5(state)94}9596/** Map / Set plugin */9798export interface MapState extends ImmerBaseState {99 type: ProxyType.Map100 copy: AnyMap | undefined101 assigned: Map<any, boolean> | undefined102 base: AnyMap103 revoked: boolean104 draft: Drafted<AnyMap, MapState>105}106107export interface SetState extends ImmerBaseState {108 type: ProxyType.Set109 copy: AnySet | undefined110 base: AnySet111 drafts: Map<any, Drafted> // maps the original value to the draft value in the new set112 revoked: boolean113 draft: Drafted<AnySet, SetState>114}115116export function proxyMap<T extends AnyMap>(117 target: T,118 parent?: ImmerState119): T & {[DRAFT_STATE]: MapState} {120 return getPlugin("mapset").proxyMap(target, parent)121}122123export function proxySet<T extends AnySet>(124 target: T,125 parent?: ImmerState126): T & {[DRAFT_STATE]: SetState} {127 return getPlugin("mapset").proxySet(target, parent)128}129130/** Patches plugin */131132export type PatchPath = (string | number)[]133134export function generatePatches(135 state: ImmerState,136 basePath: PatchPath,137 patches: Patch[],138 inversePatches: Patch[]139): void {140 getPlugin("patches").generatePatches(state, basePath, patches, inversePatches)141}142143export function applyPatches<T>(draft: T, patches: Patch[]): T {144 return getPlugin("patches").applyPatches(draft, patches)145}