immerClass.ts
This file
Path
src/immerClass.ts
252 lines · 7.1 KiB · TypeScript
Last changed by
killed hooks, as they leaked mangled properties. Also -145 bytesMichel Weststratebefore adoptionf1918cb
History of this fileTake it
More WIPMichel Weststratebefore adoptionee1c977
lines 1–27why, one step back1import {2 createES5Proxy,3 willFinalizeES5,4 markChangedES5,5 IProduceWithPatches,6 IProduce,7 ImmerState,8 Drafted,9 isDraftable,10 ImmerScope,11 processResult,12 NOTHING,13 maybeFreeze,14 Patch,15 Objectish,16 DRAFT_STATE,17 Draft,18 PatchListener,19 isDraft,20 applyPatches,21 isMap,22 proxyMap,23 isSet,24 proxySet,25 markChangedProxy,26 createProxyProxy27} from "./internal"use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 28why, one step back28import invariant from "tiny-invariant"More WIPMichel Weststratebefore adoptionee1c977
line 29why, one step back29New build set. Current statsMichel Weststratebefore adoption9aec0fe
line 30why, one step back30declare const __DEV__: booleanMore WIPMichel Weststratebefore adoptionee1c977
lines 31–39why, one step back31/* istanbul ignore next */32function verifyMinified() {}3334interface ProducersFns {35 produce: IProduce36 produceWithPatches: IProduceWithPatches37}3839export class Immer implements ProducersFns {killed hooks, as they leaked mangled properties. Also -145 bytesMichel Weststratebefore adoptionf1918cb
lines 40–46why, one step back40 useProxies_: boolean =41 typeof Proxy !== "undefined" &&42 typeof Proxy.revocable !== "undefined" &&43 typeof Reflect !== "undefined"44 autoFreeze_: boolean = __DEV__45 ? false /* istanbul ignore next */46 : verifyMinified.name === "verifyMinified"More WIPMichel Weststratebefore adoptionee1c977
line 47why, one step back47killed hooks, as they leaked mangled properties. Also -145 bytesMichel Weststratebefore adoptionf1918cb
lines 48–52why, one step back48 constructor(config?: {useProxies?: boolean; autoFreeze?: boolean}) {49 if (typeof config?.useProxies === "boolean")50 this.setUseProxies(config!.useProxies)51 if (typeof config?.autoFreeze === "boolean")52 this.setAutoFreeze(config!.autoFreeze)More WIPMichel Weststratebefore adoptionee1c977
lines 53–91why, one step back53 this.produce = this.produce.bind(this)54 this.produceWithPatches = this.produceWithPatches.bind(this)55 }5657 /**58 * The `produce` function takes a value and a "recipe function" (whose59 * return value often depends on the base state). The recipe function is60 * free to mutate its first argument however it wants. All mutations are61 * only ever applied to a __copy__ of the base state.62 *63 * Pass only a function to create a "curried producer" which relieves you64 * from passing the recipe function every time.65 *66 * Only plain objects and arrays are made mutable. All other objects are67 * considered uncopyable.68 *69 * Note: This function is __bound__ to its `Immer` instance.70 *71 * @param {any} base - the initial state72 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified73 * @param {Function} patchListener - optional function that will be called with all the patches produced here74 * @returns {any} a new state, or the initial state if nothing was modified75 */76 produce(base: any, recipe?: any, patchListener?: any) {77 // curried invocation78 if (typeof base === "function" && typeof recipe !== "function") {79 const defaultBase = recipe80 recipe = base8182 const self = this83 return function curriedProduce(84 this: any,85 base = defaultBase,86 ...args: any[]87 ) {88 return self.produce(base, (draft: Drafted) => recipe.call(this, draft, ...args)) // prettier-ignore89 }90 }91use invariant -321 bytesMichel Weststratebefore adoptionac57da5
lines 92–99why, one step back92 invariant(93 typeof recipe === "function",94 "The first or second argument to `produce` must be a function"95 )96 invariant(97 patchListener === undefined || typeof patchListener === "function",98 "The third argument to `produce` must be a function or undefined"99 )More WIPMichel Weststratebefore adoptionee1c977
lines 100–104why, one step back100101 let result102103 // Only plain objects, arrays, and "immerable classes" are drafted.104 if (isDraftable(base)) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 105why, one step back105 const scope = ImmerScope.enter_(this)More WIPMichel Weststratebefore adoptionee1c977
lines 106–112why, one step back106 const proxy = createProxy(this, base, undefined)107 let hasError = true108 try {109 result = recipe(proxy)110 hasError = false111 } finally {112 // finally instead of catch + rethrow better preserves original stackMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 113–114why, one step back113 if (hasError) scope.revoke_()114 else scope.leave_()More WIPMichel Weststratebefore adoptionee1c977
lines 115–118why, one step back115 }116 if (typeof Promise !== "undefined" && result instanceof Promise) {117 return result.then(118 result => {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 119why, one step back119 scope.usePatches_(patchListener)More WIPMichel Weststratebefore adoptionee1c977
lines 120–122why, one step back120 return processResult(this, result, scope)121 },122 error => {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 123why, one step back123 scope.revoke_()More WIPMichel Weststratebefore adoptionee1c977
lines 124–127why, one step back124 throw error125 }126 )127 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 128why, one step back128 scope.usePatches_(patchListener)More WIPMichel Weststratebefore adoptionee1c977
lines 129–145why, one step back129 return processResult(this, result, scope)130 } else {131 result = recipe(base)132 if (result === NOTHING) return undefined133 if (result === undefined) result = base134 maybeFreeze(this, result, true)135 return result136 }137 }138139 produceWithPatches(arg1: any, arg2?: any, arg3?: any): any {140 if (typeof arg1 === "function") {141 return (state: any, ...args: any[]) =>142 this.produceWithPatches(state, (draft: any) => arg1(draft, ...args))143 }144 // non-curried form145 /* istanbul ignore next */Killed `die`, -14 bytesMichel Weststratebefore adoptiond77a5c1
line 146why, one step back146 invariant(!arg3)More WIPMichel Weststratebefore adoptionee1c977
lines 147–155why, one step back147 let patches: Patch[], inversePatches: Patch[]148 const nextState = this.produce(arg1, arg2, (p: Patch[], ip: Patch[]) => {149 patches = p150 inversePatches = ip151 })152 return [nextState, patches!, inversePatches!]153 }154155 createDraft<T extends Objectish>(base: T): Draft<T> {use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 156why, one step back156 invariant(isDraftable(base), "First argument to `createDraft` must be a plain object, an array, or an immerable object") // prettier-ignoreMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 157why, one step back157 const scope = ImmerScope.enter_(this)More WIPMichel Weststratebefore adoptionee1c977
line 158why, one step back158 const proxy = createProxy(this, base, undefined)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 159–160why, one step back159 proxy[DRAFT_STATE].isManual_ = true160 scope.leave_()More WIPMichel Weststratebefore adoptionee1c977
lines 161–168why, one step back161 return proxy as any162 }163164 finishDraft<D extends Draft<any>>(165 draft: D,166 patchListener?: PatchListener167 ): D extends Draft<infer T> ? T : never {168 const state: ImmerState = draft && draft[DRAFT_STATE]Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 169–172why, one step back169 invariant(state && state.isManual_, "First argument to `finishDraft` must be a draft returned by `createDraft`") // prettier-ignore170 invariant(!state.finalized_, "The given draft is already finalized") // prettier-ignore171 const {scope_: scope} = state172 scope.usePatches_(patchListener)More WIPMichel Weststratebefore adoptionee1c977
lines 173–181why, one step back173 return processResult(this, undefined, scope)174 }175176 /**177 * Pass true to automatically freeze all copies created by Immer.178 *179 * By default, auto-freezing is disabled in production.180 */181 setAutoFreeze(value: boolean) {killed hooks, as they leaked mangled properties. Also -145 bytesMichel Weststratebefore adoptionf1918cb
line 182why, one step back182 this.autoFreeze_ = valueMore WIPMichel Weststratebefore adoptionee1c977
lines 183–191why, one step back183 }184185 /**186 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is187 * always faster than using ES5 proxies.188 *189 * By default, feature detection is used, so calling this is rarely necessary.190 */191 setUseProxies(value: boolean) {killed hooks, as they leaked mangled properties. Also -145 bytesMichel Weststratebefore adoptionf1918cb
line 192why, one step back192 this.useProxies_ = valueMore WIPMichel Weststratebefore adoptionee1c977
lines 193–227why, one step back193 }194195 applyPatches(base: Objectish, patches: Patch[]) {196 // If a patch replaces the entire state, take that replacement as base197 // before applying patches198 let i: number199 for (i = patches.length - 1; i >= 0; i--) {200 const patch = patches[i]201 if (patch.path.length === 0 && patch.op === "replace") {202 base = patch.value203 break204 }205 }206207 if (isDraft(base)) {208 // N.B: never hits if some patch a replacement, patches are never drafts209 return applyPatches(base, patches)210 }211 // Otherwise, produce a copy of the base state.212 return this.produce(base, (draft: Drafted) =>213 applyPatches(draft, patches.slice(i + 1))214 )215 }216}217218export function createProxy<T extends Objectish>(219 immer: Immer,220 value: T,221 parent?: ImmerState222): Drafted<T, ImmerState> {223 // precondition: createProxy should be guarded by isDraftable, so we know we can safely draft224 const draft: Drafted = isMap(value)225 ? proxyMap(value, parent)226 : isSet(value)227 ? proxySet(value, parent)killed hooks, as they leaked mangled properties. Also -145 bytesMichel Weststratebefore adoptionf1918cb
line 228why, one step back228 : immer.useProxies_More WIPMichel Weststratebefore adoptionee1c977
lines 229–231why, one step back229 ? createProxyProxy(value, parent)230 : createES5Proxy(value, parent)231Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 232–233why, one step back232 const scope = parent ? parent.scope_ : ImmerScope.current_!233 scope.drafts_.push(draft)More WIPMichel Weststratebefore adoptionee1c977
lines 234–242why, one step back234 return draft235}236237export function willFinalize(238 immer: Immer,239 scope: ImmerScope,240 thing: any,241 isReplaced: boolean242) {killed hooks, as they leaked mangled properties. Also -145 bytesMichel Weststratebefore adoptionf1918cb
line 243why, one step back243 if (!immer.useProxies_) willFinalizeES5(scope, thing, isReplaced)More WIPMichel Weststratebefore adoptionee1c977
lines 244–246why, one step back244}245246export function markChanged(immer: Immer, state: ImmerState) {killed hooks, as they leaked mangled properties. Also -145 bytesMichel Weststratebefore adoptionf1918cb
line 247why, one step back247 if (immer.useProxies_) {More WIPMichel Weststratebefore adoptionee1c977
lines 248–252why, one step back248 markChangedProxy(state)249 } else {250 markChangedES5(state)251 }252}