Skip to content
Myr

plugins.ts

This file

Path

src/utils/plugins.ts

112 lines · 2.3 KiB · TypeScript

Last changed by

Code cleanup, bug fixesMichel Weststratebefore adoption0592d3c

History of this file

Take it

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 1–85why, one step back
1import {
2 ImmerState,3 Patch,4 ImmerScope,5 Drafted,6 AnyObject,7 ImmerBaseState,8 AnyMap,9 AnySet,10 ProxyTypeES5Array,11 ProxyTypeES5Object,12 ProxyTypeMap,13 ProxyTypeSet,14 die15} from "../internal"1617/** Plugin utilities */18const plugins: {19 Patches?: {20 generatePatches_(21 state: ImmerState,22 basePath: PatchPath,23 patches: Patch[],24 inversePatches: Patch[]25 ): void26 generateReplacementPatches_(27 rootState: ImmerState,28 replacement: any,29 patches: Patch[],30 inversePatches: Patch[]31 ): void32 applyPatches_<T>(draft: T, patches: Patch[]): T33 }34 ES5?: {35 willFinalizeES5_(scope: ImmerScope, result: any, isReplaced: boolean): void36 createES5Proxy_<T>(37 base: T,38 parent?: ImmerState39 ): Drafted<T, ES5ObjectState | ES5ArrayState>40 }41 MapSet?: {42 proxyMap_<T extends AnyMap>(target: T, parent?: ImmerState): T43 proxySet_<T extends AnySet>(target: T, parent?: ImmerState): T44 }45} = {}4647type Plugins = typeof plugins4849export function getPlugin<K extends keyof Plugins>(50 pluginKey: K51): Exclude<Plugins[K], undefined> {52 const plugin = plugins[pluginKey]53 if (!plugin) {54 die(__DEV__ ? 18 : 19, pluginKey)55 }56 // @ts-ignore57 return plugin58}5960export function loadPlugin<K extends keyof Plugins>(61 pluginKey: K,62 implementation: Plugins[K]63): void {64 plugins[pluginKey] = implementation65}6667/** ES5 Plugin */6869interface ES5BaseState extends ImmerBaseState {70 finalizing_: boolean71 assigned_: {[key: string]: any}72 parent_?: ImmerState73 revoked_: boolean74}7576export interface ES5ObjectState extends ES5BaseState {77 type_: typeof ProxyTypeES5Object78 draft_: Drafted<AnyObject, ES5ObjectState>79 base_: AnyObject80 copy_: AnyObject | null81}8283export interface ES5ArrayState extends ES5BaseState {84 type_: typeof ProxyTypeES5Array85 draft_: Drafted<AnyObject, ES5ArrayState>

Code cleanup, bug fixesMichel Weststratebefore adoption0592d3c

lines 86–87why, one step back
86 base_: any
87 copy_: any

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 88–112why, one step back
88}
8990/** Map / Set plugin */9192export interface MapState extends ImmerBaseState {93 type_: typeof ProxyTypeMap94 copy_: AnyMap | undefined95 assigned_: Map<any, boolean> | undefined96 base_: AnyMap97 revoked_: boolean98 draft_: Drafted<AnyMap, MapState>99}100101export interface SetState extends ImmerBaseState {102 type_: typeof ProxyTypeSet103 copy_: AnySet | undefined104 base_: AnySet105 drafts_: Map<any, Drafted> // maps the original value to the draft value in the new set106 revoked_: boolean107 draft_: Drafted<AnySet, SetState>108}109110/** Patches plugin */111112export type PatchPath = (string | number)[]