immerClass.ts
This file
Path
src/core/immerClass.ts
241 lines · 6.7 KiB · TypeScript
Last changed by
chore: fixed jsdoc parameter name (#994)Christopher Tranbefore adoption8a02d81
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–114why, 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)91 const proxy = createProxy(this, base, undefined)92 let hasError = true93 try {94 result = recipe(proxy)95 hasError = false96 } finally {97 // finally instead of catch + rethrow better preserves original stack98 if (hasError) revokeScope(scope)99 else leaveScope(scope)100 }101 if (typeof Promise !== "undefined" && result instanceof Promise) {102 return result.then(103 result => {104 usePatchesInScope(scope, patchListener)105 return processResult(result, scope)106 },107 error => {108 revokeScope(scope)109 throw error110 }111 )112 }113 usePatchesInScope(scope, patchListener)114 return processResult(result, scope)Immer 7Michel Weststratebefore adoption467ea5d
line 115why, one step back115 } else if (!base || typeof base !== "object") {fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 116–117why, one step back116 result = recipe(base)117 if (result === undefined) result = basefix: No patches being generated for root primitive replacements. Fixes #888Michel Weststratebefore adoption0f96270
line 118why, one step back118 if (result === NOTHING) result = undefinedfix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
line 119why, one step back119 if (this.autoFreeze_) freeze(result, true)fix: No patches being generated for root primitive replacements. Fixes #888Michel Weststratebefore adoption0f96270
lines 120–125why, one step back120 if (patchListener) {121 const p: Patch[] = []122 const ip: Patch[] = []123 getPlugin("Patches").generateReplacementPatches_(base, result, p, ip)124 patchListener(p, ip)125 }fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
line 126why, one step back126 return resultImmer 7Michel Weststratebefore adoption467ea5d
line 127why, one step back127 } else die(21, base)fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 128–129why, one step back128 }129chore: fixed jsdoc parameter name (#994)Christopher Tranbefore adoption8a02d81
line 130why, one step back130 produceWithPatches: IProduceWithPatches = (base: any, recipe?: any): any => {chore: remove unused arg3 in produceWithPatches (#975)Brian Hungbefore adoption078e523
lines 131–132why, one step back131 // curried invocation132 if (typeof base === "function") {fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
line 133why, one step back133 return (state: any, ...args: any[]) =>chore: remove unused arg3 in produceWithPatches (#975)Brian Hungbefore adoption078e523
line 134why, one step back134 this.produceWithPatches(state, (draft: any) => base(draft, ...args))fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 135–137why, one step back135 }136137 let patches: Patch[], inversePatches: Patch[]chore: remove unused arg3 in produceWithPatches (#975)Brian Hungbefore adoption078e523
line 138why, one step back138 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 139–141why, one step back139 patches = p140 inversePatches = ip141 })fix: make sure produceWithPatches accepts promisescviejobefore adoptiond80e823
lines 142–146why, one step back142143 if (typeof Promise !== "undefined" && result instanceof Promise) {144 return result.then(nextState => [nextState, patches!, inversePatches!])145 }146 return [result, patches!, inversePatches!]fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 147–150why, one step back147 }148149 createDraft<T extends Objectish>(base: T): Draft<T> {150 if (!isDraftable(base)) die(8)Immer 7Michel Weststratebefore adoption467ea5d
line 151why, one step back151 if (isDraft(base)) base = current(base)fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 152–162why, one step back152 const scope = enterScope(this)153 const proxy = createProxy(this, base, undefined)154 proxy[DRAFT_STATE].isManual_ = true155 leaveScope(scope)156 return proxy as any157 }158159 finishDraft<D extends Draft<any>>(160 draft: D,161 patchListener?: PatchListener162 ): D extends Draft<infer T> ? T : never {Immer 7Michel Weststratebefore adoption467ea5d
line 163why, one step back163 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 164–175why, one step back164 if (__DEV__) {165 if (!state || !state.isManual_) die(9)166 if (state.finalized_) die(10)167 }168 const {scope_: scope} = state169 usePatchesInScope(scope, patchListener)170 return processResult(undefined, scope)171 }172173 /**174 * Pass true to automatically freeze all copies created by Immer.175 *docs: some refinements on freezingMichel Weststratebefore adoption7faa7b4
line 176why, one step back176 * By default, auto-freezing is enabled.fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 177–188why, one step back177 */178 setAutoFreeze(value: boolean) {179 this.autoFreeze_ = value180 }181182 /**183 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is184 * always faster than using ES5 proxies.185 *186 * By default, feature detection is used, so calling this is rarely necessary.187 */188 setUseProxies(value: boolean) {Immer 7Michel Weststratebefore adoption467ea5d
line 189why, one step back189 if (value && !hasProxies) {fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 190–194why, one step back190 die(20)191 }192 this.useProxies_ = value193 }194fix: Better applyPatches type (#810)Josh Kelleybefore adoption09ac097
line 195why, one step back195 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 196–205why, one step back196 // 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 }fix: Append to array when key is "-" (#872)Louis DeSciolibefore adoption2afdb1b
lines 206–210why, one step back206 // If there was a patch that replaced the entire state, start from the207 // patch after that.208 if (i > -1) {209 patches = patches.slice(i + 1)210 }fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 211–214why, one step back211212 const applyPatchesImpl = getPlugin("Patches").applyPatches_213 if (isDraft(base)) {214 // N.B: never hits if some patch a replacement, patches are never draftsfix: Append to array when key is "-" (#872)Louis DeSciolibefore adoption2afdb1b
line 215why, one step back215 return applyPatchesImpl(base, patches)fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 216–218why, one step back216 }217 // Otherwise, produce a copy of the base state.218 return this.produce(base, (draft: Drafted) =>fix: Append to array when key is "-" (#872)Louis DeSciolibefore adoption2afdb1b
lines 219–220why, one step back219 applyPatchesImpl(draft, patches)220 )fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 221–241why, one step back221 }222}223224export function createProxy<T extends Objectish>(225 immer: Immer,226 value: T,227 parent?: ImmerState228): Drafted<T, ImmerState> {229 // precondition: createProxy should be guarded by isDraftable, so we know we can safely draft230 const draft: Drafted = isMap(value)231 ? getPlugin("MapSet").proxyMap_(value, parent)232 : isSet(value)233 ? getPlugin("MapSet").proxySet_(value, parent)234 : immer.useProxies_235 ? createProxyProxy(value, parent)236 : getPlugin("ES5").createES5Proxy_(value, parent)237238 const scope = parent ? parent.scope_ : getCurrentScope()239 scope.drafts_.push(draft)240 return draft241}