Skip to content
Myr

immerClass.ts

This file

Path

src/immerClass.ts

251 lines · 7.1 KiB · TypeScript

Last changed by

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

History of this file

Take it

More WIPMichel Weststratebefore adoptionee1c977

lines 1–26why, one step back
1import {
2 createES5Proxy,3 willFinalizeES5,4 markChangedES5,5 IProduceWithPatches,6 IProduce,7 ImmerState,8 Drafted,9 isDraftable,10 ImmerScope,11 processResult,12 NOTHING,13 Patch,14 Objectish,15 DRAFT_STATE,16 Draft,17 PatchListener,18 isDraft,19 applyPatches,20 isMap,21 proxyMap,22 isSet,23 proxySet,24 markChangedProxy,25 createProxyProxy26} from "./internal"

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

line 27why, one step back
27import invariant from "tiny-invariant"

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

line 28why, one step back
28import {freeze} from "./common"

More WIPMichel Weststratebefore adoptionee1c977

line 29why, one step back
29

New build set. Current statsMichel Weststratebefore adoption9aec0fe

line 30why, one step back
30declare const __DEV__: boolean

More WIPMichel Weststratebefore adoptionee1c977

lines 31–39why, one step back
31/* 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 back
40 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 back
47

killed hooks, as they leaked mangled properties. Also -145 bytesMichel Weststratebefore adoptionf1918cb

lines 48–52why, one step back
48 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 back
53 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 }91

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

lines 92–99why, one step back
92 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 back
100
101 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 back
105 const scope = ImmerScope.enter_(this)

More WIPMichel Weststratebefore adoptionee1c977

lines 106–112why, one step back
106 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 stack

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 113–114why, one step back
113 if (hasError) scope.revoke_()
114 else scope.leave_()

More WIPMichel Weststratebefore adoptionee1c977

lines 115–118why, one step back
115 }
116 if (typeof Promise !== "undefined" && result instanceof Promise) {117 return result.then(118 result => {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 119why, one step back
119 scope.usePatches_(patchListener)

Small improvementsMichel Weststratebefore adoption712af5c

line 120why, one step back
120 return processResult(result, scope)

More WIPMichel Weststratebefore adoptionee1c977

lines 121–122why, one step back
121 },
122 error => {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 123why, one step back
123 scope.revoke_()

More WIPMichel Weststratebefore adoptionee1c977

lines 124–127why, one step back
124 throw error
125 }126 )127 }

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 128why, one step back
128 scope.usePatches_(patchListener)

Small improvementsMichel Weststratebefore adoption712af5c

line 129why, one step back
129 return processResult(result, scope)

More WIPMichel Weststratebefore adoptionee1c977

lines 130–133why, one step back
130 } else {
131 result = recipe(base)132 if (result === NOTHING) return undefined133 if (result === undefined) result = base

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

line 134why, one step back
134 if (this.autoFreeze_) freeze(result, true)

More WIPMichel Weststratebefore adoptionee1c977

lines 135–145why, one step back
135 return result
136 }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 back
146 invariant(!arg3)

More WIPMichel Weststratebefore adoptionee1c977

lines 147–155why, one step back
147 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 back
156 invariant(isDraftable(base), "First argument to `createDraft` must be a plain object, an array, or an immerable object") // prettier-ignore

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 157why, one step back
157 const scope = ImmerScope.enter_(this)

More WIPMichel Weststratebefore adoptionee1c977

line 158why, one step back
158 const proxy = createProxy(this, base, undefined)

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 159–160why, one step back
159 proxy[DRAFT_STATE].isManual_ = true
160 scope.leave_()

More WIPMichel Weststratebefore adoptionee1c977

lines 161–168why, one step back
161 return proxy as any
162 }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 back
169 invariant(state && state.isManual_, "First argument to `finishDraft` must be a draft returned by `createDraft`") // prettier-ignore
170 invariant(!state.finalized_, "The given draft is already finalized") // prettier-ignore171 const {scope_: scope} = state172 scope.usePatches_(patchListener)

Small improvementsMichel Weststratebefore adoption712af5c

line 173why, one step back
173 return processResult(undefined, scope)

More WIPMichel Weststratebefore adoptionee1c977

lines 174–181why, one step back
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 back
182 this.autoFreeze_ = value

More WIPMichel Weststratebefore adoptionee1c977

lines 183–191why, one step back
183 }
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 back
192 this.useProxies_ = value

More WIPMichel Weststratebefore adoptionee1c977

lines 193–227why, one step back
193 }
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 back
228 : immer.useProxies_

More WIPMichel Weststratebefore adoptionee1c977

lines 229–231why, one step back
229 ? createProxyProxy(value, parent)
230 : createES5Proxy(value, parent)231

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 232–233why, one step back
232 const scope = parent ? parent.scope_ : ImmerScope.current_!
233 scope.drafts_.push(draft)

More WIPMichel Weststratebefore adoptionee1c977

lines 234–241why, one step back
234 return draft
235}236237export function willFinalize(238 scope: ImmerScope,239 thing: any,240 isReplaced: boolean241) {

Small improvementsMichel Weststratebefore adoption712af5c

line 242why, one step back
242 if (!scope.immer_.useProxies_) willFinalizeES5(scope, thing, isReplaced)

More WIPMichel Weststratebefore adoptionee1c977

lines 243–245why, one step back
243}
244245export function markChanged(immer: Immer, state: ImmerState) {

killed hooks, as they leaked mangled properties. Also -145 bytesMichel Weststratebefore adoptionf1918cb

line 246why, one step back
246 if (immer.useProxies_) {

More WIPMichel Weststratebefore adoptionee1c977

lines 247–251why, one step back
247 markChangedProxy(state)
248 } else {249 markChangedES5(state)250 }251}