Skip to content
Myr

plugins.ts

This file

Path

src/utils/plugins.ts

111 lines · 2.3 KiB · TypeScript

Last changed by

Some cleanupMichel Weststratebefore adoptionb3b43b9

History of this file

Take it

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

lines 1–84why, 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 assigned_: {[key: string]: any}71 parent_?: ImmerState72 revoked_: boolean73}7475export interface ES5ObjectState extends ES5BaseState {76 type_: typeof ProxyTypeES5Object77 draft_: Drafted<AnyObject, ES5ObjectState>78 base_: AnyObject79 copy_: AnyObject | null80}8182export interface ES5ArrayState extends ES5BaseState {83 type_: typeof ProxyTypeES5Array84 draft_: Drafted<AnyObject, ES5ArrayState>

Code cleanup, bug fixesMichel Weststratebefore adoption0592d3c

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

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

lines 87–111why, one step back
87}
8889/** Map / Set plugin */9091export interface MapState extends ImmerBaseState {92 type_: typeof ProxyTypeMap93 copy_: AnyMap | undefined94 assigned_: Map<any, boolean> | undefined95 base_: AnyMap96 revoked_: boolean97 draft_: Drafted<AnyMap, MapState>98}99100export interface SetState extends ImmerBaseState {101 type_: typeof ProxyTypeSet102 copy_: AnySet | undefined103 base_: AnySet104 drafts_: Map<any, Drafted> // maps the original value to the draft value in the new set105 revoked_: boolean106 draft_: Drafted<AnySet, SetState>107}108109/** Patches plugin */110111export type PatchPath = (string | number)[]