Skip to content
Myr

immerClass.ts

This file

Path

src/immerClass.ts

239 lines · 7.0 KiB · TypeScript

Last changed by

scramble plugin names, -56Michel Weststratebefore adoption037b474

History of this file

Take it

More WIPMichel Weststratebefore adoptionee1c977

lines 1–20why, one step back
1import {
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,19 createProxyProxy20} from "./internal"

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

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

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

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

scramble plugin names, -56Michel Weststratebefore adoption037b474

line 23why, one step back
23import {getPlugin} from "./plugins"

More WIPMichel Weststratebefore adoptionee1c977

line 24why, one step back
24

New build set. Current statsMichel Weststratebefore adoption9aec0fe

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

More WIPMichel Weststratebefore adoptionee1c977

lines 26–34why, one step back
26/* istanbul ignore next */
27function verifyMinified() {}2829interface ProducersFns {30 produce: IProduce31 produceWithPatches: IProduceWithPatches32}3334export class Immer implements ProducersFns {

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

lines 35–41why, one step back
35 useProxies_: boolean =
36 typeof Proxy !== "undefined" &&37 typeof Proxy.revocable !== "undefined" &&38 typeof Reflect !== "undefined"39 autoFreeze_: boolean = __DEV__40 ? false /* istanbul ignore next */41 : verifyMinified.name === "verifyMinified"

More WIPMichel Weststratebefore adoptionee1c977

line 42why, one step back
42

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

lines 43–47why, one step back
43 constructor(config?: {useProxies?: boolean; autoFreeze?: boolean}) {
44 if (typeof config?.useProxies === "boolean")45 this.setUseProxies(config!.useProxies)46 if (typeof config?.autoFreeze === "boolean")47 this.setAutoFreeze(config!.autoFreeze)

More WIPMichel Weststratebefore adoptionee1c977

lines 48–86why, one step back
48 this.produce = this.produce.bind(this)
49 this.produceWithPatches = this.produceWithPatches.bind(this)50 }5152 /**53 * The `produce` function takes a value and a "recipe function" (whose54 * return value often depends on the base state). The recipe function is55 * free to mutate its first argument however it wants. All mutations are56 * only ever applied to a __copy__ of the base state.57 *58 * Pass only a function to create a "curried producer" which relieves you59 * from passing the recipe function every time.60 *61 * Only plain objects and arrays are made mutable. All other objects are62 * considered uncopyable.63 *64 * Note: This function is __bound__ to its `Immer` instance.65 *66 * @param {any} base - the initial state67 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified68 * @param {Function} patchListener - optional function that will be called with all the patches produced here69 * @returns {any} a new state, or the initial state if nothing was modified70 */71 produce(base: any, recipe?: any, patchListener?: any) {72 // curried invocation73 if (typeof base === "function" && typeof recipe !== "function") {74 const defaultBase = recipe75 recipe = base7677 const self = this78 return function curriedProduce(79 this: any,80 base = defaultBase,81 ...args: any[]82 ) {83 return self.produce(base, (draft: Drafted) => recipe.call(this, draft, ...args)) // prettier-ignore84 }85 }86

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

lines 87–94why, one step back
87 invariant(
88 typeof recipe === "function",89 "The first or second argument to `produce` must be a function"90 )91 invariant(92 patchListener === undefined || typeof patchListener === "function",93 "The third argument to `produce` must be a function or undefined"94 )

More WIPMichel Weststratebefore adoptionee1c977

lines 95–99why, one step back
95
96 let result9798 // Only plain objects, arrays, and "immerable classes" are drafted.99 if (isDraftable(base)) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

More WIPMichel Weststratebefore adoptionee1c977

lines 101–107why, one step back
101 const proxy = createProxy(this, base, undefined)
102 let hasError = true103 try {104 result = recipe(proxy)105 hasError = false106 } finally {107 // finally instead of catch + rethrow better preserves original stack

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

More WIPMichel Weststratebefore adoptionee1c977

lines 110–113why, one step back
110 }
111 if (typeof Promise !== "undefined" && result instanceof Promise) {112 return result.then(113 result => {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

Small improvementsMichel Weststratebefore adoption712af5c

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

More WIPMichel Weststratebefore adoptionee1c977

lines 116–117why, one step back
116 },
117 error => {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

More WIPMichel Weststratebefore adoptionee1c977

lines 119–122why, one step back
119 throw error
120 }121 )122 }

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

Small improvementsMichel Weststratebefore adoption712af5c

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

More WIPMichel Weststratebefore adoptionee1c977

lines 125–128why, one step back
125 } else {
126 result = recipe(base)127 if (result === NOTHING) return undefined128 if (result === undefined) result = base

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

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

More WIPMichel Weststratebefore adoptionee1c977

lines 130–140why, one step back
130 return result
131 }132 }133134 produceWithPatches(arg1: any, arg2?: any, arg3?: any): any {135 if (typeof arg1 === "function") {136 return (state: any, ...args: any[]) =>137 this.produceWithPatches(state, (draft: any) => arg1(draft, ...args))138 }139 // non-curried form140 /* istanbul ignore next */

Killed `die`, -14 bytesMichel Weststratebefore adoptiond77a5c1

line 141why, one step back
141 invariant(!arg3)

More WIPMichel Weststratebefore adoptionee1c977

lines 142–150why, one step back
142 let patches: Patch[], inversePatches: Patch[]
143 const nextState = this.produce(arg1, arg2, (p: Patch[], ip: Patch[]) => {144 patches = p145 inversePatches = ip146 })147 return [nextState, patches!, inversePatches!]148 }149150 createDraft<T extends Objectish>(base: T): Draft<T> {

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

line 151why, one step back
151 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 152why, one step back
152 const scope = ImmerScope.enter_(this)

More WIPMichel Weststratebefore adoptionee1c977

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

More WIPMichel Weststratebefore adoptionee1c977

lines 156–163why, one step back
156 return proxy as any
157 }158159 finishDraft<D extends Draft<any>>(160 draft: D,161 patchListener?: PatchListener162 ): D extends Draft<infer T> ? T : never {163 const state: ImmerState = draft && draft[DRAFT_STATE]

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 164–167why, one step back
164 invariant(state && state.isManual_, "First argument to `finishDraft` must be a draft returned by `createDraft`") // prettier-ignore
165 invariant(!state.finalized_, "The given draft is already finalized") // prettier-ignore166 const {scope_: scope} = state167 scope.usePatches_(patchListener)

Small improvementsMichel Weststratebefore adoption712af5c

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

More WIPMichel Weststratebefore adoptionee1c977

lines 169–176why, one step back
169 }
170171 /**172 * Pass true to automatically freeze all copies created by Immer.173 *174 * By default, auto-freezing is disabled in production.175 */176 setAutoFreeze(value: boolean) {

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

line 177why, one step back
177 this.autoFreeze_ = value

More WIPMichel Weststratebefore adoptionee1c977

lines 178–186why, one step back
178 }
179180 /**181 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is182 * always faster than using ES5 proxies.183 *184 * By default, feature detection is used, so calling this is rarely necessary.185 */186 setUseProxies(value: boolean) {

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

line 187why, one step back
187 this.useProxies_ = value

More WIPMichel Weststratebefore adoptionee1c977

lines 188–201why, one step back
188 }
189190 applyPatches(base: Objectish, patches: Patch[]) {191 // If a patch replaces the entire state, take that replacement as base192 // before applying patches193 let i: number194 for (i = patches.length - 1; i >= 0; i--) {195 const patch = patches[i]196 if (patch.path.length === 0 && patch.op === "replace") {197 base = patch.value198 break199 }200 }201

scramble plugin names, -56Michel Weststratebefore adoption037b474

line 202why, one step back
202 const applyPatchesImpl = getPlugin("patches").applyPatches_

More WIPMichel Weststratebefore adoptionee1c977

lines 203–204why, one step back
203 if (isDraft(base)) {
204 // N.B: never hits if some patch a replacement, patches are never drafts

scramble plugin names, -56Michel Weststratebefore adoption037b474

line 205why, one step back
205 return applyPatchesImpl(base, patches)

More WIPMichel Weststratebefore adoptionee1c977

lines 206–208why, one step back
206 }
207 // Otherwise, produce a copy of the base state.208 return this.produce(base, (draft: Drafted) =>

scramble plugin names, -56Michel Weststratebefore adoption037b474

line 209why, one step back
209 applyPatchesImpl(draft, patches.slice(i + 1))

More WIPMichel Weststratebefore adoptionee1c977

lines 210–220why, one step back
210 )
211 }212}213214export function createProxy<T extends Objectish>(215 immer: Immer,216 value: T,217 parent?: ImmerState218): Drafted<T, ImmerState> {219 // precondition: createProxy should be guarded by isDraftable, so we know we can safely draft220 const draft: Drafted = isMap(value)

scramble plugin names, -56Michel Weststratebefore adoption037b474

line 221why, one step back
221 ? getPlugin("mapset").proxyMap_(value, parent)

More WIPMichel Weststratebefore adoptionee1c977

line 222why, one step back
222 : isSet(value)

scramble plugin names, -56Michel Weststratebefore adoption037b474

line 223why, one step back
223 ? getPlugin("mapset").proxySet_(value, parent)

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

line 224why, one step back
224 : immer.useProxies_

More WIPMichel Weststratebefore adoptionee1c977

line 225why, one step back
225 ? createProxyProxy(value, parent)

scramble plugin names, -56Michel Weststratebefore adoption037b474

line 226why, one step back
226 : getPlugin("es5").createES5Proxy_(value, parent)

More WIPMichel Weststratebefore adoptionee1c977

line 227why, one step back
227

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

More WIPMichel Weststratebefore adoptionee1c977

lines 230–233why, one step back
230 return draft
231}232233export function markChanged(immer: Immer, state: ImmerState) {

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

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

More WIPMichel Weststratebefore adoptionee1c977

lines 235–236why, one step back
235 markChangedProxy(state)
236 } else {

scramble plugin names, -56Michel Weststratebefore adoption037b474

line 237why, one step back
237 getPlugin("es5").markChangedES5_(state)

More WIPMichel Weststratebefore adoptionee1c977

lines 238–239why, one step back
238 }
239}