immerClass.ts
This file
Path
src/immerClass.ts
233 lines · 6.5 KiB · TypeScript
Last changed by
Errors for prodMichel Weststratebefore adoption67565dd
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 }85Errors for prodMichel Weststratebefore adoption67565dd
lines 86–88why, one step back86 if (typeof recipe !== "function") die(6)87 if (patchListener !== undefined && typeof patchListener !== "function")88 die(7)More WIPMichel Weststratebefore adoptionee1c977
lines 89–93why, one step back8990 let result9192 // Only plain objects, arrays, and "immerable classes" are drafted.93 if (isDraftable(base)) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 94why, one step back94 const scope = ImmerScope.enter_(this)More WIPMichel Weststratebefore adoptionee1c977
lines 95–101why, one step back95 const proxy = createProxy(this, base, undefined)96 let hasError = true97 try {98 result = recipe(proxy)99 hasError = false100 } finally {101 // finally instead of catch + rethrow better preserves original stackMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 102–103why, one step back102 if (hasError) scope.revoke_()103 else scope.leave_()More WIPMichel Weststratebefore adoptionee1c977
lines 104–107why, one step back104 }105 if (typeof Promise !== "undefined" && result instanceof Promise) {106 return result.then(107 result => {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 108why, one step back108 scope.usePatches_(patchListener)Small improvementsMichel Weststratebefore adoption712af5c
line 109why, one step back109 return processResult(result, scope)More WIPMichel Weststratebefore adoptionee1c977
lines 110–111why, one step back110 },111 error => {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 112why, one step back112 scope.revoke_()More WIPMichel Weststratebefore adoptionee1c977
lines 113–116why, one step back113 throw error114 }115 )116 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 117why, one step back117 scope.usePatches_(patchListener)Small improvementsMichel Weststratebefore adoption712af5c
line 118why, one step back118 return processResult(result, scope)More WIPMichel Weststratebefore adoptionee1c977
lines 119–122why, one step back119 } else {120 result = recipe(base)121 if (result === NOTHING) return undefined122 if (result === undefined) result = baseCleanup finalize, -57Michel Weststratebefore adoption530d81b
line 123why, one step back123 if (this.autoFreeze_) freeze(result, true)More WIPMichel Weststratebefore adoptionee1c977
lines 124–132why, one step back124 return result125 }126 }127128 produceWithPatches(arg1: any, arg2?: any, arg3?: any): any {129 if (typeof arg1 === "function") {130 return (state: any, ...args: any[]) =>131 this.produceWithPatches(state, (draft: any) => arg1(draft, ...args))132 }Preserve error codes in productionMichel Weststratebefore adoptiond005fab
line 133why, one step back133More WIPMichel Weststratebefore adoptionee1c977
lines 134–142why, one step back134 let patches: Patch[], inversePatches: Patch[]135 const nextState = this.produce(arg1, arg2, (p: Patch[], ip: Patch[]) => {136 patches = p137 inversePatches = ip138 })139 return [nextState, patches!, inversePatches!]140 }141142 createDraft<T extends Objectish>(base: T): Draft<T> {Preserve error codes in productionMichel Weststratebefore adoptiond005fab
line 143why, one step back143 if (!isDraftable(base)) die(8)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 144why, one step back144 const scope = ImmerScope.enter_(this)More WIPMichel Weststratebefore adoptionee1c977
line 145why, one step back145 const proxy = createProxy(this, base, undefined)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 146–147why, one step back146 proxy[DRAFT_STATE].isManual_ = true147 scope.leave_()More WIPMichel Weststratebefore adoptionee1c977
lines 148–155why, one step back148 return proxy as any149 }150151 finishDraft<D extends Draft<any>>(152 draft: D,153 patchListener?: PatchListener154 ): D extends Draft<infer T> ? T : never {155 const state: ImmerState = draft && draft[DRAFT_STATE]Preserve error codes in productionMichel Weststratebefore adoptiond005fab
lines 156–159why, one step back156 if (__DEV__) {157 if (!state || !state.isManual_) die(9)158 if (state.finalized_) die(10)159 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 160–161why, one step back160 const {scope_: scope} = state161 scope.usePatches_(patchListener)Small improvementsMichel Weststratebefore adoption712af5c
line 162why, one step back162 return processResult(undefined, scope)More WIPMichel Weststratebefore adoptionee1c977
lines 163–170why, one step back163 }164165 /**166 * Pass true to automatically freeze all copies created by Immer.167 *168 * By default, auto-freezing is disabled in production.169 */170 setAutoFreeze(value: boolean) {killed hooks, as they leaked mangled properties. Also -145 bytesMichel Weststratebefore adoptionf1918cb
line 171why, one step back171 this.autoFreeze_ = valueMore WIPMichel Weststratebefore adoptionee1c977
lines 172–180why, one step back172 }173174 /**175 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is176 * always faster than using ES5 proxies.177 *178 * By default, feature detection is used, so calling this is rarely necessary.179 */180 setUseProxies(value: boolean) {killed hooks, as they leaked mangled properties. Also -145 bytesMichel Weststratebefore adoptionf1918cb
line 181why, one step back181 this.useProxies_ = valueMore WIPMichel Weststratebefore adoptionee1c977
lines 182–195why, one step back182 }183184 applyPatches(base: Objectish, patches: Patch[]) {185 // If a patch replaces the entire state, take that replacement as base186 // before applying patches187 let i: number188 for (i = patches.length - 1; i >= 0; i--) {189 const patch = patches[i]190 if (patch.path.length === 0 && patch.op === "replace") {191 base = patch.value192 break193 }194 }195scramble plugin names, -56Michel Weststratebefore adoption037b474
line 196why, one step back196 const applyPatchesImpl = getPlugin("patches").applyPatches_More WIPMichel Weststratebefore adoptionee1c977
lines 197–198why, one step back197 if (isDraft(base)) {198 // N.B: never hits if some patch a replacement, patches are never draftsscramble plugin names, -56Michel Weststratebefore adoption037b474
line 199why, one step back199 return applyPatchesImpl(base, patches)More WIPMichel Weststratebefore adoptionee1c977
lines 200–202why, one step back200 }201 // Otherwise, produce a copy of the base state.202 return this.produce(base, (draft: Drafted) =>scramble plugin names, -56Michel Weststratebefore adoption037b474
line 203why, one step back203 applyPatchesImpl(draft, patches.slice(i + 1))More WIPMichel Weststratebefore adoptionee1c977
lines 204–214why, one step back204 )205 }206}207208export function createProxy<T extends Objectish>(209 immer: Immer,210 value: T,211 parent?: ImmerState212): Drafted<T, ImmerState> {213 // precondition: createProxy should be guarded by isDraftable, so we know we can safely draft214 const draft: Drafted = isMap(value)scramble plugin names, -56Michel Weststratebefore adoption037b474
line 215why, one step back215 ? getPlugin("mapset").proxyMap_(value, parent)More WIPMichel Weststratebefore adoptionee1c977
line 216why, one step back216 : isSet(value)scramble plugin names, -56Michel Weststratebefore adoption037b474
line 217why, one step back217 ? getPlugin("mapset").proxySet_(value, parent)killed hooks, as they leaked mangled properties. Also -145 bytesMichel Weststratebefore adoptionf1918cb
line 218why, one step back218 : immer.useProxies_More WIPMichel Weststratebefore adoptionee1c977
line 219why, one step back219 ? createProxyProxy(value, parent)scramble plugin names, -56Michel Weststratebefore adoption037b474
line 220why, one step back220 : getPlugin("es5").createES5Proxy_(value, parent)More WIPMichel Weststratebefore adoptionee1c977
line 221why, one step back221Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 222–223why, one step back222 const scope = parent ? parent.scope_ : ImmerScope.current_!223 scope.drafts_.push(draft)More WIPMichel Weststratebefore adoptionee1c977
lines 224–227why, one step back224 return draft225}226227export function markChanged(immer: Immer, state: ImmerState) {killed hooks, as they leaked mangled properties. Also -145 bytesMichel Weststratebefore adoptionf1918cb
line 228why, one step back228 if (immer.useProxies_) {More WIPMichel Weststratebefore adoptionee1c977
lines 229–230why, one step back229 markChangedProxy(state)230 } else {scramble plugin names, -56Michel Weststratebefore adoption037b474
line 231why, one step back231 getPlugin("es5").markChangedES5_(state)More WIPMichel Weststratebefore adoptionee1c977
lines 232–233why, one step back232 }233}