Skip to content
Myr

immer.js

This file

Path

src/immer.js

192 lines · 6.9 KiB · JavaScript

Last changed by

feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364

History of this file

Take it

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 1–13why, one step back
1import * as legacyProxy from "./es5"
2import * as modernProxy from "./proxy"3import {generatePatches} from "./patches"4import {5 assign,6 each,7 has,8 is,9 isDraft,10 isDraftable,11 shallowCopy,12 DRAFT_STATE,13 NOTHING

Merge remote-tracking branch 'origin/v1.8'Michel Weststratebefore adoption7790417

line 14why, one step back
14} from "./common"

Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667

line 15why, one step back
15

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 16why, one step back
16function verifyMinified() {}

Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667

line 17why, one step back
17

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 18–22why, one step back
18const configDefaults = {
19 useProxies: typeof Proxy !== "undefined" && typeof Reflect !== "undefined",20 autoFreeze:21 typeof process !== "undefined"22 ? process.env.NODE_ENV !== "production"

feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364

lines 23–26why, one step back
23 : verifyMinified.name === "verifyMinified",
24 onAssign: null,25 onDelete: null,26 onCopy: null

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 27why, one step back
27}

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

line 28why, one step back
28

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 29–39why, one step back
29export class Immer {
30 constructor(config) {31 assign(this, configDefaults, config)32 this.setUseProxies(this.useProxies)33 this.produce = this.produce.bind(this)34 }35 produce(base, recipe, patchListener) {36 // curried invocation37 if (typeof base === "function" && typeof recipe !== "function") {38 const defaultBase = recipe39 recipe = base

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

line 40why, one step back
40

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 41–43why, one step back
41 // prettier-ignore
42 return (base = defaultBase, ...args) =>43 this.produce(base, draft => recipe.call(draft, draft, ...args))

Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667

lines 44–45why, one step back
44 }
45

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 46–50why, one step back
46 // prettier-ignore
47 {48 if (typeof recipe !== "function") throw new Error("if first argument is not a function, the second argument to produce should be a function")49 if (patchListener !== undefined && typeof patchListener !== "function") throw new Error("the third argument of a producer should not be set or a function")50 }

Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667

line 51why, one step back
51

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 52–69why, one step back
52 let result
53 // Only create proxies for plain objects/arrays.54 if (!isDraftable(base)) {55 result = recipe(base)56 if (result === undefined) return base57 }58 // See #100, don't nest producers59 else if (isDraft(base)) {60 result = recipe.call(base, base)61 if (result === undefined) return base62 }63 // The given value must be proxied.64 else {65 this.scopes.push([])66 const baseDraft = this.createDraft(base)67 try {68 result = recipe.call(baseDraft, baseDraft)69 this.willFinalize(result, baseDraft, !!patchListener)

nit: dedupe some codealeclarsonbefore adoptionc9e1d98

line 70why, one step back
70

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 71–73why, one step back
71 // Never generate patches when no listener exists.
72 var patches = patchListener && [],73 inversePatches = patchListener && []

Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637

line 74why, one step back
74

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 75–88why, one step back
75 // Finalize the modified draft...
76 if (result === undefined || result === baseDraft) {77 result = this.finalize(78 baseDraft,79 [],80 patches,81 inversePatches82 )83 }84 // ...or use a replacement value.85 else {86 // Users must never modify the draft _and_ return something else.87 if (baseDraft[DRAFT_STATE].modified)88 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 89why, one step back
89

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 90–91why, one step back
90 // Finalize the replacement in case it contains (or is) a subset of the draft.
91 if (isDraftable(result)) result = this.finalize(result)

Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2

line 92why, one step back
92

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 93–136why, one step back
93 if (patchListener) {
94 patches.push({95 op: "replace",96 path: [],97 value: result98 })99 inversePatches.push({100 op: "replace",101 path: [],102 value: base103 })104 }105 }106 } finally {107 this.currentScope().forEach(state => state.revoke())108 this.scopes.pop()109 }110 patchListener && patchListener(patches, inversePatches)111 }112 // Normalize the result.113 return result === NOTHING ? undefined : result114 }115 setAutoFreeze(value) {116 this.autoFreeze = value117 }118 setUseProxies(value) {119 this.useProxies = value120 assign(this, value ? modernProxy : legacyProxy)121 }122 /**123 * @internal124 * Finalize a draft, returning either the unmodified base state or a modified125 * copy of the base state.126 */127 finalize(draft, path, patches, inversePatches) {128 const state = draft[DRAFT_STATE]129 if (!state) {130 if (Object.isFrozen(draft)) return draft131 return this.finalizeTree(draft)132 }133 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 back
137 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: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 143–158why, one step back
143 if (this.autoFreeze) Object.freeze(state.copy)
144 if (patches) generatePatches(state, path, patches, inversePatches)145 }146 return state.copy147 }148 /**149 * @internal150 * Finalize all drafts in the given state tree.151 */152 finalizeTree(root, path, patches, inversePatches) {153 const state = root[DRAFT_STATE]154 if (state) {155 root = this.useProxies156 ? state.copy157 : (state.copy = shallowCopy(state.draft))158 }

feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364

line 159why, one step back
159 const {onAssign} = this

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 160why, one step back
160 const finalizeProperty = (prop, value, parent) => {

feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364

lines 161–182why, one step back
161 // Only `root` can be a draft in here.
162 const inDraft = state && parent === root163 if (isDraftable(value)) {164 // Skip unchanged properties in draft objects.165 if (inDraft && is(value, state.base[prop])) return166167 // Find proxies within new objects. Frozen objects are already finalized.168 if (!isDraft(value)) {169 if (!Object.isFrozen(value)) each(value, finalizeProperty)170 if (onAssign && inDraft) onAssign(state, prop, value)171 return172 }173174 // prettier-ignore175 parent[prop] =176 // Patches are never generated for assigned properties.177 patches && parent === root && !(state && has(state.assigned, prop))178 ? this.finalize(value, path.concat(prop), patches, inversePatches)179 : this.finalize(value)180181 if (onAssign && inDraft) onAssign(state, prop, parent[prop])182 return

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 183why, one step back
183 }

feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364

lines 184–187why, one step back
184 // Call `onAssign` for primitive values.
185 if (onAssign && inDraft && !is(value, state.base[prop])) {186 onAssign(state, prop, value)187 }

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 188–192why, one step back
188 }
189 each(root, finalizeProperty)190 return root191 }192}