plugins.ts
This file
Path
src/crap/plugins.ts
108 lines · 2.3 KiB · TypeScript
Last changed by
Moving files into foldersMichel Weststratebefore adoptionab49edc
History of this fileTake it
Moving files into foldersMichel Weststratebefore adoptionab49edc
lines 1–108why, one step back1import {2 ImmerState,3 Patch,4 ImmerScope,5 Drafted,6 AnyObject,7 ImmerBaseState,8 AnyArray,9 AnyMap,10 AnySet,11 ProxyTypeES5Array,12 ProxyTypeES5Object,13 ProxyTypeMap,14 ProxyTypeSet,15 die16} from "../internal"1718/** Plugin utilities */19const plugins: {20 patches?: {21 generatePatches_(22 state: ImmerState,23 basePath: PatchPath,24 patches: Patch[],25 inversePatches: Patch[]26 ): void27 applyPatches_<T>(draft: T, patches: Patch[]): T28 }29 es5?: {30 willFinalizeES5_(scope: ImmerScope, result: any, isReplaced: boolean): void31 createES5Proxy_<T>(32 base: T,33 parent?: ImmerState34 ): Drafted<T, ES5ObjectState | ES5ArrayState>35 markChangedES5_(state: ImmerState): void36 }37 mapset?: {38 proxyMap_<T extends AnyMap>(target: T, parent?: ImmerState): T39 proxySet_<T extends AnySet>(target: T, parent?: ImmerState): T40 }41} = {}4243type Plugins = typeof plugins4445export function getPlugin<K extends keyof Plugins>(46 pluginKey: K47): Exclude<Plugins[K], undefined> {48 const plugin = plugins[pluginKey]49 if (!plugin) {50 die(__DEV__ ? 18 : 19, plugin)51 }52 // @ts-ignore53 return plugin54}5556export function loadPlugin<K extends keyof Plugins>(57 pluginKey: K,58 implementation: Plugins[K]59): void {60 plugins[pluginKey] = implementation61}6263/** ES5 Plugin */6465interface ES5BaseState extends ImmerBaseState {66 finalizing_: boolean67 assigned_: {[key: string]: any}68 parent_?: ImmerState69 revoked_: boolean70}7172export interface ES5ObjectState extends ES5BaseState {73 type_: typeof ProxyTypeES5Object74 draft_: Drafted<AnyObject, ES5ObjectState>75 base_: AnyObject76 copy_: AnyObject | null77}7879export interface ES5ArrayState extends ES5BaseState {80 type_: typeof ProxyTypeES5Array81 draft_: Drafted<AnyObject, ES5ArrayState>82 base_: AnyArray83 copy_: AnyArray | null84}8586/** Map / Set plugin */8788export interface MapState extends ImmerBaseState {89 type_: typeof ProxyTypeMap90 copy_: AnyMap | undefined91 assigned_: Map<any, boolean> | undefined92 base_: AnyMap93 revoked_: boolean94 draft_: Drafted<AnyMap, MapState>95}9697export interface SetState extends ImmerBaseState {98 type_: typeof ProxyTypeSet99 copy_: AnySet | undefined100 base_: AnySet101 drafts_: Map<any, Drafted> // maps the original value to the draft value in the new set102 revoked_: boolean103 draft_: Drafted<AnySet, SetState>104}105106/** Patches plugin */107108export type PatchPath = (string | number)[]