Skip to content
Myr

plugins.ts

This file

Path

src/plugins.ts

194 lines · 3.8 KiB · TypeScript

Last changed by

Removed some couplingMichel Weststratebefore adoption70e77aa

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–64why, 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 plugins5152export function getPlugin<K extends keyof Plugins>(53 pluginKey: K54): Exclude<Plugins[K], undefined> {55 const plugin = plugins[pluginKey]56 if (!plugin) {57 throw new Error(58 `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.`59 )60 }61 // @ts-ignore62 return plugin63}64

Removed some couplingMichel Weststratebefore adoption70e77aa

lines 65–93why, one step back
65function buildUtilities() {
66 return {67 get,68 each,69 has,70 die,71 getArchtype,72 ProxyType,73 Archtype,74 isSet,75 isMap,76 isDraft,77 isDraftable,78 isEnumerable,79 shallowCopy,80 latest,81 createHiddenProperty,82 ImmerScope,83 DRAFT_STATE,84 assertUnrevoked,85 is,86 iteratorSymbol,87 createProxy88 } as const89}9091let utilities: ReturnType<typeof buildUtilities> | undefined = undefined9293export function __loadPlugin<K extends keyof Plugins>(

separating plugins from coreMichel Weststratebefore adoption124518b

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

Removed some couplingMichel Weststratebefore adoption70e77aa

lines 95–100why, one step back
95 getImplementation: (core: typeof utilities) => Plugins[K]
96): void {97 if (!utilities) {98 utilities = buildUtilities()99 }100 plugins[pluginKey] = getImplementation(utilities)

separating plugins from coreMichel Weststratebefore adoption124518b

lines 101–194why, one step back
101}
102103/** ES5 Plugin */104105interface ES5BaseState extends ImmerBaseState {106 finalizing: boolean107 assigned: {[key: string]: any}108 parent?: ImmerState109 revoked: boolean110}111112export interface ES5ObjectState extends ES5BaseState {113 type: ProxyType.ES5Object114 draft: Drafted<AnyObject, ES5ObjectState>115 base: AnyObject116 copy: AnyObject | null117}118119export interface ES5ArrayState extends ES5BaseState {120 type: ProxyType.ES5Array121 draft: Drafted<AnyObject, ES5ArrayState>122 base: AnyArray123 copy: AnyArray | null124}125126export function willFinalizeES5(127 scope: ImmerScope,128 result: any,129 isReplaced: boolean130) {131 getPlugin("es5").willFinalizeES5(scope, result, isReplaced)132}133134export function createES5Proxy<T>(135 base: T,136 parent?: ImmerState137): Drafted<T, ES5ObjectState | ES5ArrayState> {138 return getPlugin("es5").createES5Proxy(base, parent)139}140141export function markChangedES5(state: ImmerState) {142 getPlugin("es5").markChangedES5(state)143}144145/** Map / Set plugin */146147export interface MapState extends ImmerBaseState {148 type: ProxyType.Map149 copy: AnyMap | undefined150 assigned: Map<any, boolean> | undefined151 base: AnyMap152 revoked: boolean153 draft: Drafted<AnyMap, MapState>154}155156export interface SetState extends ImmerBaseState {157 type: ProxyType.Set158 copy: AnySet | undefined159 base: AnySet160 drafts: Map<any, Drafted> // maps the original value to the draft value in the new set161 revoked: boolean162 draft: Drafted<AnySet, SetState>163}164165export function proxyMap<T extends AnyMap>(166 target: T,167 parent?: ImmerState168): T & {[DRAFT_STATE]: MapState} {169 return getPlugin("mapset").proxyMap(target, parent)170}171172export function proxySet<T extends AnySet>(173 target: T,174 parent?: ImmerState175): T & {[DRAFT_STATE]: SetState} {176 return getPlugin("mapset").proxySet(target, parent)177}178179/** Patches plugin */180181export type PatchPath = (string | number)[]182183export function generatePatches(184 state: ImmerState,185 basePath: PatchPath,186 patches: Patch[],187 inversePatches: Patch[]188): void {189 getPlugin("patches").generatePatches(state, basePath, patches, inversePatches)190}191192export function applyPatches<T>(draft: T, patches: Patch[]): T {193 return getPlugin("patches").applyPatches(draft, patches)194}