immerClass.ts
This file
Path
src/immerClass.ts
235 lines · 6.5 KiB · TypeScript
Last changed by
Preserve error codes in productionMichel Weststratebefore adoptiond005fab
History of this fileTake it
More WIPMichel Weststratebefore adoptionee1c977
lines 1–18why, one step back1import {2 IProduceWithPatches,3 IProduce,4 ImmerState,5 Drafted,6 isDraftable,7 ImmerScope,8 processResult,9 NOTHING,10 Patch,11 Objectish,12 DRAFT_STATE,13 Draft,14 PatchListener,15 isDraft,16 isMap,17 isSet,18 markChangedProxy,Preserve error codes in productionMichel Weststratebefore adoptiond005fab
lines 19–22why, one step back19 createProxyProxy,20 freeze,21 getPlugin,22 dieMore WIPMichel Weststratebefore adoptionee1c977
lines 23–33why, one step back23} from "./internal"2425/* istanbul ignore next */26function verifyMinified() {}2728interface ProducersFns {29 produce: IProduce30 produceWithPatches: IProduceWithPatches31}3233export class Immer implements ProducersFns {killed hooks, as they leaked mangled properties. Also -145 bytesMichel Weststratebefore adoptionf1918cb
lines 34–40why, one step back34 useProxies_: boolean =35 typeof Proxy !== "undefined" &&36 typeof Proxy.revocable !== "undefined" &&37 typeof Reflect !== "undefined"38 autoFreeze_: boolean = __DEV__39 ? false /* istanbul ignore next */40 : verifyMinified.name === "verifyMinified"More WIPMichel Weststratebefore adoptionee1c977
line 41why, one step back41killed hooks, as they leaked mangled properties. Also -145 bytesMichel Weststratebefore adoptionf1918cb
lines 42–46why, one step back42 constructor(config?: {useProxies?: boolean; autoFreeze?: boolean}) {43 if (typeof config?.useProxies === "boolean")44 this.setUseProxies(config!.useProxies)45 if (typeof config?.autoFreeze === "boolean")46 this.setAutoFreeze(config!.autoFreeze)More WIPMichel Weststratebefore adoptionee1c977
lines 47–85why, one step back47 this.produce = this.produce.bind(this)48 this.produceWithPatches = this.produceWithPatches.bind(this)49 }5051 /**52 * The `produce` function takes a value and a "recipe function" (whose53 * return value often depends on the base state). The recipe function is54 * free to mutate its first argument however it wants. All mutations are55 * only ever applied to a __copy__ of the base state.56 *57 * Pass only a function to create a "curried producer" which relieves you58 * from passing the recipe function every time.59 *60 * Only plain objects and arrays are made mutable. All other objects are61 * considered uncopyable.62 *63 * Note: This function is __bound__ to its `Immer` instance.64 *65 * @param {any} base - the initial state66 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified67 * @param {Function} patchListener - optional function that will be called with all the patches produced here68 * @returns {any} a new state, or the initial state if nothing was modified69 */70 produce(base: any, recipe?: any, patchListener?: any) {71 // curried invocation72 if (typeof base === "function" && typeof recipe !== "function") {73 const defaultBase = recipe74 recipe = base7576 const self = this77 return function curriedProduce(78 this: any,79 base = defaultBase,80 ...args: any[]81 ) {82 return self.produce(base, (draft: Drafted) => recipe.call(this, draft, ...args)) // prettier-ignore83 }84 }85Preserve error codes in productionMichel Weststratebefore adoptiond005fab
lines 86–90why, one step back86 if (__DEV__) {87 if (typeof recipe !== "function") die(6)88 if (patchListener !== undefined && typeof patchListener !== "function")89 die(7)90 }More WIPMichel Weststratebefore adoptionee1c977
lines 91–95why, one step back9192 let result9394 // Only plain objects, arrays, and "immerable classes" are drafted.95 if (isDraftable(base)) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 96why, one step back96 const scope = ImmerScope.enter_(this)More WIPMichel Weststratebefore adoptionee1c977
lines 97–103why, one step back97 const proxy = createProxy(this, base, undefined)98 let hasError = true99 try {100 result = recipe(proxy)101 hasError = false102 } finally {103 // finally instead of catch + rethrow better preserves original stackMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 104–105why, one step back104 if (hasError) scope.revoke_()105 else scope.leave_()More WIPMichel Weststratebefore adoptionee1c977
lines 106–109why, one step back106 }107 if (typeof Promise !== "undefined" && result instanceof Promise) {108 return result.then(109 result => {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 110why, one step back110 scope.usePatches_(patchListener)Small improvementsMichel Weststratebefore adoption712af5c
line 111why, one step back111 return processResult(result, scope)More WIPMichel Weststratebefore adoptionee1c977
lines 112–113why, one step back112 },113 error => {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 114why, one step back114 scope.revoke_()More WIPMichel Weststratebefore adoptionee1c977
lines 115–118why, one step back115 throw error116 }117 )118 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 119why, one step back119 scope.usePatches_(patchListener)Small improvementsMichel Weststratebefore adoption712af5c
line 120why, one step back120 return processResult(result, scope)More WIPMichel Weststratebefore adoptionee1c977
lines 121–124why, one step back121 } else {122 result = recipe(base)123 if (result === NOTHING) return undefined124 if (result === undefined) result = baseCleanup finalize, -57Michel Weststratebefore adoption530d81b
line 125why, one step back125 if (this.autoFreeze_) freeze(result, true)More WIPMichel Weststratebefore adoptionee1c977
lines 126–134why, one step back126 return result127 }128 }129130 produceWithPatches(arg1: any, arg2?: any, arg3?: any): any {131 if (typeof arg1 === "function") {132 return (state: any, ...args: any[]) =>133 this.produceWithPatches(state, (draft: any) => arg1(draft, ...args))134 }Preserve error codes in productionMichel Weststratebefore adoptiond005fab
line 135why, one step back135More WIPMichel Weststratebefore adoptionee1c977
lines 136–144why, one step back136 let patches: Patch[], inversePatches: Patch[]137 const nextState = this.produce(arg1, arg2, (p: Patch[], ip: Patch[]) => {138 patches = p139 inversePatches = ip140 })141 return [nextState, patches!, inversePatches!]142 }143144 createDraft<T extends Objectish>(base: T): Draft<T> {Preserve error codes in productionMichel Weststratebefore adoptiond005fab
line 145why, one step back145 if (!isDraftable(base)) die(8)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 146why, one step back146 const scope = ImmerScope.enter_(this)More WIPMichel Weststratebefore adoptionee1c977
line 147why, one step back147 const proxy = createProxy(this, base, undefined)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 148–149why, one step back148 proxy[DRAFT_STATE].isManual_ = true149 scope.leave_()More WIPMichel Weststratebefore adoptionee1c977
lines 150–157why, one step back150 return proxy as any151 }152153 finishDraft<D extends Draft<any>>(154 draft: D,155 patchListener?: PatchListener156 ): D extends Draft<infer T> ? T : never {157 const state: ImmerState = draft && draft[DRAFT_STATE]Preserve error codes in productionMichel Weststratebefore adoptiond005fab
lines 158–161why, one step back158 if (__DEV__) {159 if (!state || !state.isManual_) die(9)160 if (state.finalized_) die(10)161 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 162–163why, one step back162 const {scope_: scope} = state163 scope.usePatches_(patchListener)Small improvementsMichel Weststratebefore adoption712af5c
line 164why, one step back164 return processResult(undefined, scope)More WIPMichel Weststratebefore adoptionee1c977
lines 165–172why, one step back165 }166167 /**168 * Pass true to automatically freeze all copies created by Immer.169 *170 * By default, auto-freezing is disabled in production.171 */172 setAutoFreeze(value: boolean) {killed hooks, as they leaked mangled properties. Also -145 bytesMichel Weststratebefore adoptionf1918cb
line 173why, one step back173 this.autoFreeze_ = valueMore WIPMichel Weststratebefore adoptionee1c977
lines 174–182why, one step back174 }175176 /**177 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is178 * always faster than using ES5 proxies.179 *180 * By default, feature detection is used, so calling this is rarely necessary.181 */182 setUseProxies(value: boolean) {killed hooks, as they leaked mangled properties. Also -145 bytesMichel Weststratebefore adoptionf1918cb
line 183why, one step back183 this.useProxies_ = valueMore WIPMichel Weststratebefore adoptionee1c977
lines 184–197why, one step back184 }185186 applyPatches(base: Objectish, patches: Patch[]) {187 // If a patch replaces the entire state, take that replacement as base188 // before applying patches189 let i: number190 for (i = patches.length - 1; i >= 0; i--) {191 const patch = patches[i]192 if (patch.path.length === 0 && patch.op === "replace") {193 base = patch.value194 break195 }196 }197scramble plugin names, -56Michel Weststratebefore adoption037b474
line 198why, one step back198 const applyPatchesImpl = getPlugin("patches").applyPatches_More WIPMichel Weststratebefore adoptionee1c977
lines 199–200why, one step back199 if (isDraft(base)) {200 // N.B: never hits if some patch a replacement, patches are never draftsscramble plugin names, -56Michel Weststratebefore adoption037b474
line 201why, one step back201 return applyPatchesImpl(base, patches)More WIPMichel Weststratebefore adoptionee1c977
lines 202–204why, one step back202 }203 // Otherwise, produce a copy of the base state.204 return this.produce(base, (draft: Drafted) =>scramble plugin names, -56Michel Weststratebefore adoption037b474
line 205why, one step back205 applyPatchesImpl(draft, patches.slice(i + 1))More WIPMichel Weststratebefore adoptionee1c977
lines 206–216why, one step back206 )207 }208}209210export function createProxy<T extends Objectish>(211 immer: Immer,212 value: T,213 parent?: ImmerState214): Drafted<T, ImmerState> {215 // precondition: createProxy should be guarded by isDraftable, so we know we can safely draft216 const draft: Drafted = isMap(value)scramble plugin names, -56Michel Weststratebefore adoption037b474
line 217why, one step back217 ? getPlugin("mapset").proxyMap_(value, parent)More WIPMichel Weststratebefore adoptionee1c977
line 218why, one step back218 : isSet(value)scramble plugin names, -56Michel Weststratebefore adoption037b474
line 219why, one step back219 ? getPlugin("mapset").proxySet_(value, parent)killed hooks, as they leaked mangled properties. Also -145 bytesMichel Weststratebefore adoptionf1918cb
line 220why, one step back220 : immer.useProxies_More WIPMichel Weststratebefore adoptionee1c977
line 221why, one step back221 ? createProxyProxy(value, parent)scramble plugin names, -56Michel Weststratebefore adoption037b474
line 222why, one step back222 : getPlugin("es5").createES5Proxy_(value, parent)More WIPMichel Weststratebefore adoptionee1c977
line 223why, one step back223Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 224–225why, one step back224 const scope = parent ? parent.scope_ : ImmerScope.current_!225 scope.drafts_.push(draft)More WIPMichel Weststratebefore adoptionee1c977
lines 226–229why, one step back226 return draft227}228229export function markChanged(immer: Immer, state: ImmerState) {killed hooks, as they leaked mangled properties. Also -145 bytesMichel Weststratebefore adoptionf1918cb
line 230why, one step back230 if (immer.useProxies_) {More WIPMichel Weststratebefore adoptionee1c977
lines 231–232why, one step back231 markChangedProxy(state)232 } else {scramble plugin names, -56Michel Weststratebefore adoption037b474
line 233why, one step back233 getPlugin("es5").markChangedES5_(state)More WIPMichel Weststratebefore adoptionee1c977
lines 234–235why, one step back234 }235}