immer.js
This file
Path
src/immer.js
213 lines · 7.5 KiB · JavaScript
Last changed by
feat: limited class supportaleclarsonbefore adoption9877d64
History of this fileTake it
The walk stopped after 200 commits: lines older than 2cd32e0 point at it.
feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 1–11why, 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,feat: limited class supportaleclarsonbefore adoption9877d64
lines 12–14why, one step back12 NOTHING,13 isEnumerable,14 eachOwnMerge remote-tracking branch 'origin/v1.8'Michel Weststratebefore adoption7790417
line 15why, one step back15} from "./common"Merge pull request #131 from dmorosinotto/patch_dtsMichel Weststratebefore adoption2cd32e0
line 16why, one step back16feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 17why, one step back17function verifyMinified() {}Merge pull request #131 from dmorosinotto/patch_dtsMichel Weststratebefore adoption2cd32e0
line 18why, one step back18feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 19–23why, one step back19const configDefaults = {20 useProxies: typeof Proxy !== "undefined" && typeof Reflect !== "undefined",21 autoFreeze:22 typeof process !== "undefined"23 ? process.env.NODE_ENV !== "production"feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
lines 24–27why, one step back24 : verifyMinified.name === "verifyMinified",25 onAssign: null,26 onDelete: null,27 onCopy: nullfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 28why, one step back28}Merge pull request #131 from dmorosinotto/patch_dtsMichel Weststratebefore adoption2cd32e0
line 29why, one step back29feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 30–40why, one step back30export class Immer {31 constructor(config) {32 assign(this, configDefaults, config)33 this.setUseProxies(this.useProxies)34 this.produce = this.produce.bind(this)35 }36 produce(base, recipe, patchListener) {37 // curried invocation38 if (typeof base === "function" && typeof recipe !== "function") {39 const defaultBase = recipe40 recipe = baseMerge pull request #131 from dmorosinotto/patch_dtsMichel Weststratebefore adoption2cd32e0
line 41why, one step back41feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 42–44why, one step back42 // prettier-ignore43 return (base = defaultBase, ...args) =>44 this.produce(base, draft => recipe.call(draft, draft, ...args))Merge pull request #131 from dmorosinotto/patch_dtsMichel Weststratebefore adoption2cd32e0
lines 45–46why, one step back45 }46feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 47–51why, one step back47 // prettier-ignore48 {49 if (typeof recipe !== "function") throw new Error("if first argument is not a function, the second argument to produce should be a function")50 if (patchListener !== undefined && typeof patchListener !== "function") throw new Error("the third argument of a producer should not be set or a function")51 }Merge pull request #131 from dmorosinotto/patch_dtsMichel Weststratebefore adoption2cd32e0
line 52why, one step back52feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 53–70why, one step back53 let result54 // Only create proxies for plain objects/arrays.55 if (!isDraftable(base)) {56 result = recipe(base)57 if (result === undefined) return base58 }59 // See #100, don't nest producers60 else if (isDraft(base)) {61 result = recipe.call(base, base)62 if (result === undefined) return base63 }64 // The given value must be proxied.65 else {66 this.scopes.push([])67 const baseDraft = this.createDraft(base)68 try {69 result = recipe.call(baseDraft, baseDraft)70 this.willFinalize(result, baseDraft, !!patchListener)nit: dedupe some codealeclarsonbefore adoptionc9e1d98
line 71why, one step back71feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 72–74why, one step back72 // Never generate patches when no listener exists.73 var patches = patchListener && [],74 inversePatches = patchListener && []Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
line 75why, one step back75feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 76–89why, one step back76 // Finalize the modified draft...77 if (result === undefined || result === baseDraft) {78 result = this.finalize(79 baseDraft,80 [],81 patches,82 inversePatches83 )84 }85 // ...or use a replacement value.86 else {87 // Users must never modify the draft _and_ return something else.88 if (baseDraft[DRAFT_STATE].modified)89 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 90why, one step back90feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 91–92why, one step back91 // Finalize the replacement in case it contains (or is) a subset of the draft.92 if (isDraftable(result)) result = this.finalize(result)Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2
line 93why, one step back93feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 94–133why, one step back94 if (patchListener) {95 patches.push({96 op: "replace",97 path: [],98 value: result99 })100 inversePatches.push({101 op: "replace",102 path: [],103 value: base104 })105 }106 }107 } finally {108 this.currentScope().forEach(state => state.revoke())109 this.scopes.pop()110 }111 patchListener && patchListener(patches, inversePatches)112 }113 // Normalize the result.114 return result === NOTHING ? undefined : result115 }116 setAutoFreeze(value) {117 this.autoFreeze = value118 }119 setUseProxies(value) {120 this.useProxies = value121 assign(this, value ? modernProxy : legacyProxy)122 }123 /**124 * @internal125 * Finalize a draft, returning either the unmodified base state or a modified126 * copy of the base state.127 */128 finalize(draft, path, patches, inversePatches) {129 const state = draft[DRAFT_STATE]130 if (!state) {131 if (Object.isFrozen(draft)) return draft132 return this.finalizeTree(draft)133 }feat: passing drafts to producealeclarsonbefore adoption9c53415
lines 134–137why, one step back134 // Never finalize drafts owned by an outer scope.135 if (state.scope !== this.currentScope()) {136 return draft137 }feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 138–141why, one step back138 if (!state.modified) return state.base139 if (!state.finalized) {140 state.finalized = true141 this.finalizeTree(state.draft, path, patches, inversePatches)feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
lines 142–147why, one step back142 if (this.onDelete) {143 const {assigned} = state144 for (const prop in assigned)145 assigned[prop] || this.onDelete(state, prop)146 }147 if (this.onCopy) this.onCopy(state)feat: passing drafts to producealeclarsonbefore adoption9c53415
lines 148–154why, one step back148149 // Nested producers must never auto-freeze their result,150 // because it may contain drafts from parent producers.151 if (this.autoFreeze && this.scopes.length === 1) {152 Object.freeze(state.copy)153 }154feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 155–165why, one step back155 if (patches) generatePatches(state, path, patches, inversePatches)156 }157 return state.copy158 }159 /**160 * @internal161 * Finalize all drafts in the given state tree.162 */163 finalizeTree(root, path, patches, inversePatches) {164 const state = root[DRAFT_STATE]165 if (state) {feat: limited class supportaleclarsonbefore adoption9877d64
lines 166–171why, one step back166 if (!this.useProxies) {167 state.finalizing = true168 state.copy = shallowCopy(state.draft, true)169 state.finalizing = false170 }171 root = state.copyfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 172why, one step back172 }refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 173why, one step back173feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
line 174why, one step back174 const {onAssign} = thisfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 175why, one step back175 const finalizeProperty = (prop, value, parent) => {feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
line 176why, one step back176 // Only `root` can be a draft in here.refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 177why, one step back177 const inDraft = !!state && parent === rootfeat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
line 178why, one step back178refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 179why, one step back179 if (isDraft(value)) {feat: limited class supportaleclarsonbefore adoption9877d64
line 180why, one step back180 value =feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
line 181why, one step back181 // Patches are never generated for assigned properties.refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 182why, one step back182 patches && inDraft && !state.assigned[prop]feat: limited class supportaleclarsonbefore adoption9877d64
line 183why, one step back183 ? this.finalize(value, path.concat(prop), patches, inversePatches) // prettier-ignorefeat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
line 184why, one step back184 : this.finalize(value)feat: passing drafts to producealeclarsonbefore adoption9c53415
line 185why, one step back185feat: limited class supportaleclarsonbefore adoption9877d64
lines 186–193why, one step back186 // Preserve non-enumerable properties.187 if (Array.isArray(parent) || isEnumerable(parent, prop)) {188 parent[prop] = value189 } else {190 Object.defineProperty(parent, prop, {value})191 }192193 // Unchanged drafts are never passed to the `onAssign` hook.feat: passing drafts to producealeclarsonbefore adoption9c53415
line 194why, one step back194 if (inDraft && value === state.base[prop]) returnrefactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 195why, one step back195 }feat: passing drafts to producealeclarsonbefore adoption9c53415
line 196why, one step back196 // Unchanged draft properties are ignored.refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 197why, one step back197 else if (inDraft && is(value, state.base[prop])) {feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
line 198why, one step back198 returnfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 199why, one step back199 }refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
lines 200–201why, one step back200 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.201 else if (isDraftable(value) && !Object.isFrozen(value)) {feat: limited class supportaleclarsonbefore adoption9877d64
line 202why, one step back202 eachOwn(value, finalizeProperty)refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
lines 203–205why, one step back203 }204205 if (inDraft && onAssign) {feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
lines 206–207why, one step back206 onAssign(state, prop, value)207 }feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 208why, one step back208 }refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 209why, one step back209feat: limited class supportaleclarsonbefore adoption9877d64
line 210why, one step back210 eachOwn(root, finalizeProperty)feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 211–213why, one step back211 return root212 }213}