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 fileTake 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 back1import {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 back24 getCurrentScope,25 NOTHING,26 freeze,27 currentfix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 28–37why, one step back28} from "../internal"2930interface ProducersFns {31 produce: IProduce32 produceWithPatches: IProduceWithPatches33}3435export class Immer implements ProducersFns {36 useProxies_: boolean = hasProxies37feature: Always freeze by default (#702)Michel Weststratebefore adoptiona406c8f
line 38why, one step back38 autoFreeze_: boolean = truefix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 39–61why, one step back3940 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 statechore: fixed jsdoc parameter name (#994)Christopher Tranbefore adoption8a02d81
line 62why, one step back62 * @param {Function} recipe - function that receives a proxy of the base state as first argument and which can be freely modifiedfix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 63–65why, one step back63 * @param {Function} patchListener - optional function that will be called with all the patches produced here64 * @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 back66 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 back67 // curried invocation68 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 back91 //when base is a draft,can't freeze92 scope.canAutoFreeze_ = !isDraft(base)fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 93–116why, one step back93 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 back117 } 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 back118 result = recipe(base)119 if (result === undefined) result = basefix: No patches being generated for root primitive replacements. Fixes #888Michel Weststratebefore adoption0f96270
line 120why, one step back120 if (result === NOTHING) result = undefinedfix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
line 121why, one step back121 if (this.autoFreeze_) freeze(result, true)fix: No patches being generated for root primitive replacements. Fixes #888Michel Weststratebefore adoption0f96270
lines 122–127why, one step back122 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 back128 return resultImmer 7Michel Weststratebefore adoption467ea5d
line 129why, one step back129 } else die(21, base)fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 130–131why, one step back130 }131chore: fixed jsdoc parameter name (#994)Christopher Tranbefore adoption8a02d81
line 132why, one step back132 produceWithPatches: IProduceWithPatches = (base: any, recipe?: any): any => {chore: remove unused arg3 in produceWithPatches (#975)Brian Hungbefore adoption078e523
lines 133–134why, one step back133 // curried invocation134 if (typeof base === "function") {fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
line 135why, one step back135 return (state: any, ...args: any[]) =>chore: remove unused arg3 in produceWithPatches (#975)Brian Hungbefore adoption078e523
line 136why, one step back136 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 back137 }138139 let patches: Patch[], inversePatches: Patch[]chore: remove unused arg3 in produceWithPatches (#975)Brian Hungbefore adoption078e523
line 140why, one step back140 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 back141 patches = p142 inversePatches = ip143 })fix: make sure produceWithPatches accepts promisescviejobefore adoptiond80e823
lines 144–148why, one step back144145 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 back149 }150151 createDraft<T extends Objectish>(base: T): Draft<T> {152 if (!isDraftable(base)) die(8)Immer 7Michel Weststratebefore adoption467ea5d
line 153why, one step back153 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 back154 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 back165 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 back166 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 back178 * 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 back179 */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 back191 if (value && !hasProxies) {fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 192–196why, one step back192 die(20)193 }194 this.useProxies_ = value195 }196fix: Better applyPatches type (#810)Josh Kelleybefore adoption09ac097
line 197why, one step back197 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 back198 // If a patch replaces the entire state, take that replacement as base199 // 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 back208 // If there was a patch that replaced the entire state, start from the209 // 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 back213214 const applyPatchesImpl = getPlugin("Patches").applyPatches_215 if (isDraft(base)) {216 // N.B: never hits if some patch a replacement, patches are never draftsfix: Append to array when key is "-" (#872)Louis DeSciolibefore adoption2afdb1b
line 217why, one step back217 return applyPatchesImpl(base, patches)fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 218–220why, one step back218 }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 back221 applyPatchesImpl(draft, patches)222 )fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 223–243why, one step back223 }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}