plugins.ts
2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import {
ImmerState,
Patch,
ImmerScope,
Drafted,
AnyObject,
ImmerBaseState,
AnyMap,
AnySet,
ProxyType,
die
} from "../internal"
/** Plugin utilities */
const plugins: {
Patches?: {
generatePatches_(
state: ImmerState,
basePath: PatchPath,
patches: Patch[],
inversePatches: Patch[]
): void
generateReplacementPatches_(
base: any,
replacement: any,
patches: Patch[],
inversePatches: Patch[]
): void
applyPatches_<T>(draft: T, patches: Patch[]): T
}
ES5?: {
willFinalizeES5_(scope: ImmerScope, result: any, isReplaced: boolean): void
createES5Proxy_<T>(
base: T,
parent?: ImmerState
): Drafted<T, ES5ObjectState | ES5ArrayState>
hasChanges_(state: ES5ArrayState | ES5ObjectState): boolean
}
MapSet?: {
proxyMap_<T extends AnyMap>(target: T, parent?: ImmerState): T
proxySet_<T extends AnySet>(target: T, parent?: ImmerState): T
}
} = {}
type Plugins = typeof plugins
export function getPlugin<K extends keyof Plugins>(
pluginKey: K
): Exclude<Plugins[K], undefined> {
const plugin = plugins[pluginKey]
if (!plugin) {
die(18, pluginKey)
}
// @ts-ignore
return plugin
}
export function loadPlugin<K extends keyof Plugins>(
pluginKey: K,
implementation: Plugins[K]
): void {
if (!plugins[pluginKey]) plugins[pluginKey] = implementation
}
/** ES5 Plugin */
interface ES5BaseState extends ImmerBaseState {
assigned_: {[key: string]: any}
parent_?: ImmerState
revoked_: boolean
}
export interface ES5ObjectState extends ES5BaseState {
type_: ProxyType.ES5Object
draft_: Drafted<AnyObject, ES5ObjectState>
base_: AnyObject
copy_: AnyObject | null
}
export interface ES5ArrayState extends ES5BaseState {
type_: ProxyType.ES5Array
draft_: Drafted<AnyObject, ES5ArrayState>
base_: any
copy_: any
}
/** Map / Set plugin */
export interface MapState extends ImmerBaseState {
type_: ProxyType.Map
copy_: AnyMap | undefined
assigned_: Map<any, boolean> | undefined
base_: AnyMap
revoked_: boolean
draft_: Drafted<AnyMap, MapState>
}
export interface SetState extends ImmerBaseState {
type_: ProxyType.Set
copy_: AnySet | undefined
base_: AnySet
drafts_: Map<any, Drafted> // maps the original value to the draft value in the new set
revoked_: boolean
draft_: Drafted<AnySet, SetState>
}
/** Patches plugin */
export type PatchPath = (string | number)[]