Skip to content
Myr

plugins.ts

This file

Path

src/plugins.ts

143 lines · 3.2 KiB · TypeScript

Last changed by

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

History of this file

Take it

separating plugins from coreMichel Weststratebefore adoption124518b

lines 1–9why, one step back
1import {
2 ImmerState,3 Patch,4 ImmerScope,5 Drafted,6 AnyObject,7 ImmerBaseState,8 AnyArray,9 AnyMap,

Removed some couplingMichel Weststratebefore adoption70e77aa

line 10why, one step back
10 AnySet,

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

lines 11–14why, one step back
11 ProxyTypeES5Array,
12 ProxyTypeES5Object,13 ProxyTypeMap,14 ProxyTypeSet

separating plugins from coreMichel Weststratebefore adoption124518b

lines 15–41why, one step back
15} from "./internal"
1617/** Plugin utilities */18const plugins: {19 patches?: {20 generatePatches: typeof generatePatches21 applyPatches: typeof applyPatches22 }23 es5?: {24 willFinalizeES5: typeof willFinalizeES525 createES5Proxy: typeof createES5Proxy26 markChangedES5: typeof markChangedES527 }28 mapset?: {29 proxyMap: typeof proxyMap30 proxySet: typeof proxySet31 }32} = {}3334type Plugins = typeof plugins3536export function getPlugin<K extends keyof Plugins>(37 pluginKey: K38): Exclude<Plugins[K], undefined> {39 const plugin = plugins[pluginKey]40 if (!plugin) {41 throw new Error(

New build set. Current statsMichel Weststratebefore adoption9aec0fe

lines 42–44why, one step back
42 `The plugin ${pluginKey} has not been loaded into Immer. Make sure to call "enable${pluginKey[0].toUpperCase()}${pluginKey.substr(
43 144 )}()" when initializing your application, just after requiring immer itself.`

separating plugins from coreMichel Weststratebefore adoption124518b

lines 45–50why, one step back
45 )
46 }47 // @ts-ignore48 return plugin49}50

TSDXMichel Weststratebefore adoption4c4fecc

line 51why, one step back
51export function loadPlugin<K extends keyof Plugins>(

separating plugins from coreMichel Weststratebefore adoption124518b

line 52why, one step back
52 pluginKey: K,

TSDXMichel Weststratebefore adoption4c4fecc

line 53why, one step back
53 implementation: Plugins[K]

Removed some couplingMichel Weststratebefore adoption70e77aa

line 54why, one step back
54): void {

TSDXMichel Weststratebefore adoption4c4fecc

line 55why, one step back
55 plugins[pluginKey] = implementation

separating plugins from coreMichel Weststratebefore adoption124518b

lines 56–60why, one step back
56}
5758/** ES5 Plugin */5960interface ES5BaseState extends ImmerBaseState {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 61–64why, one step back
61 finalizing_: boolean
62 assigned_: {[key: string]: any}63 parent_?: ImmerState64 revoked_: boolean

separating plugins from coreMichel Weststratebefore adoption124518b

lines 65–67why, one step back
65}
6667export interface ES5ObjectState extends ES5BaseState {

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 68why, one step back
68 type_: typeof ProxyTypeES5Object

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 69–71why, one step back
69 draft_: Drafted<AnyObject, ES5ObjectState>
70 base_: AnyObject71 copy_: AnyObject | null

separating plugins from coreMichel Weststratebefore adoption124518b

lines 72–74why, one step back
72}
7374export interface ES5ArrayState extends ES5BaseState {

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 75why, one step back
75 type_: typeof ProxyTypeES5Array

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 76–78why, one step back
76 draft_: Drafted<AnyObject, ES5ArrayState>
77 base_: AnyArray78 copy_: AnyArray | null

separating plugins from coreMichel Weststratebefore adoption124518b

lines 79–102why, one step back
79}
8081export function willFinalizeES5(82 scope: ImmerScope,83 result: any,84 isReplaced: boolean85) {86 getPlugin("es5").willFinalizeES5(scope, result, isReplaced)87}8889export function createES5Proxy<T>(90 base: T,91 parent?: ImmerState92): Drafted<T, ES5ObjectState | ES5ArrayState> {93 return getPlugin("es5").createES5Proxy(base, parent)94}9596export function markChangedES5(state: ImmerState) {97 getPlugin("es5").markChangedES5(state)98}99100/** Map / Set plugin */101102export interface MapState extends ImmerBaseState {

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 103why, one step back
103 type_: typeof ProxyTypeMap

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 104–108why, one step back
104 copy_: AnyMap | undefined
105 assigned_: Map<any, boolean> | undefined106 base_: AnyMap107 revoked_: boolean108 draft_: Drafted<AnyMap, MapState>

separating plugins from coreMichel Weststratebefore adoption124518b

lines 109–111why, one step back
109}
110111export interface SetState extends ImmerBaseState {

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 112why, one step back
112 type_: typeof ProxyTypeSet

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 113–117why, one step back
113 copy_: AnySet | undefined
114 base_: AnySet115 drafts_: Map<any, Drafted> // maps the original value to the draft value in the new set116 revoked_: boolean117 draft_: Drafted<AnySet, SetState>

separating plugins from coreMichel Weststratebefore adoption124518b

lines 118–119why, one step back
118}
119

TSDXMichel Weststratebefore adoption4c4fecc

line 120why, one step back
120export function proxyMap<T extends AnyMap>(target: T, parent?: ImmerState): T {

separating plugins from coreMichel Weststratebefore adoption124518b

lines 121–123why, one step back
121 return getPlugin("mapset").proxyMap(target, parent)
122}123

TSDXMichel Weststratebefore adoption4c4fecc

line 124why, one step back
124export function proxySet<T extends AnySet>(target: T, parent?: ImmerState): T {

separating plugins from coreMichel Weststratebefore adoption124518b

lines 125–143why, one step back
125 return getPlugin("mapset").proxySet(target, parent)
126}127128/** Patches plugin */129130export type PatchPath = (string | number)[]131132export function generatePatches(133 state: ImmerState,134 basePath: PatchPath,135 patches: Patch[],136 inversePatches: Patch[]137): void {138 getPlugin("patches").generatePatches(state, basePath, patches, inversePatches)139}140141export function applyPatches<T>(draft: T, patches: Patch[]): T {142 return getPlugin("patches").applyPatches(draft, patches)143}