Skip to content
Myr

plugins.ts

This file

Path

src/plugins.ts

196 lines · 3.9 KiB · TypeScript

Last changed by

More WIPMichel Weststratebefore adoptionee1c977

History of this file

Take it

separating plugins from coreMichel Weststratebefore adoption124518b

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

Removed some couplingMichel Weststratebefore adoption70e77aa

lines 11–30why, one step back
11 AnySet,
12 get,13 each,14 has,15 die,16 getArchtype,17 ProxyType,18 Archtype,19 isSet,20 isMap,21 isDraft,22 isDraftable,23 isEnumerable,24 shallowCopy,25 latest,26 createHiddenProperty,27 assertUnrevoked,28 is,29 createProxy,30 iteratorSymbol

separating plugins from coreMichel Weststratebefore adoption124518b

lines 31–51why, one step back
31} from "./internal"
3233/** Plugin utilities */34const plugins: {35 patches?: {36 generatePatches: typeof generatePatches37 applyPatches: typeof applyPatches38 }39 es5?: {40 willFinalizeES5: typeof willFinalizeES541 createES5Proxy: typeof createES5Proxy42 markChangedES5: typeof markChangedES543 }44 mapset?: {45 proxyMap: typeof proxyMap46 proxySet: typeof proxySet47 }48} = {}4950type Plugins = typeof plugins51

More WIPMichel Weststratebefore adoptionee1c977

lines 52–53why, one step back
52export type Utilities = ReturnType<typeof buildUtilities>
53

separating plugins from coreMichel Weststratebefore adoption124518b

lines 54–66why, one step back
54export function getPlugin<K extends keyof Plugins>(
55 pluginKey: K56): Exclude<Plugins[K], undefined> {57 const plugin = plugins[pluginKey]58 if (!plugin) {59 throw new Error(60 `The plugin ${pluginKey} has not been loaded into Immer. Make sure the require from "immer/${pluginKey}" when initializing your application, just after requiring immer itself.`61 )62 }63 // @ts-ignore64 return plugin65}66

Removed some couplingMichel Weststratebefore adoption70e77aa

lines 67–92why, one step back
67function buildUtilities() {
68 return {69 get,70 each,71 has,72 die,73 getArchtype,74 ProxyType,75 Archtype,76 isSet,77 isMap,78 isDraft,79 isDraftable,80 isEnumerable,81 shallowCopy,82 latest,83 createHiddenProperty,84 ImmerScope,85 DRAFT_STATE,86 assertUnrevoked,87 is,88 iteratorSymbol,89 createProxy90 } as const91}92

More WIPMichel Weststratebefore adoptionee1c977

line 93why, one step back
93let utilities: Utilities | undefined = undefined

Removed some couplingMichel Weststratebefore adoption70e77aa

lines 94–95why, one step back
94
95export function __loadPlugin<K extends keyof Plugins>(

separating plugins from coreMichel Weststratebefore adoption124518b

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

More WIPMichel Weststratebefore adoptionee1c977

line 97why, one step back
97 getImplementation: (core: any /* TODO: Utilities */) => Plugins[K]

Removed some couplingMichel Weststratebefore adoption70e77aa

lines 98–102why, one step back
98): void {
99 if (!utilities) {100 utilities = buildUtilities()101 }102 plugins[pluginKey] = getImplementation(utilities)

separating plugins from coreMichel Weststratebefore adoption124518b

lines 103–196why, one step back
103}
104105/** ES5 Plugin */106107interface ES5BaseState extends ImmerBaseState {108 finalizing: boolean109 assigned: {[key: string]: any}110 parent?: ImmerState111 revoked: boolean112}113114export interface ES5ObjectState extends ES5BaseState {115 type: ProxyType.ES5Object116 draft: Drafted<AnyObject, ES5ObjectState>117 base: AnyObject118 copy: AnyObject | null119}120121export interface ES5ArrayState extends ES5BaseState {122 type: ProxyType.ES5Array123 draft: Drafted<AnyObject, ES5ArrayState>124 base: AnyArray125 copy: AnyArray | null126}127128export function willFinalizeES5(129 scope: ImmerScope,130 result: any,131 isReplaced: boolean132) {133 getPlugin("es5").willFinalizeES5(scope, result, isReplaced)134}135136export function createES5Proxy<T>(137 base: T,138 parent?: ImmerState139): Drafted<T, ES5ObjectState | ES5ArrayState> {140 return getPlugin("es5").createES5Proxy(base, parent)141}142143export function markChangedES5(state: ImmerState) {144 getPlugin("es5").markChangedES5(state)145}146147/** Map / Set plugin */148149export interface MapState extends ImmerBaseState {150 type: ProxyType.Map151 copy: AnyMap | undefined152 assigned: Map<any, boolean> | undefined153 base: AnyMap154 revoked: boolean155 draft: Drafted<AnyMap, MapState>156}157158export interface SetState extends ImmerBaseState {159 type: ProxyType.Set160 copy: AnySet | undefined161 base: AnySet162 drafts: Map<any, Drafted> // maps the original value to the draft value in the new set163 revoked: boolean164 draft: Drafted<AnySet, SetState>165}166167export function proxyMap<T extends AnyMap>(168 target: T,169 parent?: ImmerState170): T & {[DRAFT_STATE]: MapState} {171 return getPlugin("mapset").proxyMap(target, parent)172}173174export function proxySet<T extends AnySet>(175 target: T,176 parent?: ImmerState177): T & {[DRAFT_STATE]: SetState} {178 return getPlugin("mapset").proxySet(target, parent)179}180181/** Patches plugin */182183export type PatchPath = (string | number)[]184185export function generatePatches(186 state: ImmerState,187 basePath: PatchPath,188 patches: Patch[],189 inversePatches: Patch[]190): void {191 getPlugin("patches").generatePatches(state, basePath, patches, inversePatches)192}193194export function applyPatches<T>(draft: T, patches: Patch[]): T {195 return getPlugin("patches").applyPatches(draft, patches)196}