immer.js
This file
Path
src/immer.js
202 lines · 7.0 KiB · JavaScript
Last changed by
feat: passing drafts to producealeclarsonbefore adoption9c53415
History of this fileTake it
The walk stopped after 200 commits: lines older than 2cb5496 point at it.
feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 1–12why, 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,12 NOTHINGMerge remote-tracking branch 'origin/v1.8'Michel Weststratebefore adoption7790417
line 13why, one step back13} from "./common"Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
line 14why, one step back14feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 15why, one step back15function verifyMinified() {}Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
line 16why, one step back16feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 17–21why, one step back17const configDefaults = {18 useProxies: typeof Proxy !== "undefined" && typeof Reflect !== "undefined",19 autoFreeze:20 typeof process !== "undefined"21 ? process.env.NODE_ENV !== "production"feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
lines 22–25why, one step back22 : verifyMinified.name === "verifyMinified",23 onAssign: null,24 onDelete: null,25 onCopy: nullfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 26why, one step back26}Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
line 27why, one step back27feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 28–38why, one step back28export class Immer {29 constructor(config) {30 assign(this, configDefaults, config)31 this.setUseProxies(this.useProxies)32 this.produce = this.produce.bind(this)33 }34 produce(base, recipe, patchListener) {35 // curried invocation36 if (typeof base === "function" && typeof recipe !== "function") {37 const defaultBase = recipe38 recipe = baseMerge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
line 39why, one step back39feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 40–42why, one step back40 // prettier-ignore41 return (base = defaultBase, ...args) =>42 this.produce(base, draft => recipe.call(draft, draft, ...args))Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 43–44why, one step back43 }44feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 45–49why, one step back45 // prettier-ignore46 {47 if (typeof recipe !== "function") throw new Error("if first argument is not a function, the second argument to produce should be a function")48 if (patchListener !== undefined && typeof patchListener !== "function") throw new Error("the third argument of a producer should not be set or a function")49 }Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
line 50why, one step back50feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 51–68why, one step back51 let result52 // Only create proxies for plain objects/arrays.53 if (!isDraftable(base)) {54 result = recipe(base)55 if (result === undefined) return base56 }57 // See #100, don't nest producers58 else if (isDraft(base)) {59 result = recipe.call(base, base)60 if (result === undefined) return base61 }62 // The given value must be proxied.63 else {64 this.scopes.push([])65 const baseDraft = this.createDraft(base)66 try {67 result = recipe.call(baseDraft, baseDraft)68 this.willFinalize(result, baseDraft, !!patchListener)nit: dedupe some codealeclarsonbefore adoptionc9e1d98
line 69why, one step back69feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 70–72why, one step back70 // Never generate patches when no listener exists.71 var patches = patchListener && [],72 inversePatches = patchListener && []Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
line 73why, one step back73feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 74–87why, one step back74 // Finalize the modified draft...75 if (result === undefined || result === baseDraft) {76 result = this.finalize(77 baseDraft,78 [],79 patches,80 inversePatches81 )82 }83 // ...or use a replacement value.84 else {85 // Users must never modify the draft _and_ return something else.86 if (baseDraft[DRAFT_STATE].modified)87 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 88why, one step back88feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 89–90why, one step back89 // Finalize the replacement in case it contains (or is) a subset of the draft.90 if (isDraftable(result)) result = this.finalize(result)Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2
line 91why, one step back91feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 92–131why, one step back92 if (patchListener) {93 patches.push({94 op: "replace",95 path: [],96 value: result97 })98 inversePatches.push({99 op: "replace",100 path: [],101 value: base102 })103 }104 }105 } finally {106 this.currentScope().forEach(state => state.revoke())107 this.scopes.pop()108 }109 patchListener && patchListener(patches, inversePatches)110 }111 // Normalize the result.112 return result === NOTHING ? undefined : result113 }114 setAutoFreeze(value) {115 this.autoFreeze = value116 }117 setUseProxies(value) {118 this.useProxies = value119 assign(this, value ? modernProxy : legacyProxy)120 }121 /**122 * @internal123 * Finalize a draft, returning either the unmodified base state or a modified124 * copy of the base state.125 */126 finalize(draft, path, patches, inversePatches) {127 const state = draft[DRAFT_STATE]128 if (!state) {129 if (Object.isFrozen(draft)) return draft130 return this.finalizeTree(draft)131 }feat: passing drafts to producealeclarsonbefore adoption9c53415
lines 132–135why, one step back132 // Never finalize drafts owned by an outer scope.133 if (state.scope !== this.currentScope()) {134 return draft135 }feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 136–139why, one step back136 if (!state.modified) return state.base137 if (!state.finalized) {138 state.finalized = true139 this.finalizeTree(state.draft, path, patches, inversePatches)feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
lines 140–145why, one step back140 if (this.onDelete) {141 const {assigned} = state142 for (const prop in assigned)143 assigned[prop] || this.onDelete(state, prop)144 }145 if (this.onCopy) this.onCopy(state)feat: passing drafts to producealeclarsonbefore adoption9c53415
lines 146–152why, one step back146147 // Nested producers must never auto-freeze their result,148 // because it may contain drafts from parent producers.149 if (this.autoFreeze && this.scopes.length === 1) {150 Object.freeze(state.copy)151 }152feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 153–167why, one step back153 if (patches) generatePatches(state, path, patches, inversePatches)154 }155 return state.copy156 }157 /**158 * @internal159 * Finalize all drafts in the given state tree.160 */161 finalizeTree(root, path, patches, inversePatches) {162 const state = root[DRAFT_STATE]163 if (state) {164 root = this.useProxies165 ? state.copy166 : (state.copy = shallowCopy(state.draft))167 }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: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
line 175why, one step back175 // prettier-ignorerefactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 176why, one step back176 parent[prop] = value =feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
line 177why, one step back177 // Patches are never generated for assigned properties.refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 178why, one step back178 patches && inDraft && !state.assigned[prop]feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
lines 179–180why, one step back179 ? this.finalize(value, path.concat(prop), patches, inversePatches)180 : this.finalize(value)feat: passing drafts to producealeclarsonbefore adoption9c53415
lines 181–183why, one step back181182 // Unchanged drafts are ignored.183 if (inDraft && value === state.base[prop]) returnrefactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 184why, one step back184 }feat: passing drafts to producealeclarsonbefore adoption9c53415
line 185why, one step back185 // Unchanged draft properties are ignored.refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 186why, one step back186 else if (inDraft && is(value, state.base[prop])) {feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
line 187why, one step back187 returnfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 188why, one step back188 }refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
lines 189–194why, one step back189 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.190 else if (isDraftable(value) && !Object.isFrozen(value)) {191 each(value, finalizeProperty)192 }193194 if (inDraft && onAssign) {feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364
lines 195–196why, one step back195 onAssign(state, prop, value)196 }feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 197why, one step back197 }refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a
line 198why, one step back198feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 199–202why, one step back199 each(root, finalizeProperty)200 return root201 }202}