immerClass.ts
This file
Path
src/core/immerClass.ts
273 lines · 7.6 KiB · TypeScript
Last changed by
fix: update current and original typings to assert draft value is unwrapped (#1236)Dean Kerrbefore adoptionbf2d154
History of this fileTake it
The walk stopped after 200 commits: lines older than 607b994 point at it.
chore: added unit test for #897Michel Weststratebefore adoption607b994
lines 1–25why, 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,perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 26–37why, one step back26 current,27 ImmerScope,28 registerChildFinalizationCallback,29 ArchType,30 MapSetPlugin,31 AnyMap,32 AnySet,33 isObjectish,34 isFunction,35 isBoolean,36 PluginMapSet,37 PluginPatcheschore: added unit test for #897Michel Weststratebefore adoption607b994
lines 38–44why, one step back38} from "../internal"3940interface ProducersFns {41 produce: IProduce42 produceWithPatches: IProduceWithPatches43}44feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
line 45why, one step back45export type StrictMode = boolean | "class_only"feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203
line 46why, one step back46chore: added unit test for #897Michel Weststratebefore adoption607b994
lines 47–48why, one step back47export class Immer implements ProducersFns {48 autoFreeze_: boolean = truefeat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203
line 49why, one step back49 useStrictShallowCopy_: StrictMode = falseperf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 50why, one step back50 useStrictIteration_: boolean = falsechore: added unit test for #897Michel Weststratebefore adoption607b994
line 51why, one step back51feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203
lines 52–54why, one step back52 constructor(config?: {53 autoFreeze?: boolean54 useStrictShallowCopy?: StrictModefeat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
line 55why, one step back55 useStrictIteration?: booleanfeat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203
line 56why, one step back56 }) {perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 57–58why, one step back57 if (isBoolean(config?.autoFreeze)) this.setAutoFreeze(config!.autoFreeze)58 if (isBoolean(config?.useStrictShallowCopy))Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 59why, one step back59 this.setUseStrictShallowCopy(config!.useStrictShallowCopy)perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 60why, one step back60 if (isBoolean(config?.useStrictIteration))feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
line 61why, one step back61 this.setUseStrictIteration(config!.useStrictIteration)chore: added unit test for #897Michel Weststratebefore adoption607b994
lines 62–78why, one step back62 }6364 /**65 * The `produce` function takes a value and a "recipe function" (whose66 * return value often depends on the base state). The recipe function is67 * free to mutate its first argument however it wants. All mutations are68 * only ever applied to a __copy__ of the base state.69 *70 * Pass only a function to create a "curried producer" which relieves you71 * from passing the recipe function every time.72 *73 * Only plain objects and arrays are made mutable. All other objects are74 * considered uncopyable.75 *76 * Note: This function is __bound__ to its `Immer` instance.77 *78 * @param {any} base - the initial statechore: fixed jsdoc parameter name (#994)Christopher Tranbefore adoption8a02d81
line 79why, one step back79 * @param {Function} recipe - function that receives a proxy of the base state as first argument and which can be freely modifiedchore: added unit test for #897Michel Weststratebefore adoption607b994
lines 80–84why, one step back80 * @param {Function} patchListener - optional function that will be called with all the patches produced here81 * @returns {any} a new state, or the initial state if nothing was modified82 */83 produce: IProduce = (base: any, recipe?: any, patchListener?: any) => {84 // curried invocationperf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 85why, one step back85 if (isFunction(base) && !isFunction(recipe)) {chore: added unit test for #897Michel Weststratebefore adoption607b994
lines 86–98why, one step back86 const defaultBase = recipe87 recipe = base8889 const self = this90 return function curriedProduce(91 this: any,92 base = defaultBase,93 ...args: any[]94 ) {95 return self.produce(base, (draft: Drafted) => recipe.call(this, draft, ...args)) // prettier-ignore96 }97 }98perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 99–100why, one step back99 if (!isFunction(recipe)) die(6)100 if (patchListener !== undefined && !isFunction(patchListener)) die(7)chore: added unit test for #897Michel Weststratebefore adoption607b994
lines 101–106why, one step back101102 let result103104 // Only plain objects, arrays, and "immerable classes" are drafted.105 if (isDraftable(base)) {106 const scope = enterScope(this)perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 107why, one step back107 const proxy = createProxy(scope, base, undefined)chore: added unit test for #897Michel Weststratebefore adoption607b994
lines 108–118why, one step back108 let hasError = true109 try {110 result = recipe(proxy)111 hasError = false112 } finally {113 // finally instead of catch + rethrow better preserves original stack114 if (hasError) revokeScope(scope)115 else leaveScope(scope)116 }117 usePatchesInScope(scope, patchListener)118 return processResult(result, scope)perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 119why, one step back119 } else if (!base || !isObjectish(base)) {chore: added unit test for #897Michel Weststratebefore adoption607b994
lines 120–126why, one step back120 result = recipe(base)121 if (result === undefined) result = base122 if (result === NOTHING) result = undefined123 if (this.autoFreeze_) freeze(result, true)124 if (patchListener) {125 const p: Patch[] = []126 const ip: Patch[] = []perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 127–130why, one step back127 getPlugin(PluginPatches).generateReplacementPatches_(base, result, {128 patches_: p,129 inversePatches_: ip130 } as ImmerScope) // dummy scopechore: added unit test for #897Michel Weststratebefore adoption607b994
lines 131–133why, one step back131 patchListener(p, ip)132 }133 return resultMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 134why, one step back134 } else die(1, base)chore: added unit test for #897Michel Weststratebefore adoption607b994
lines 135–136why, one step back135 }136chore: fixed jsdoc parameter name (#994)Christopher Tranbefore adoption8a02d81
line 137why, one step back137 produceWithPatches: IProduceWithPatches = (base: any, recipe?: any): any => {chore: remove unused arg3 in produceWithPatches (#975)Brian Hungbefore adoption078e523
line 138why, one step back138 // curried invocationperf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 139why, one step back139 if (isFunction(base)) {chore: added unit test for #897Michel Weststratebefore adoption607b994
line 140why, one step back140 return (state: any, ...args: any[]) =>chore: remove unused arg3 in produceWithPatches (#975)Brian Hungbefore adoption078e523
line 141why, one step back141 this.produceWithPatches(state, (draft: any) => base(draft, ...args))chore: added unit test for #897Michel Weststratebefore adoption607b994
lines 142–144why, one step back142 }143144 let patches: Patch[], inversePatches: Patch[]chore: remove unused arg3 in produceWithPatches (#975)Brian Hungbefore adoption078e523
line 145why, one step back145 const result = this.produce(base, recipe, (p: Patch[], ip: Patch[]) => {chore: added unit test for #897Michel Weststratebefore adoption607b994
lines 146–153why, one step back146 patches = p147 inversePatches = ip148 })149 return [result, patches!, inversePatches!]150 }151152 createDraft<T extends Objectish>(base: T): Draft<T> {153 if (!isDraftable(base)) die(8)fix: update current and original typings to assert draft value is unwrapped (#1236)Dean Kerrbefore adoptionbf2d154
line 154why, one step back154 if (isDraft(base)) base = current(base as Draft<T>)chore: added unit test for #897Michel Weststratebefore adoption607b994
line 155why, one step back155 const scope = enterScope(this)perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 156why, one step back156 const proxy = createProxy(scope, base, undefined)chore: added unit test for #897Michel Weststratebefore adoption607b994
lines 157–166why, one step back157 proxy[DRAFT_STATE].isManual_ = true158 leaveScope(scope)159 return proxy as any160 }161162 finishDraft<D extends Draft<any>>(163 draft: D,164 patchListener?: PatchListener165 ): D extends Draft<infer T> ? T : never {166 const state: ImmerState = draft && (draft as any)[DRAFT_STATE]Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 167why, one step back167 if (!state || !state.isManual_) die(9)chore: added unit test for #897Michel Weststratebefore adoption607b994
lines 168–182why, one step back168 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 *176 * By default, auto-freezing is enabled.177 */178 setAutoFreeze(value: boolean) {179 this.autoFreeze_ = value180 }181182 /**Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 183why, one step back183 * Pass true to enable strict shallow copy.chore: added unit test for #897Michel Weststratebefore adoption607b994
line 184why, one step back184 *Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 185why, one step back185 * By default, immer does not copy the object descriptors such as getter, setter and non-enumrable properties.chore: added unit test for #897Michel Weststratebefore adoption607b994
line 186why, one step back186 */feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203
line 187why, one step back187 setUseStrictShallowCopy(value: StrictMode) {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 188why, one step back188 this.useStrictShallowCopy_ = valuefeat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
lines 189–202why, one step back189 }190191 /**192 * Pass false to use faster iteration that skips non-enumerable properties193 * but still handles symbols for compatibility.194 *195 * By default, strict iteration is enabled (includes all own properties).196 */197 setUseStrictIteration(value: boolean) {198 this.useStrictIteration_ = value199 }200201 shouldUseStrictIteration(): boolean {202 return this.useStrictIteration_chore: added unit test for #897Michel Weststratebefore adoption607b994
lines 203–204why, one step back203 }204fix: Make `applyPatches` to accept `readonly Patch[]` (#1094)Mateusz Burzyńskibefore adoption4da2e0d
line 205why, one step back205 applyPatches<T extends Objectish>(base: T, patches: readonly Patch[]): T {chore: added unit test for #897Michel Weststratebefore adoption607b994
lines 206–221why, one step back206 // If a patch replaces the entire state, take that replacement as base207 // before applying patches208 let i: number209 for (i = patches.length - 1; i >= 0; i--) {210 const patch = patches[i]211 if (patch.path.length === 0 && patch.op === "replace") {212 base = patch.value213 break214 }215 }216 // If there was a patch that replaced the entire state, start from the217 // patch after that.218 if (i > -1) {219 patches = patches.slice(i + 1)220 }221perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 222why, one step back222 const applyPatchesImpl = getPlugin(PluginPatches).applyPatches_chore: added unit test for #897Michel Weststratebefore adoption607b994
lines 223–234why, one step back223 if (isDraft(base)) {224 // N.B: never hits if some patch a replacement, patches are never drafts225 return applyPatchesImpl(base, patches)226 }227 // Otherwise, produce a copy of the base state.228 return this.produce(base, (draft: Drafted) =>229 applyPatchesImpl(draft, patches)230 )231 }232}233234export function createProxy<T extends Objectish>(perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 235why, one step back235 rootScope: ImmerScope,chore: added unit test for #897Michel Weststratebefore adoption607b994
line 236why, one step back236 value: T,perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 237–238why, one step back237 parent?: ImmerState,238 key?: string | number | symbolchore: added unit test for #897Michel Weststratebefore adoption607b994
lines 239–240why, one step back239): Drafted<T, ImmerState> {240 // precondition: createProxy should be guarded by isDraftable, so we know we can safely draftperf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 241–244why, one step back241 // returning a tuple here lets us skip a proxy access242 // to DRAFT_STATE later243 const [draft, state] = isMap(value)244 ? getPlugin(PluginMapSet).proxyMap_(value, parent)chore: added unit test for #897Michel Weststratebefore adoption607b994
line 245why, one step back245 : isSet(value)chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f
lines 246–247why, one step back246 ? getPlugin(PluginMapSet).proxySet_(value, parent)247 : createProxyProxy(value, parent)chore: added unit test for #897Michel Weststratebefore adoption607b994
line 248why, one step back248perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 249why, one step back249 const scope = parent?.scope_ ?? getCurrentScope()chore: added unit test for #897Michel Weststratebefore adoption607b994
line 250why, one step back250 scope.drafts_.push(draft)perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 251–272why, one step back251252 // Ensure the parent callbacks are passed down so we actually253 // track all callbacks added throughout the tree254 state.callbacks_ = parent?.callbacks_ ?? []255 state.key_ = key256257 if (parent && key !== undefined) {258 registerChildFinalizationCallback(parent, state, key)259 } else {260 // It's a root draft, register it with the scope261 state.callbacks_.push(function rootDraftCleanup(rootScope) {262 rootScope.mapSetPlugin_?.fixSetContents(state)263264 const {patchPlugin_} = rootScope265266 if (state.modified_ && patchPlugin_) {267 patchPlugin_.generatePatches_(state, [], rootScope)268 }269 })270 }271272 return draft as anychore: added unit test for #897Michel Weststratebefore adoption607b994
line 273why, one step back273}