UNPKG

1.6 kBPlain TextView Raw
1import {
2 ImmerState,
3 Patch,
4 Drafted,
5 ImmerBaseState,
6 AnyMap,
7 AnySet,
8 ArchType,
9 die
10} from "../internal"
11
12/** Plugin utilities */
13const plugins: {
14 Patches?: {
15 generatePatches_(
16 state: ImmerState,
17 basePath: PatchPath,
18 patches: Patch[],
19 inversePatches: Patch[]
20 ): void
21 generateReplacementPatches_(
22 base: any,
23 replacement: any,
24 patches: Patch[],
25 inversePatches: Patch[]
26 ): void
27 applyPatches_<T>(draft: T, patches: readonly Patch[]): T
28 }
29 MapSet?: {
30 proxyMap_<T extends AnyMap>(target: T, parent?: ImmerState): T
31 proxySet_<T extends AnySet>(target: T, parent?: ImmerState): T
32 }
33} = {}
34
35type Plugins = typeof plugins
36
37export function getPlugin<K extends keyof Plugins>(
38 pluginKey: K
39): Exclude<Plugins[K], undefined> {
40 const plugin = plugins[pluginKey]
41 if (!plugin) {
42 die(0, pluginKey)
43 }
44 // @ts-ignore
45 return plugin
46}
47
48export function loadPlugin<K extends keyof Plugins>(
49 pluginKey: K,
50 implementation: Plugins[K]
51): void {
52 if (!plugins[pluginKey]) plugins[pluginKey] = implementation
53}
54/** Map / Set plugin */
55
56export interface MapState extends ImmerBaseState {
57 type_: ArchType.Map
58 copy_: AnyMap | undefined
59 assigned_: Map<any, boolean> | undefined
60 base_: AnyMap
61 revoked_: boolean
62 draft_: Drafted<AnyMap, MapState>
63}
64
65export interface SetState extends ImmerBaseState {
66 type_: ArchType.Set
67 copy_: AnySet | undefined
68 base_: AnySet
69 drafts_: Map<any, Drafted> // maps the original value to the draft value in the new set
70 revoked_: boolean
71 draft_: Drafted<AnySet, SetState>
72}
73
74/** Patches plugin */
75
76export type PatchPath = (string | number)[]