Skip to content
Myr

immerClass.ts

This file

Path

src/core/immerClass.ts

243 lines · 6.8 KiB · TypeScript

Last changed by

fix: don't freeze drafts returned from produce if they were passed in as draft (#917)Alt-erbefore adoption46867f8

History of this file

Take it

The walk stopped after 200 commits: lines older than 497d1a0 point at it.

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 1–23why, one step back
1import {
2 IProduceWithPatches,3 IProduce,4 ImmerState,5 Drafted,6 isDraftable,7 processResult,8 Patch,9 Objectish,10 DRAFT_STATE,11 Draft,12 PatchListener,13 isDraft,14 isMap,15 isSet,16 createProxyProxy,17 getPlugin,18 die,19 hasProxies,20 enterScope,21 revokeScope,22 leaveScope,23 usePatchesInScope,

Immer 7Michel Weststratebefore adoption467ea5d

lines 24–27why, one step back
24 getCurrentScope,
25 NOTHING,26 freeze,27 current

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 28–37why, one step back
28} from "../internal"
2930interface ProducersFns {31 produce: IProduce32 produceWithPatches: IProduceWithPatches33}3435export class Immer implements ProducersFns {36 useProxies_: boolean = hasProxies37

feature: Always freeze by default (#702)Michel Weststratebefore adoptiona406c8f

line 38why, one step back
38 autoFreeze_: boolean = true

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 39–61why, one step back
39
40 constructor(config?: {useProxies?: boolean; autoFreeze?: boolean}) {41 if (typeof config?.useProxies === "boolean")42 this.setUseProxies(config!.useProxies)43 if (typeof config?.autoFreeze === "boolean")44 this.setAutoFreeze(config!.autoFreeze)45 }4647 /**48 * The `produce` function takes a value and a "recipe function" (whose49 * return value often depends on the base state). The recipe function is50 * free to mutate its first argument however it wants. All mutations are51 * only ever applied to a __copy__ of the base state.52 *53 * Pass only a function to create a "curried producer" which relieves you54 * from passing the recipe function every time.55 *56 * Only plain objects and arrays are made mutable. All other objects are57 * considered uncopyable.58 *59 * Note: This function is __bound__ to its `Immer` instance.60 *61 * @param {any} base - the initial state

chore: fixed jsdoc parameter name (#994)Christopher Tranbefore adoption8a02d81

line 62why, one step back
62 * @param {Function} recipe - function that receives a proxy of the base state as first argument and which can be freely modified

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 63–65why, one step back
63 * @param {Function} patchListener - optional function that will be called with all the patches produced here
64 * @returns {any} a new state, or the initial state if nothing was modified65 */

fix: new Immer().produce now has the same type as produce. Fixes #749Michel Weststratebefore adoptionf8b77d1

line 66why, one step back
66 produce: IProduce = (base: any, recipe?: any, patchListener?: any) => {

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 67–90why, one step back
67 // curried invocation
68 if (typeof base === "function" && typeof recipe !== "function") {69 const defaultBase = recipe70 recipe = base7172 const self = this73 return function curriedProduce(74 this: any,75 base = defaultBase,76 ...args: any[]77 ) {78 return self.produce(base, (draft: Drafted) => recipe.call(this, draft, ...args)) // prettier-ignore79 }80 }8182 if (typeof recipe !== "function") die(6)83 if (patchListener !== undefined && typeof patchListener !== "function")84 die(7)8586 let result8788 // Only plain objects, arrays, and "immerable classes" are drafted.89 if (isDraftable(base)) {90 const scope = enterScope(this)

fix: don't freeze drafts returned from produce if they were passed in as draft (#917)Alt-erbefore adoption46867f8

lines 91–92why, one step back
91 //when base is a draft,can't freeze
92 scope.canAutoFreeze_ = !isDraft(base)

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 93–116why, one step back
93 const proxy = createProxy(this, base, undefined)
94 let hasError = true95 try {96 result = recipe(proxy)97 hasError = false98 } finally {99 // finally instead of catch + rethrow better preserves original stack100 if (hasError) revokeScope(scope)101 else leaveScope(scope)102 }103 if (typeof Promise !== "undefined" && result instanceof Promise) {104 return result.then(105 result => {106 usePatchesInScope(scope, patchListener)107 return processResult(result, scope)108 },109 error => {110 revokeScope(scope)111 throw error112 }113 )114 }115 usePatchesInScope(scope, patchListener)116 return processResult(result, scope)

Immer 7Michel Weststratebefore adoption467ea5d

line 117why, one step back
117 } else if (!base || typeof base !== "object") {

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 118–119why, one step back
118 result = recipe(base)
119 if (result === undefined) result = base

fix: No patches being generated for root primitive replacements. Fixes #888Michel Weststratebefore adoption0f96270

line 120why, one step back
120 if (result === NOTHING) result = undefined

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

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

fix: No patches being generated for root primitive replacements. Fixes #888Michel Weststratebefore adoption0f96270

lines 122–127why, one step back
122 if (patchListener) {
123 const p: Patch[] = []124 const ip: Patch[] = []125 getPlugin("Patches").generateReplacementPatches_(base, result, p, ip)126 patchListener(p, ip)127 }

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

line 128why, one step back
128 return result

Immer 7Michel Weststratebefore adoption467ea5d

line 129why, one step back
129 } else die(21, base)

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 130–131why, one step back
130 }
131

chore: fixed jsdoc parameter name (#994)Christopher Tranbefore adoption8a02d81

line 132why, one step back
132 produceWithPatches: IProduceWithPatches = (base: any, recipe?: any): any => {

chore: remove unused arg3 in produceWithPatches (#975)Brian Hungbefore adoption078e523

lines 133–134why, one step back
133 // curried invocation
134 if (typeof base === "function") {

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

line 135why, one step back
135 return (state: any, ...args: any[]) =>

chore: remove unused arg3 in produceWithPatches (#975)Brian Hungbefore adoption078e523

line 136why, one step back
136 this.produceWithPatches(state, (draft: any) => base(draft, ...args))

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 137–139why, one step back
137 }
138139 let patches: Patch[], inversePatches: Patch[]

chore: remove unused arg3 in produceWithPatches (#975)Brian Hungbefore adoption078e523

line 140why, one step back
140 const result = this.produce(base, recipe, (p: Patch[], ip: Patch[]) => {

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 141–143why, one step back
141 patches = p
142 inversePatches = ip143 })

fix: make sure produceWithPatches accepts promisescviejobefore adoptiond80e823

lines 144–148why, one step back
144
145 if (typeof Promise !== "undefined" && result instanceof Promise) {146 return result.then(nextState => [nextState, patches!, inversePatches!])147 }148 return [result, patches!, inversePatches!]

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 149–152why, one step back
149 }
150151 createDraft<T extends Objectish>(base: T): Draft<T> {152 if (!isDraftable(base)) die(8)

Immer 7Michel Weststratebefore adoption467ea5d

line 153why, one step back
153 if (isDraft(base)) base = current(base)

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 154–164why, one step back
154 const scope = enterScope(this)
155 const proxy = createProxy(this, base, undefined)156 proxy[DRAFT_STATE].isManual_ = true157 leaveScope(scope)158 return proxy as any159 }160161 finishDraft<D extends Draft<any>>(162 draft: D,163 patchListener?: PatchListener164 ): D extends Draft<infer T> ? T : never {

Immer 7Michel Weststratebefore adoption467ea5d

line 165why, one step back
165 const state: ImmerState = draft && (draft as any)[DRAFT_STATE]

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 166–177why, one step back
166 if (__DEV__) {
167 if (!state || !state.isManual_) die(9)168 if (state.finalized_) die(10)169 }170 const {scope_: scope} = state171 usePatchesInScope(scope, patchListener)172 return processResult(undefined, scope)173 }174175 /**176 * Pass true to automatically freeze all copies created by Immer.177 *

docs: some refinements on freezingMichel Weststratebefore adoption7faa7b4

line 178why, one step back
178 * By default, auto-freezing is enabled.

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 179–190why, one step back
179 */
180 setAutoFreeze(value: boolean) {181 this.autoFreeze_ = value182 }183184 /**185 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is186 * always faster than using ES5 proxies.187 *188 * By default, feature detection is used, so calling this is rarely necessary.189 */190 setUseProxies(value: boolean) {

Immer 7Michel Weststratebefore adoption467ea5d

line 191why, one step back
191 if (value && !hasProxies) {

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 192–196why, one step back
192 die(20)
193 }194 this.useProxies_ = value195 }196

fix: Better applyPatches type (#810)Josh Kelleybefore adoption09ac097

line 197why, one step back
197 applyPatches<T extends Objectish>(base: T, patches: Patch[]): T {

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 198–207why, one step back
198 // If a patch replaces the entire state, take that replacement as base
199 // before applying patches200 let i: number201 for (i = patches.length - 1; i >= 0; i--) {202 const patch = patches[i]203 if (patch.path.length === 0 && patch.op === "replace") {204 base = patch.value205 break206 }207 }

fix: Append to array when key is "-" (#872)Louis DeSciolibefore adoption2afdb1b

lines 208–212why, one step back
208 // If there was a patch that replaced the entire state, start from the
209 // patch after that.210 if (i > -1) {211 patches = patches.slice(i + 1)212 }

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 213–216why, one step back
213
214 const applyPatchesImpl = getPlugin("Patches").applyPatches_215 if (isDraft(base)) {216 // N.B: never hits if some patch a replacement, patches are never drafts

fix: Append to array when key is "-" (#872)Louis DeSciolibefore adoption2afdb1b

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

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

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

fix: Append to array when key is "-" (#872)Louis DeSciolibefore adoption2afdb1b

lines 221–222why, one step back
221 applyPatchesImpl(draft, patches)
222 )

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 223–243why, one step back
223 }
224}225226export function createProxy<T extends Objectish>(227 immer: Immer,228 value: T,229 parent?: ImmerState230): Drafted<T, ImmerState> {231 // precondition: createProxy should be guarded by isDraftable, so we know we can safely draft232 const draft: Drafted = isMap(value)233 ? getPlugin("MapSet").proxyMap_(value, parent)234 : isSet(value)235 ? getPlugin("MapSet").proxySet_(value, parent)236 : immer.useProxies_237 ? createProxyProxy(value, parent)238 : getPlugin("ES5").createES5Proxy_(value, parent)239240 const scope = parent ? parent.scope_ : getCurrentScope()241 scope.drafts_.push(draft)242 return draft243}