immerClass.ts
This file
Path
src/core/immerClass.ts
218 lines · 6.1 KiB · TypeScript
Last changed by
feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203
History of this fileTake it
The walk stopped after 200 commits: lines older than 1a15615 point at it.
chore(deps): bump ini from 1.3.5 to 1.3.7 (#722)dependabot[bot]before adoption1a15615
lines 1–33why, 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 enterScope,20 revokeScope,21 leaveScope,22 usePatchesInScope,23 getCurrentScope,24 NOTHING,25 freeze,26 current27} from "../internal"2829interface ProducersFns {30 produce: IProduce31 produceWithPatches: IProduceWithPatches32}33feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203
lines 34–35why, one step back34export type StrictMode = boolean | "class_only";35chore(deps): bump ini from 1.3.5 to 1.3.7 (#722)dependabot[bot]before adoption1a15615
lines 36–37why, one step back36export class Immer implements ProducersFns {37 autoFreeze_: boolean = truefeat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203
line 38why, one step back38 useStrictShallowCopy_: StrictMode = falsechore(deps): bump ini from 1.3.5 to 1.3.7 (#722)dependabot[bot]before adoption1a15615
line 39why, one step back39feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203
lines 40–43why, one step back40 constructor(config?: {41 autoFreeze?: boolean42 useStrictShallowCopy?: StrictMode43 }) {chore(deps): bump ini from 1.3.5 to 1.3.7 (#722)dependabot[bot]before adoption1a15615
lines 44–45why, one step back44 if (typeof config?.autoFreeze === "boolean")45 this.setAutoFreeze(config!.autoFreeze)Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 46–47why, one step back46 if (typeof config?.useStrictShallowCopy === "boolean")47 this.setUseStrictShallowCopy(config!.useStrictShallowCopy)chore(deps): bump ini from 1.3.5 to 1.3.7 (#722)dependabot[bot]before adoption1a15615
lines 48–64why, one step back48 }4950 /**51 * The `produce` function takes a value and a "recipe function" (whose52 * return value often depends on the base state). The recipe function is53 * free to mutate its first argument however it wants. All mutations are54 * only ever applied to a __copy__ of the base state.55 *56 * Pass only a function to create a "curried producer" which relieves you57 * from passing the recipe function every time.58 *59 * Only plain objects and arrays are made mutable. All other objects are60 * considered uncopyable.61 *62 * Note: This function is __bound__ to its `Immer` instance.63 *64 * @param {any} base - the initial statechore: fixed jsdoc parameter name (#994)Christopher Tranbefore adoption8a02d81
line 65why, one step back65 * @param {Function} recipe - function that receives a proxy of the base state as first argument and which can be freely modifiedchore(deps): bump ini from 1.3.5 to 1.3.7 (#722)dependabot[bot]before adoption1a15615
lines 66–68why, one step back66 * @param {Function} patchListener - optional function that will be called with all the patches produced here67 * @returns {any} a new state, or the initial state if nothing was modified68 */fix: new Immer().produce now has the same type as produce. Fixes #749Michel Weststratebefore adoptionf8b77d1
line 69why, one step back69 produce: IProduce = (base: any, recipe?: any, patchListener?: any) => {chore(deps): bump ini from 1.3.5 to 1.3.7 (#722)dependabot[bot]before adoption1a15615
lines 70–93why, one step back70 // curried invocation71 if (typeof base === "function" && typeof recipe !== "function") {72 const defaultBase = recipe73 recipe = base7475 const self = this76 return function curriedProduce(77 this: any,78 base = defaultBase,79 ...args: any[]80 ) {81 return self.produce(base, (draft: Drafted) => recipe.call(this, draft, ...args)) // prettier-ignore82 }83 }8485 if (typeof recipe !== "function") die(6)86 if (patchListener !== undefined && typeof patchListener !== "function")87 die(7)8889 let result9091 // Only plain objects, arrays, and "immerable classes" are drafted.92 if (isDraftable(base)) {93 const scope = enterScope(this)Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 94why, one step back94 const proxy = createProxy(base, undefined)chore(deps): bump ini from 1.3.5 to 1.3.7 (#722)dependabot[bot]before adoption1a15615
lines 95–108why, one step back95 let hasError = true96 try {97 result = recipe(proxy)98 hasError = false99 } finally {100 // finally instead of catch + rethrow better preserves original stack101 if (hasError) revokeScope(scope)102 else leaveScope(scope)103 }104 usePatchesInScope(scope, patchListener)105 return processResult(result, scope)106 } else if (!base || typeof base !== "object") {107 result = recipe(base)108 if (result === undefined) result = basefix: No patches being generated for root primitive replacements. Fixes #888Michel Weststratebefore adoption0f96270
line 109why, one step back109 if (result === NOTHING) result = undefinedchore(deps): bump ini from 1.3.5 to 1.3.7 (#722)dependabot[bot]before adoption1a15615
line 110why, one step back110 if (this.autoFreeze_) freeze(result, true)fix: No patches being generated for root primitive replacements. Fixes #888Michel Weststratebefore adoption0f96270
lines 111–116why, one step back111 if (patchListener) {112 const p: Patch[] = []113 const ip: Patch[] = []114 getPlugin("Patches").generateReplacementPatches_(base, result, p, ip)115 patchListener(p, ip)116 }chore(deps): bump ini from 1.3.5 to 1.3.7 (#722)dependabot[bot]before adoption1a15615
line 117why, one step back117 return resultMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 118why, one step back118 } else die(1, base)chore(deps): bump ini from 1.3.5 to 1.3.7 (#722)dependabot[bot]before adoption1a15615
lines 119–120why, one step back119 }120chore: fixed jsdoc parameter name (#994)Christopher Tranbefore adoption8a02d81
line 121why, one step back121 produceWithPatches: IProduceWithPatches = (base: any, recipe?: any): any => {chore: remove unused arg3 in produceWithPatches (#975)Brian Hungbefore adoption078e523
lines 122–123why, one step back122 // curried invocation123 if (typeof base === "function") {chore(deps): bump ini from 1.3.5 to 1.3.7 (#722)dependabot[bot]before adoption1a15615
line 124why, one step back124 return (state: any, ...args: any[]) =>chore: remove unused arg3 in produceWithPatches (#975)Brian Hungbefore adoption078e523
line 125why, one step back125 this.produceWithPatches(state, (draft: any) => base(draft, ...args))chore(deps): bump ini from 1.3.5 to 1.3.7 (#722)dependabot[bot]before adoption1a15615
lines 126–128why, one step back126 }127128 let patches: Patch[], inversePatches: Patch[]chore: remove unused arg3 in produceWithPatches (#975)Brian Hungbefore adoption078e523
line 129why, one step back129 const result = this.produce(base, recipe, (p: Patch[], ip: Patch[]) => {chore(deps): bump ini from 1.3.5 to 1.3.7 (#722)dependabot[bot]before adoption1a15615
lines 130–132why, one step back130 patches = p131 inversePatches = ip132 })fix: make sure produceWithPatches accepts promisescviejobefore adoptiond80e823
line 133why, one step back133 return [result, patches!, inversePatches!]chore(deps): bump ini from 1.3.5 to 1.3.7 (#722)dependabot[bot]before adoption1a15615
lines 134–139why, one step back134 }135136 createDraft<T extends Objectish>(base: T): Draft<T> {137 if (!isDraftable(base)) die(8)138 if (isDraft(base)) base = current(base)139 const scope = enterScope(this)Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 140why, one step back140 const proxy = createProxy(base, undefined)chore(deps): bump ini from 1.3.5 to 1.3.7 (#722)dependabot[bot]before adoption1a15615
lines 141–150why, one step back141 proxy[DRAFT_STATE].isManual_ = true142 leaveScope(scope)143 return proxy as any144 }145146 finishDraft<D extends Draft<any>>(147 draft: D,148 patchListener?: PatchListener149 ): D extends Draft<infer T> ? T : never {150 const state: ImmerState = draft && (draft as any)[DRAFT_STATE]Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 151why, one step back151 if (!state || !state.isManual_) die(9)chore(deps): bump ini from 1.3.5 to 1.3.7 (#722)dependabot[bot]before adoption1a15615
lines 152–166why, one step back152 const {scope_: scope} = state153 usePatchesInScope(scope, patchListener)154 return processResult(undefined, scope)155 }156157 /**158 * Pass true to automatically freeze all copies created by Immer.159 *160 * By default, auto-freezing is enabled.161 */162 setAutoFreeze(value: boolean) {163 this.autoFreeze_ = value164 }165166 /**Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 167why, one step back167 * Pass true to enable strict shallow copy.chore(deps): bump ini from 1.3.5 to 1.3.7 (#722)dependabot[bot]before adoption1a15615
line 168why, one step back168 *Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 169why, one step back169 * By default, immer does not copy the object descriptors such as getter, setter and non-enumrable properties.chore(deps): bump ini from 1.3.5 to 1.3.7 (#722)dependabot[bot]before adoption1a15615
line 170why, one step back170 */feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203
line 171why, one step back171 setUseStrictShallowCopy(value: StrictMode) {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 172why, one step back172 this.useStrictShallowCopy_ = valuechore(deps): bump ini from 1.3.5 to 1.3.7 (#722)dependabot[bot]before adoption1a15615
lines 173–174why, one step back173 }174fix: Better applyPatches type (#810)Josh Kelleybefore adoption09ac097
line 175why, one step back175 applyPatches<T extends Objectish>(base: T, patches: Patch[]): T {chore(deps): bump ini from 1.3.5 to 1.3.7 (#722)dependabot[bot]before adoption1a15615
lines 176–185why, one step back176 // If a patch replaces the entire state, take that replacement as base177 // before applying patches178 let i: number179 for (i = patches.length - 1; i >= 0; i--) {180 const patch = patches[i]181 if (patch.path.length === 0 && patch.op === "replace") {182 base = patch.value183 break184 }185 }fix: Append to array when key is "-" (#872)Louis DeSciolibefore adoption2afdb1b
lines 186–190why, one step back186 // If there was a patch that replaced the entire state, start from the187 // patch after that.188 if (i > -1) {189 patches = patches.slice(i + 1)190 }chore(deps): bump ini from 1.3.5 to 1.3.7 (#722)dependabot[bot]before adoption1a15615
lines 191–194why, one step back191192 const applyPatchesImpl = getPlugin("Patches").applyPatches_193 if (isDraft(base)) {194 // N.B: never hits if some patch a replacement, patches are never draftsfix: Append to array when key is "-" (#872)Louis DeSciolibefore adoption2afdb1b
line 195why, one step back195 return applyPatchesImpl(base, patches)chore(deps): bump ini from 1.3.5 to 1.3.7 (#722)dependabot[bot]before adoption1a15615
lines 196–198why, one step back196 }197 // Otherwise, produce a copy of the base state.198 return this.produce(base, (draft: Drafted) =>fix: Append to array when key is "-" (#872)Louis DeSciolibefore adoption2afdb1b
lines 199–200why, one step back199 applyPatchesImpl(draft, patches)200 )chore(deps): bump ini from 1.3.5 to 1.3.7 (#722)dependabot[bot]before adoption1a15615
lines 201–212why, one step back201 }202}203204export function createProxy<T extends Objectish>(205 value: T,206 parent?: ImmerState207): Drafted<T, ImmerState> {208 // precondition: createProxy should be guarded by isDraftable, so we know we can safely draft209 const draft: Drafted = isMap(value)210 ? getPlugin("MapSet").proxyMap_(value, parent)211 : isSet(value)212 ? getPlugin("MapSet").proxySet_(value, parent)Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 213why, one step back213 : createProxyProxy(value, parent)chore(deps): bump ini from 1.3.5 to 1.3.7 (#722)dependabot[bot]before adoption1a15615
lines 214–218why, one step back214215 const scope = parent ? parent.scope_ : getCurrentScope()216 scope.drafts_.push(draft)217 return draft218}