immer.js
This file
Path
src/immer.js
256 lines · 8.4 KiB · JavaScript
Last changed by
feat: support async recipesaleclarsonbefore adoption5fee518
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"feat: support async recipesaleclarsonbefore adoption5fee518
line 16why, one step back16import {ImmerScope} from "./scope"Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
line 17why, one step back17feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 18why, one step back18function verifyMinified() {}Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
line 19why, one step back19feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 20–24why, one step back20const configDefaults = {21 useProxies: typeof Proxy !== "undefined" && typeof Reflect !== "undefined",22 autoFreeze:23 typeof process !== "undefined"24 ? process.env.NODE_ENV !== "production"feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
lines 25–28why, one step back25 : verifyMinified.name === "verifyMinified",26 onAssign: null,27 onDelete: null,28 onCopy: nullfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 29why, one step back29}Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
line 30why, one step back30feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 31–41why, one step back31export class Immer {32 constructor(config) {33 assign(this, configDefaults, config)34 this.setUseProxies(this.useProxies)35 this.produce = this.produce.bind(this)36 }37 produce(base, recipe, patchListener) {38 // curried invocation39 if (typeof base === "function" && typeof recipe !== "function") {40 const defaultBase = recipe41 recipe = baseMerge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
line 42why, one step back42feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 43–45why, one step back43 // prettier-ignore44 return (base = defaultBase, ...args) =>45 this.produce(base, draft => recipe.call(draft, draft, ...args))Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
lines 46–47why, one step back46 }47feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 48–52why, one step back48 // prettier-ignore49 {50 if (typeof recipe !== "function") throw new Error("if first argument is not a function, the second argument to produce should be a function")51 if (patchListener !== undefined && typeof patchListener !== "function") throw new Error("the third argument of a producer should not be set or a function")52 }Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
line 53why, one step back53feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 54why, one step back54 let resultfeat: support async recipesaleclarsonbefore adoption5fee518
lines 55–58why, one step back5556 // Only plain objects, arrays, and "immerable classes" are drafted.57 if (isDraftable(base)) {58 const scope = ImmerScope.enter()feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 59–61why, one step back59 const baseDraft = this.createDraft(base)60 try {61 result = recipe.call(baseDraft, baseDraft)feat: support async recipesaleclarsonbefore adoption5fee518
lines 62–75why, one step back62 } catch (error) {63 scope.revoke()64 throw error65 }66 scope.leave()67 if (result instanceof Promise) {68 return result.then(69 result => {70 scope.usePatches(patchListener)71 return this.processResult(result, scope)72 },73 error => {74 scope.revoke()75 throw errorfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 76why, one step back76 }feat: support async recipesaleclarsonbefore adoption5fee518
line 77why, one step back77 )feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 78why, one step back78 }feat: support async recipesaleclarsonbefore adoption5fee518
lines 79–84why, one step back79 scope.usePatches(patchListener)80 return this.processResult(result, scope)81 } else {82 result = recipe(base)83 if (result === undefined) return base84 return result !== NOTHING ? result : undefinedfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 85–92why, one step back85 }86 }87 setAutoFreeze(value) {88 this.autoFreeze = value89 }90 setUseProxies(value) {91 this.useProxies = value92 assign(this, value ? modernProxy : legacyProxy)fix: applyPatches should mutate draftsaleclarsonbefore adoptiond41be77
lines 93–100why, one step back93 }94 applyPatches(base, patches) {95 // Mutate the base state when a draft is passed.96 if (isDraft(base)) {97 return applyPatches(base, patches)98 }99 // Otherwise, produce a copy of the base state.100 return this.produce(base, draft => applyPatches(draft, patches))feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 101why, one step back101 }feat: support async recipesaleclarsonbefore adoption5fee518
lines 102–137why, one step back102 /** @internal */103 processResult(result, scope) {104 const baseDraft = scope.drafts[0]105 const isReplaced = result !== undefined && result !== baseDraft106 this.willFinalize(scope, result, isReplaced)107 if (isReplaced) {108 if (baseDraft[DRAFT_STATE].modified) {109 scope.revoke()110 throw new Error("An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.") // prettier-ignore111 }112 if (isDraftable(result)) {113 // Finalize the result in case it contains (or is) a subset of the draft.114 result = this.finalize(result, null, scope)115 }116 if (scope.patches) {117 scope.patches.push({118 op: "replace",119 path: [],120 value: result121 })122 scope.inversePatches.push({123 op: "replace",124 path: [],125 value: baseDraft[DRAFT_STATE].base126 })127 }128 } else {129 // Finalize the base draft.130 result = this.finalize(baseDraft, [], scope)131 }132 scope.revoke()133 if (scope.patches) {134 scope.patchListener(scope.patches, scope.inversePatches)135 }136 return result !== NOTHING ? result : undefined137 }feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 138–142why, one step back138 /**139 * @internal140 * Finalize a draft, returning either the unmodified base state or a modified141 * copy of the base state.142 */feat: support async recipesaleclarsonbefore adoption5fee518
line 143why, one step back143 finalize(draft, path, scope) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 144–146why, one step back144 const state = draft[DRAFT_STATE]145 if (!state) {146 if (Object.isFrozen(draft)) return draftfeat: support async recipesaleclarsonbefore adoption5fee518
line 147why, one step back147 return this.finalizeTree(draft, null, scope)feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 148why, one step back148 }feat: support async recipesaleclarsonbefore adoption5fee518
lines 149–150why, one step back149 // Never finalize drafts owned by another scope.150 if (state.scope !== scope) {feat: passing drafts to producealeclarsonbefore adoption9c53415
lines 151–152why, one step back151 return draft152 }feat: support async recipesaleclarsonbefore adoption5fee518
lines 153–155why, one step back153 if (!state.modified) {154 return state.base155 }feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 156–157why, one step back156 if (!state.finalized) {157 state.finalized = truefeat: support async recipesaleclarsonbefore adoption5fee518
lines 158–159why, one step back158 this.finalizeTree(state.draft, path, scope)159feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
line 160why, one step back160 if (this.onDelete) {fix(es5): onDelete hookaleclarsonbefore adoption46cc6a1
lines 161–168why, one step back161 // The `assigned` object is unreliable with ES5 drafts.162 if (this.useProxies) {163 const {assigned} = state164 for (const prop in assigned) {165 if (!assigned[prop]) this.onDelete(state, prop)166 }167 } else {168 const {base, copy} = statenit: remove old `each` and rename `eachOwn`aleclarsonbefore adoptionc0ea419
line 169why, one step back169 each(base, prop => {fix(es5): onDelete hookaleclarsonbefore adoption46cc6a1
lines 170–172why, one step back170 if (!has(copy, prop)) this.onDelete(state, prop)171 })172 }feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
line 173why, one step back173 }feat: support async recipesaleclarsonbefore adoption5fee518
lines 174–176why, one step back174 if (this.onCopy) {175 this.onCopy(state)176 }feat: passing drafts to producealeclarsonbefore adoption9c53415
lines 177–179why, one step back177178 // Nested producers must never auto-freeze their result,179 // because it may contain drafts from parent producers.feat: support async recipesaleclarsonbefore adoption5fee518
line 180why, one step back180 if (this.autoFreeze && !scope.parent) {feat: passing drafts to producealeclarsonbefore adoption9c53415
lines 181–183why, one step back181 Object.freeze(state.copy)182 }183feat: support async recipesaleclarsonbefore adoption5fee518
lines 184–191why, one step back184 if (path && scope.patches) {185 generatePatches(186 state,187 path,188 scope.patches,189 scope.inversePatches190 )191 }feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 192–198why, one step back192 }193 return state.copy194 }195 /**196 * @internal197 * Finalize all drafts in the given state tree.198 */feat: support async recipesaleclarsonbefore adoption5fee518
line 199why, one step back199 finalizeTree(root, path, scope) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 200–201why, one step back200 const state = root[DRAFT_STATE]201 if (state) {feat: limited class supportaleclarsonbefore adoption9877d64
lines 202–207why, one step back202 if (!this.useProxies) {203 state.finalizing = true204 state.copy = shallowCopy(state.draft, true)205 state.finalizing = false206 }207 root = state.copyfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 208why, one step back208 }refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 209why, one step back209feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
line 210why, one step back210 const {onAssign} = thisfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 211why, one step back211 const finalizeProperty = (prop, value, parent) => {fix: throw on circular referencesaleclarsonbefore adoption9c6603e
lines 212–216why, one step back212 if (value === parent) {213 throw Error("Immer forbids circular references")214 }215216 // The only possible draft (in the scope of a `finalizeTree` call) is the `root` object.refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 217why, one step back217 const inDraft = !!state && parent === rootfeat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
line 218why, one step back218refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 219why, one step back219 if (isDraft(value)) {feat: support async recipesaleclarsonbefore adoption5fee518
lines 220–227why, one step back220 const needPatches =221 inDraft && path && scope.patches && !state.assigned[prop]222223 value = this.finalize(224 value,225 needPatches ? path.concat(prop) : null,226 scope227 )feat: passing drafts to producealeclarsonbefore adoption9c53415
line 228why, one step back228feat: limited class supportaleclarsonbefore adoption9877d64
lines 229–236why, one step back229 // Preserve non-enumerable properties.230 if (Array.isArray(parent) || isEnumerable(parent, prop)) {231 parent[prop] = value232 } else {233 Object.defineProperty(parent, prop, {value})234 }235236 // Unchanged drafts are never passed to the `onAssign` hook.feat: passing drafts to producealeclarsonbefore adoption9c53415
line 237why, one step back237 if (inDraft && value === state.base[prop]) returnrefactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 238why, one step back238 }feat: passing drafts to producealeclarsonbefore adoption9c53415
line 239why, one step back239 // Unchanged draft properties are ignored.refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 240why, one step back240 else if (inDraft && is(value, state.base[prop])) {feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
line 241why, one step back241 returnfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 242why, one step back242 }refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
lines 243–244why, one step back243 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.244 else if (isDraftable(value) && !Object.isFrozen(value)) {nit: remove old `each` and rename `eachOwn`aleclarsonbefore adoptionc0ea419
line 245why, one step back245 each(value, finalizeProperty)refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
lines 246–248why, one step back246 }247248 if (inDraft && onAssign) {feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
lines 249–250why, one step back249 onAssign(state, prop, value)250 }feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 251why, one step back251 }refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 252why, one step back252nit: remove old `each` and rename `eachOwn`aleclarsonbefore adoptionc0ea419
line 253why, one step back253 each(root, finalizeProperty)feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 254–256why, one step back254 return root255 }256}