immer.js
This file
Path
src/immer.js
191 lines · 6.9 KiB · JavaScript
Last changed by
perf(proxy): avoid `has` where possiblealeclarsonbefore adoption4b9b637
History of this fileTake it
feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 1–12why, one step back1import * as legacyProxy from "./es5"2import * as modernProxy from "./proxy"3import {generatePatches} from "./patches"4import {5 assign,6 each,7 is,8 isDraft,9 isDraftable,10 shallowCopy,11 DRAFT_STATE,12 NOTHINGMerge remote-tracking branch 'origin/v1.8'Michel Weststratebefore adoption7790417
line 13why, one step back13} from "./common"Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
line 14why, one step back14feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 15why, one step back15function verifyMinified() {}Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
line 16why, one step back16feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 17–21why, one step back17const configDefaults = {18 useProxies: typeof Proxy !== "undefined" && typeof Reflect !== "undefined",19 autoFreeze:20 typeof process !== "undefined"21 ? process.env.NODE_ENV !== "production"feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
lines 22–25why, one step back22 : verifyMinified.name === "verifyMinified",23 onAssign: null,24 onDelete: null,25 onCopy: nullfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 26why, one step back26}Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
line 27why, one step back27feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 28–38why, one step back28export class Immer {29 constructor(config) {30 assign(this, configDefaults, config)31 this.setUseProxies(this.useProxies)32 this.produce = this.produce.bind(this)33 }34 produce(base, recipe, patchListener) {35 // curried invocation36 if (typeof base === "function" && typeof recipe !== "function") {37 const defaultBase = recipe38 recipe = baseMerge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
line 39why, one step back39feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 40–42why, one step back40 // prettier-ignore41 return (base = defaultBase, ...args) =>42 this.produce(base, draft => recipe.call(draft, draft, ...args))Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
lines 43–44why, one step back43 }44feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 45–49why, one step back45 // prettier-ignore46 {47 if (typeof recipe !== "function") throw new Error("if first argument is not a function, the second argument to produce should be a function")48 if (patchListener !== undefined && typeof patchListener !== "function") throw new Error("the third argument of a producer should not be set or a function")49 }Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
line 50why, one step back50feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 51–68why, one step back51 let result52 // Only create proxies for plain objects/arrays.53 if (!isDraftable(base)) {54 result = recipe(base)55 if (result === undefined) return base56 }57 // See #100, don't nest producers58 else if (isDraft(base)) {59 result = recipe.call(base, base)60 if (result === undefined) return base61 }62 // The given value must be proxied.63 else {64 this.scopes.push([])65 const baseDraft = this.createDraft(base)66 try {67 result = recipe.call(baseDraft, baseDraft)68 this.willFinalize(result, baseDraft, !!patchListener)nit: dedupe some codealeclarsonbefore adoptionc9e1d98
line 69why, one step back69feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 70–72why, one step back70 // Never generate patches when no listener exists.71 var patches = patchListener && [],72 inversePatches = patchListener && []Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
line 73why, one step back73feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 74–87why, one step back74 // Finalize the modified draft...75 if (result === undefined || result === baseDraft) {76 result = this.finalize(77 baseDraft,78 [],79 patches,80 inversePatches81 )82 }83 // ...or use a replacement value.84 else {85 // Users must never modify the draft _and_ return something else.86 if (baseDraft[DRAFT_STATE].modified)87 throw new Error("An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.") // prettier-ignore`produce` is now exported as both default and name export. Implements #136Michel Weststratebefore adoption05e3015
line 88why, one step back88feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 89–90why, one step back89 // Finalize the replacement in case it contains (or is) a subset of the draft.90 if (isDraftable(result)) result = this.finalize(result)Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2
line 91why, one step back91feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 92–135why, one step back92 if (patchListener) {93 patches.push({94 op: "replace",95 path: [],96 value: result97 })98 inversePatches.push({99 op: "replace",100 path: [],101 value: base102 })103 }104 }105 } finally {106 this.currentScope().forEach(state => state.revoke())107 this.scopes.pop()108 }109 patchListener && patchListener(patches, inversePatches)110 }111 // Normalize the result.112 return result === NOTHING ? undefined : result113 }114 setAutoFreeze(value) {115 this.autoFreeze = value116 }117 setUseProxies(value) {118 this.useProxies = value119 assign(this, value ? modernProxy : legacyProxy)120 }121 /**122 * @internal123 * Finalize a draft, returning either the unmodified base state or a modified124 * copy of the base state.125 */126 finalize(draft, path, patches, inversePatches) {127 const state = draft[DRAFT_STATE]128 if (!state) {129 if (Object.isFrozen(draft)) return draft130 return this.finalizeTree(draft)131 }132 if (!state.modified) return state.base133 if (!state.finalized) {134 state.finalized = true135 this.finalizeTree(state.draft, path, patches, inversePatches)feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
lines 136–141why, one step back136 if (this.onDelete) {137 const {assigned} = state138 for (const prop in assigned)139 assigned[prop] || this.onDelete(state, prop)140 }141 if (this.onCopy) this.onCopy(state)feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 142–157why, one step back142 if (this.autoFreeze) Object.freeze(state.copy)143 if (patches) generatePatches(state, path, patches, inversePatches)144 }145 return state.copy146 }147 /**148 * @internal149 * Finalize all drafts in the given state tree.150 */151 finalizeTree(root, path, patches, inversePatches) {152 const state = root[DRAFT_STATE]153 if (state) {154 root = this.useProxies155 ? state.copy156 : (state.copy = shallowCopy(state.draft))157 }feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
line 158why, one step back158 const {onAssign} = thisfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 159why, one step back159 const finalizeProperty = (prop, value, parent) => {feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
lines 160–175why, one step back160 // Only `root` can be a draft in here.161 const inDraft = state && parent === root162 if (isDraftable(value)) {163 // Skip unchanged properties in draft objects.164 if (inDraft && is(value, state.base[prop])) return165166 // Find proxies within new objects. Frozen objects are already finalized.167 if (!isDraft(value)) {168 if (!Object.isFrozen(value)) each(value, finalizeProperty)169 if (onAssign && inDraft) onAssign(state, prop, value)170 return171 }172173 // prettier-ignore174 parent[prop] =175 // Patches are never generated for assigned properties.perf(proxy): avoid `has` where possiblealeclarsonbefore adoption4b9b637
line 176why, one step back176 patches && parent === root && !(state && state.assigned[prop])feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
lines 177–181why, one step back177 ? this.finalize(value, path.concat(prop), patches, inversePatches)178 : this.finalize(value)179180 if (onAssign && inDraft) onAssign(state, prop, parent[prop])181 returnfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 182why, one step back182 }feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
lines 183–186why, one step back183 // Call `onAssign` for primitive values.184 if (onAssign && inDraft && !is(value, state.base[prop])) {185 onAssign(state, prop, value)186 }feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 187–191why, one step back187 }188 each(root, finalizeProperty)189 return root190 }191}