Skip to content
Myr

plugins.ts

This file

Path

src/utils/plugins.ts

113 lines · 2.4 KiB · TypeScript

Last changed by

fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5

History of this file

Take it

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

lines 1–23why, 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[],

fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5

lines 24–25why, one step back
24 inversePatches: Patch[],
25 originalCopy: any

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

lines 26–40why, one step back
26 ): void
27 generateReplacementPatches_(28 rootState: ImmerState,29 replacement: any,30 patches: Patch[],31 inversePatches: Patch[]32 ): void33 applyPatches_<T>(draft: T, patches: Patch[]): T34 }35 ES5?: {36 willFinalizeES5_(scope: ImmerScope, result: any, isReplaced: boolean): void37 createES5Proxy_<T>(38 base: T,39 parent?: ImmerState40 ): Drafted<T, ES5ObjectState | ES5ArrayState>

Made ES5 version workMichel Weststratebefore adoption870f44e

line 41why, one step back
41 hasChanges_(state: ES5ArrayState | ES5ObjectState): boolean

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

lines 42–86why, one step back
42 }
43 MapSet?: {44 proxyMap_<T extends AnyMap>(target: T, parent?: ImmerState): T45 proxySet_<T extends AnySet>(target: T, parent?: ImmerState): T46 }47} = {}4849type Plugins = typeof plugins5051export function getPlugin<K extends keyof Plugins>(52 pluginKey: K53): Exclude<Plugins[K], undefined> {54 const plugin = plugins[pluginKey]55 if (!plugin) {56 die(__DEV__ ? 18 : 19, pluginKey)57 }58 // @ts-ignore59 return plugin60}6162export function loadPlugin<K extends keyof Plugins>(63 pluginKey: K,64 implementation: Plugins[K]65): void {66 plugins[pluginKey] = implementation67}6869/** ES5 Plugin */7071interface ES5BaseState extends ImmerBaseState {72 assigned_: {[key: string]: any}73 parent_?: ImmerState74 revoked_: boolean75}7677export interface ES5ObjectState extends ES5BaseState {78 type_: typeof ProxyTypeES5Object79 draft_: Drafted<AnyObject, ES5ObjectState>80 base_: AnyObject81 copy_: AnyObject | null82}8384export interface ES5ArrayState extends ES5BaseState {85 type_: typeof ProxyTypeES5Array86 draft_: Drafted<AnyObject, ES5ArrayState>

Code cleanup, bug fixesMichel Weststratebefore adoption0592d3c

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

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

lines 89–113why, one step back
89}
9091/** Map / Set plugin */9293export interface MapState extends ImmerBaseState {94 type_: typeof ProxyTypeMap95 copy_: AnyMap | undefined96 assigned_: Map<any, boolean> | undefined97 base_: AnyMap98 revoked_: boolean99 draft_: Drafted<AnyMap, MapState>100}101102export interface SetState extends ImmerBaseState {103 type_: typeof ProxyTypeSet104 copy_: AnySet | undefined105 base_: AnySet106 drafts_: Map<any, Drafted> // maps the original value to the draft value in the new set107 revoked_: boolean108 draft_: Drafted<AnySet, SetState>109}110111/** Patches plugin */112113export type PatchPath = (string | number)[]