immer.js
This file
Path
src/immer.js
221 lines · 7.8 KiB · JavaScript
Last changed by
nit: remove old `each` and rename `eachOwn`aleclarsonbefore adoptionc0ea419
History of this fileTake it
The walk stopped after 200 commits: lines older than 427308e point at it.
feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 1–6why, one step back1import * as legacyProxy from "./es5"2import * as modernProxy from "./proxy"3import {generatePatches} from "./patches"4import {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 #139 from celebro/fix/remove-object-assign-for-IEMichel Weststratebefore adoption427308e
line 16why, one step back16feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 17why, one step back17function verifyMinified() {}Merge pull request #139 from celebro/fix/remove-object-assign-for-IEMichel Weststratebefore adoption427308e
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 #139 from celebro/fix/remove-object-assign-for-IEMichel Weststratebefore adoption427308e
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 #139 from celebro/fix/remove-object-assign-for-IEMichel Weststratebefore adoption427308e
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 #139 from celebro/fix/remove-object-assign-for-IEMichel Weststratebefore adoption427308e
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 #139 from celebro/fix/remove-object-assign-for-IEMichel Weststratebefore adoption427308e
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–128why, 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)117 }118 /**119 * @internal120 * Finalize a draft, returning either the unmodified base state or a modified121 * copy of the base state.122 */123 finalize(draft, path, patches, inversePatches) {124 const state = draft[DRAFT_STATE]125 if (!state) {126 if (Object.isFrozen(draft)) return draft127 return this.finalizeTree(draft)128 }feat: passing drafts to producealeclarsonbefore adoption9c53415
lines 129–132why, one step back129 // Never finalize drafts owned by an outer scope.130 if (state.scope !== this.currentScope()) {131 return draft132 }feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 133–136why, one step back133 if (!state.modified) return state.base134 if (!state.finalized) {135 state.finalized = true136 this.finalizeTree(state.draft, path, patches, inversePatches)feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
line 137why, one step back137 if (this.onDelete) {fix(es5): onDelete hookaleclarsonbefore adoption46cc6a1
lines 138–145why, one step back138 // The `assigned` object is unreliable with ES5 drafts.139 if (this.useProxies) {140 const {assigned} = state141 for (const prop in assigned) {142 if (!assigned[prop]) this.onDelete(state, prop)143 }144 } else {145 const {base, copy} = statenit: remove old `each` and rename `eachOwn`aleclarsonbefore adoptionc0ea419
line 146why, one step back146 each(base, prop => {fix(es5): onDelete hookaleclarsonbefore adoption46cc6a1
lines 147–149why, one step back147 if (!has(copy, prop)) this.onDelete(state, prop)148 })149 }feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
lines 150–151why, one step back150 }151 if (this.onCopy) this.onCopy(state)feat: passing drafts to producealeclarsonbefore adoption9c53415
lines 152–158why, one step back152153 // Nested producers must never auto-freeze their result,154 // because it may contain drafts from parent producers.155 if (this.autoFreeze && this.scopes.length === 1) {156 Object.freeze(state.copy)157 }158feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 159–169why, one step back159 if (patches) generatePatches(state, path, patches, inversePatches)160 }161 return state.copy162 }163 /**164 * @internal165 * Finalize all drafts in the given state tree.166 */167 finalizeTree(root, path, patches, inversePatches) {168 const state = root[DRAFT_STATE]169 if (state) {feat: limited class supportaleclarsonbefore adoption9877d64
lines 170–175why, one step back170 if (!this.useProxies) {171 state.finalizing = true172 state.copy = shallowCopy(state.draft, true)173 state.finalizing = false174 }175 root = state.copyfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 176why, one step back176 }refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 177why, one step back177feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
line 178why, one step back178 const {onAssign} = thisfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 179why, one step back179 const finalizeProperty = (prop, value, parent) => {fix: throw on circular referencesaleclarsonbefore adoption9c6603e
lines 180–184why, one step back180 if (value === parent) {181 throw Error("Immer forbids circular references")182 }183184 // The only possible draft (in the scope of a `finalizeTree` call) is the `root` object.refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 185why, one step back185 const inDraft = !!state && parent === rootfeat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
line 186why, one step back186refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 187why, one step back187 if (isDraft(value)) {feat: limited class supportaleclarsonbefore adoption9877d64
line 188why, one step back188 value =feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
line 189why, one step back189 // Patches are never generated for assigned properties.refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 190why, one step back190 patches && inDraft && !state.assigned[prop]feat: limited class supportaleclarsonbefore adoption9877d64
line 191why, one step back191 ? this.finalize(value, path.concat(prop), patches, inversePatches) // prettier-ignorefeat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
line 192why, one step back192 : this.finalize(value)feat: passing drafts to producealeclarsonbefore adoption9c53415
line 193why, one step back193feat: limited class supportaleclarsonbefore adoption9877d64
lines 194–201why, one step back194 // Preserve non-enumerable properties.195 if (Array.isArray(parent) || isEnumerable(parent, prop)) {196 parent[prop] = value197 } else {198 Object.defineProperty(parent, prop, {value})199 }200201 // Unchanged drafts are never passed to the `onAssign` hook.feat: passing drafts to producealeclarsonbefore adoption9c53415
line 202why, one step back202 if (inDraft && value === state.base[prop]) returnrefactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 203why, one step back203 }feat: passing drafts to producealeclarsonbefore adoption9c53415
line 204why, one step back204 // Unchanged draft properties are ignored.refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 205why, one step back205 else if (inDraft && is(value, state.base[prop])) {feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
line 206why, one step back206 returnfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 207why, one step back207 }refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
lines 208–209why, one step back208 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.209 else if (isDraftable(value) && !Object.isFrozen(value)) {nit: remove old `each` and rename `eachOwn`aleclarsonbefore adoptionc0ea419
line 210why, one step back210 each(value, finalizeProperty)refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
lines 211–213why, one step back211 }212213 if (inDraft && onAssign) {feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
lines 214–215why, one step back214 onAssign(state, prop, value)215 }feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 216why, one step back216 }refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 217why, one step back217nit: remove old `each` and rename `eachOwn`aleclarsonbefore adoptionc0ea419
line 218why, one step back218 each(root, finalizeProperty)feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 219–221why, one step back219 return root220 }221}