immer.js
This file
Path
src/immer.js
208 lines · 7.3 KiB · JavaScript
Last changed by
fix: draft as base statealeclarsonbefore adoption9a2e756
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–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 #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
lines 137–142why, one step back137 if (this.onDelete) {138 const {assigned} = state139 for (const prop in assigned)140 assigned[prop] || this.onDelete(state, prop)141 }142 if (this.onCopy) this.onCopy(state)feat: passing drafts to producealeclarsonbefore adoption9c53415
lines 143–149why, one step back143144 // Nested producers must never auto-freeze their result,145 // because it may contain drafts from parent producers.146 if (this.autoFreeze && this.scopes.length === 1) {147 Object.freeze(state.copy)148 }149feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 150–160why, one step back150 if (patches) generatePatches(state, path, patches, inversePatches)151 }152 return state.copy153 }154 /**155 * @internal156 * Finalize all drafts in the given state tree.157 */158 finalizeTree(root, path, patches, inversePatches) {159 const state = root[DRAFT_STATE]160 if (state) {feat: limited class supportaleclarsonbefore adoption9877d64
lines 161–166why, one step back161 if (!this.useProxies) {162 state.finalizing = true163 state.copy = shallowCopy(state.draft, true)164 state.finalizing = false165 }166 root = state.copyfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 167why, one step back167 }refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 168why, one step back168feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
line 169why, one step back169 const {onAssign} = thisfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 170why, one step back170 const finalizeProperty = (prop, value, parent) => {feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
line 171why, one step back171 // Only `root` can be a draft in here.refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 172why, one step back172 const inDraft = !!state && parent === rootfeat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
line 173why, one step back173refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 174why, one step back174 if (isDraft(value)) {feat: limited class supportaleclarsonbefore adoption9877d64
line 175why, one step back175 value =feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
line 176why, one step back176 // Patches are never generated for assigned properties.refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 177why, one step back177 patches && inDraft && !state.assigned[prop]feat: limited class supportaleclarsonbefore adoption9877d64
line 178why, one step back178 ? this.finalize(value, path.concat(prop), patches, inversePatches) // prettier-ignorefeat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
line 179why, one step back179 : this.finalize(value)feat: passing drafts to producealeclarsonbefore adoption9c53415
line 180why, one step back180feat: limited class supportaleclarsonbefore adoption9877d64
lines 181–188why, one step back181 // Preserve non-enumerable properties.182 if (Array.isArray(parent) || isEnumerable(parent, prop)) {183 parent[prop] = value184 } else {185 Object.defineProperty(parent, prop, {value})186 }187188 // Unchanged drafts are never passed to the `onAssign` hook.feat: passing drafts to producealeclarsonbefore adoption9c53415
line 189why, one step back189 if (inDraft && value === state.base[prop]) returnrefactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 190why, one step back190 }feat: passing drafts to producealeclarsonbefore adoption9c53415
line 191why, one step back191 // Unchanged draft properties are ignored.refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 192why, one step back192 else if (inDraft && is(value, state.base[prop])) {feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
line 193why, one step back193 returnfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 194why, one step back194 }refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
lines 195–196why, one step back195 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.196 else if (isDraftable(value) && !Object.isFrozen(value)) {feat: limited class supportaleclarsonbefore adoption9877d64
line 197why, one step back197 eachOwn(value, finalizeProperty)refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
lines 198–200why, one step back198 }199200 if (inDraft && onAssign) {feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
lines 201–202why, one step back201 onAssign(state, prop, value)202 }feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 203why, one step back203 }refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 204why, one step back204feat: limited class supportaleclarsonbefore adoption9877d64
line 205why, one step back205 eachOwn(root, finalizeProperty)feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 206–208why, one step back206 return root207 }208}