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