Skip to content
Myr

plugins.ts

This file

Path

src/plugins.ts

140 lines · 3.1 KiB · TypeScript

Last changed by

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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,

TSDXMichel Weststratebefore adoption4c4fecc

line 11why, one step back
11 ProxyType

separating plugins from coreMichel Weststratebefore adoption124518b

lines 12–38why, one step back
12} from "./internal"
1314/** Plugin utilities */15const plugins: {16 patches?: {17 generatePatches: typeof generatePatches18 applyPatches: typeof applyPatches19 }20 es5?: {21 willFinalizeES5: typeof willFinalizeES522 createES5Proxy: typeof createES5Proxy23 markChangedES5: typeof markChangedES524 }25 mapset?: {26 proxyMap: typeof proxyMap27 proxySet: typeof proxySet28 }29} = {}3031type Plugins = typeof plugins3233export function getPlugin<K extends keyof Plugins>(34 pluginKey: K35): Exclude<Plugins[K], undefined> {36 const plugin = plugins[pluginKey]37 if (!plugin) {38 throw new Error(

New build set. Current statsMichel Weststratebefore adoption9aec0fe

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

separating plugins from coreMichel Weststratebefore adoption124518b

lines 42–47why, one step back
42 )
43 }44 // @ts-ignore45 return plugin46}47

TSDXMichel Weststratebefore adoption4c4fecc

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

separating plugins from coreMichel Weststratebefore adoption124518b

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Removed some couplingMichel Weststratebefore adoption70e77aa

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

TSDXMichel Weststratebefore adoption4c4fecc

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

separating plugins from coreMichel Weststratebefore adoption124518b

lines 53–57why, one step back
53}
5455/** ES5 Plugin */5657interface ES5BaseState extends ImmerBaseState {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 58–61why, one step back
58 finalizing_: boolean
59 assigned_: {[key: string]: any}60 parent_?: ImmerState61 revoked_: boolean

separating plugins from coreMichel Weststratebefore adoption124518b

lines 62–64why, one step back
62}
6364export interface ES5ObjectState extends ES5BaseState {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 65–68why, one step back
65 type_: ProxyType.ES5Object
66 draft_: Drafted<AnyObject, ES5ObjectState>67 base_: AnyObject68 copy_: AnyObject | null

separating plugins from coreMichel Weststratebefore adoption124518b

lines 69–71why, one step back
69}
7071export interface ES5ArrayState extends ES5BaseState {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 72–75why, one step back
72 type_: ProxyType.ES5Array
73 draft_: Drafted<AnyObject, ES5ArrayState>74 base_: AnyArray75 copy_: AnyArray | null

separating plugins from coreMichel Weststratebefore adoption124518b

lines 76–99why, one step back
76}
7778export function willFinalizeES5(79 scope: ImmerScope,80 result: any,81 isReplaced: boolean82) {83 getPlugin("es5").willFinalizeES5(scope, result, isReplaced)84}8586export function createES5Proxy<T>(87 base: T,88 parent?: ImmerState89): Drafted<T, ES5ObjectState | ES5ArrayState> {90 return getPlugin("es5").createES5Proxy(base, parent)91}9293export function markChangedES5(state: ImmerState) {94 getPlugin("es5").markChangedES5(state)95}9697/** Map / Set plugin */9899export interface MapState extends ImmerBaseState {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 100–105why, one step back
100 type_: ProxyType.Map
101 copy_: AnyMap | undefined102 assigned_: Map<any, boolean> | undefined103 base_: AnyMap104 revoked_: boolean105 draft_: Drafted<AnyMap, MapState>

separating plugins from coreMichel Weststratebefore adoption124518b

lines 106–108why, one step back
106}
107108export interface SetState extends ImmerBaseState {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 109–114why, one step back
109 type_: ProxyType.Set
110 copy_: AnySet | undefined111 base_: AnySet112 drafts_: Map<any, Drafted> // maps the original value to the draft value in the new set113 revoked_: boolean114 draft_: Drafted<AnySet, SetState>

separating plugins from coreMichel Weststratebefore adoption124518b

lines 115–116why, one step back
115}
116

TSDXMichel Weststratebefore adoption4c4fecc

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

separating plugins from coreMichel Weststratebefore adoption124518b

lines 118–120why, one step back
118 return getPlugin("mapset").proxyMap(target, parent)
119}120

TSDXMichel Weststratebefore adoption4c4fecc

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

separating plugins from coreMichel Weststratebefore adoption124518b

lines 122–140why, one step back
122 return getPlugin("mapset").proxySet(target, parent)
123}124125/** Patches plugin */126127export type PatchPath = (string | number)[]128129export function generatePatches(130 state: ImmerState,131 basePath: PatchPath,132 patches: Patch[],133 inversePatches: Patch[]134): void {135 getPlugin("patches").generatePatches(state, basePath, patches, inversePatches)136}137138export function applyPatches<T>(draft: T, patches: Patch[]): T {139 return getPlugin("patches").applyPatches(draft, patches)140}