immer.js
This file
Path
src/immer.js
229 lines · 8.1 KiB · JavaScript
Last changed by
fix: applyPatches should mutate draftsaleclarsonbefore adoptiond41be77
History of this fileTake it
The walk stopped after 200 commits: lines older than d87d637 point at it.
feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 1–2why, one step back1import * as legacyProxy from "./es5"2import * as modernProxy from "./proxy"fix: applyPatches should mutate draftsaleclarsonbefore adoptiond41be77
line 3why, one step back3import {applyPatches, generatePatches} from "./patches"feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 4–6why, one step back4import {5 assign,6 each,fix(es5): onDelete hookaleclarsonbefore adoption46cc6a1
line 7why, one step back7 has,feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 8–10why, one step back8 is,9 isDraft,10 isDraftable,nit: remove old `each` and rename `eachOwn`aleclarsonbefore adoptionc0ea419
line 11why, one step back11 isEnumerable,feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 12–13why, one step back12 shallowCopy,13 DRAFT_STATE,nit: remove old `each` and rename `eachOwn`aleclarsonbefore adoptionc0ea419
line 14why, one step back14 NOTHINGMerge remote-tracking branch 'origin/v1.8'Michel Weststratebefore adoption7790417
line 15why, one step back15} from "./common"Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
line 16why, one step back16feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 17why, one step back17function verifyMinified() {}Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
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 #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
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 #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
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 #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
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 #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
line 52why, one step back52feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 53–65why, 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 // The given value must be proxied.60 else {61 this.scopes.push([])62 const baseDraft = this.createDraft(base)63 try {64 result = recipe.call(baseDraft, baseDraft)65 this.willFinalize(result, baseDraft, !!patchListener)nit: dedupe some codealeclarsonbefore adoptionc9e1d98
line 66why, one step back66feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 67–69why, one step back67 // Never generate patches when no listener exists.68 var patches = patchListener && [],69 inversePatches = patchListener && []Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
line 70why, one step back70feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 71–84why, one step back71 // Finalize the modified draft...72 if (result === undefined || result === baseDraft) {73 result = this.finalize(74 baseDraft,75 [],76 patches,77 inversePatches78 )79 }80 // ...or use a replacement value.81 else {82 // Users must never modify the draft _and_ return something else.83 if (baseDraft[DRAFT_STATE].modified)84 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 85why, one step back85feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 86–87why, one step back86 // Finalize the replacement in case it contains (or is) a subset of the draft.87 if (isDraftable(result)) result = this.finalize(result)Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2
line 88why, one step back88feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 89–116why, one step back89 if (patchListener) {90 patches.push({91 op: "replace",92 path: [],93 value: result94 })95 inversePatches.push({96 op: "replace",97 path: [],98 value: base99 })100 }101 }102 } finally {103 this.currentScope().forEach(state => state.revoke())104 this.scopes.pop()105 }106 patchListener && patchListener(patches, inversePatches)107 }108 // Normalize the result.109 return result === NOTHING ? undefined : result110 }111 setAutoFreeze(value) {112 this.autoFreeze = value113 }114 setUseProxies(value) {115 this.useProxies = value116 assign(this, value ? modernProxy : legacyProxy)fix: applyPatches should mutate draftsaleclarsonbefore adoptiond41be77
lines 117–124why, one step back117 }118 applyPatches(base, patches) {119 // Mutate the base state when a draft is passed.120 if (isDraft(base)) {121 return applyPatches(base, patches)122 }123 // Otherwise, produce a copy of the base state.124 return this.produce(base, draft => applyPatches(draft, patches))feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 125–136why, one step back125 }126 /**127 * @internal128 * Finalize a draft, returning either the unmodified base state or a modified129 * copy of the base state.130 */131 finalize(draft, path, patches, inversePatches) {132 const state = draft[DRAFT_STATE]133 if (!state) {134 if (Object.isFrozen(draft)) return draft135 return this.finalizeTree(draft)136 }feat: passing drafts to producealeclarsonbefore adoption9c53415
lines 137–140why, one step back137 // Never finalize drafts owned by an outer scope.138 if (state.scope !== this.currentScope()) {139 return draft140 }feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 141–144why, one step back141 if (!state.modified) return state.base142 if (!state.finalized) {143 state.finalized = true144 this.finalizeTree(state.draft, path, patches, inversePatches)feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
line 145why, one step back145 if (this.onDelete) {fix(es5): onDelete hookaleclarsonbefore adoption46cc6a1
lines 146–153why, one step back146 // The `assigned` object is unreliable with ES5 drafts.147 if (this.useProxies) {148 const {assigned} = state149 for (const prop in assigned) {150 if (!assigned[prop]) this.onDelete(state, prop)151 }152 } else {153 const {base, copy} = statenit: remove old `each` and rename `eachOwn`aleclarsonbefore adoptionc0ea419
line 154why, one step back154 each(base, prop => {fix(es5): onDelete hookaleclarsonbefore adoption46cc6a1
lines 155–157why, one step back155 if (!has(copy, prop)) this.onDelete(state, prop)156 })157 }feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
lines 158–159why, one step back158 }159 if (this.onCopy) this.onCopy(state)feat: passing drafts to producealeclarsonbefore adoption9c53415
lines 160–166why, one step back160161 // Nested producers must never auto-freeze their result,162 // because it may contain drafts from parent producers.163 if (this.autoFreeze && this.scopes.length === 1) {164 Object.freeze(state.copy)165 }166feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 167–177why, one step back167 if (patches) generatePatches(state, path, patches, inversePatches)168 }169 return state.copy170 }171 /**172 * @internal173 * Finalize all drafts in the given state tree.174 */175 finalizeTree(root, path, patches, inversePatches) {176 const state = root[DRAFT_STATE]177 if (state) {feat: limited class supportaleclarsonbefore adoption9877d64
lines 178–183why, one step back178 if (!this.useProxies) {179 state.finalizing = true180 state.copy = shallowCopy(state.draft, true)181 state.finalizing = false182 }183 root = state.copyfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 184why, one step back184 }refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 185why, one step back185feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
line 186why, one step back186 const {onAssign} = thisfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 187why, one step back187 const finalizeProperty = (prop, value, parent) => {fix: throw on circular referencesaleclarsonbefore adoption9c6603e
lines 188–192why, one step back188 if (value === parent) {189 throw Error("Immer forbids circular references")190 }191192 // The only possible draft (in the scope of a `finalizeTree` call) is the `root` object.refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 193why, one step back193 const inDraft = !!state && parent === rootfeat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
line 194why, one step back194refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 195why, one step back195 if (isDraft(value)) {feat: limited class supportaleclarsonbefore adoption9877d64
line 196why, one step back196 value =feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
line 197why, one step back197 // Patches are never generated for assigned properties.refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 198why, one step back198 patches && inDraft && !state.assigned[prop]feat: limited class supportaleclarsonbefore adoption9877d64
line 199why, one step back199 ? this.finalize(value, path.concat(prop), patches, inversePatches) // prettier-ignorefeat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
line 200why, one step back200 : this.finalize(value)feat: passing drafts to producealeclarsonbefore adoption9c53415
line 201why, one step back201feat: limited class supportaleclarsonbefore adoption9877d64
lines 202–209why, one step back202 // Preserve non-enumerable properties.203 if (Array.isArray(parent) || isEnumerable(parent, prop)) {204 parent[prop] = value205 } else {206 Object.defineProperty(parent, prop, {value})207 }208209 // Unchanged drafts are never passed to the `onAssign` hook.feat: passing drafts to producealeclarsonbefore adoption9c53415
line 210why, one step back210 if (inDraft && value === state.base[prop]) returnrefactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 211why, one step back211 }feat: passing drafts to producealeclarsonbefore adoption9c53415
line 212why, one step back212 // Unchanged draft properties are ignored.refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 213why, one step back213 else if (inDraft && is(value, state.base[prop])) {feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
line 214why, one step back214 returnfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 215why, one step back215 }refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
lines 216–217why, one step back216 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.217 else if (isDraftable(value) && !Object.isFrozen(value)) {nit: remove old `each` and rename `eachOwn`aleclarsonbefore adoptionc0ea419
line 218why, one step back218 each(value, finalizeProperty)refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
lines 219–221why, one step back219 }220221 if (inDraft && onAssign) {feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
lines 222–223why, one step back222 onAssign(state, prop, value)223 }feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 224why, one step back224 }refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 225why, one step back225nit: remove old `each` and rename `eachOwn`aleclarsonbefore adoptionc0ea419
line 226why, one step back226 each(root, finalizeProperty)feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 227–229why, one step back227 return root228 }229}