Skip to content
Myr

proxy.js

This file

Path

src/proxy.js

415 lines · 11.5 KiB · JavaScript

Last changed by

nit: minor refactoringAlec Larsonbefore adoptiona2c4754

History of this file

Take it

The walk stopped after 200 commits: lines older than 53e9ce8 point at it.

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 1–6why, one step back
1"use strict"
2import {3 assign,4 each,5 has,6 is,

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 7–8why, one step back
7 isDraftable,
8 isDraft,

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

line 9why, one step back
9 isMap,

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

line 10why, one step back
10 isSet,

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 11why, one step back
11 shallowCopy,

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

lines 12–15why, one step back
12 DRAFT_STATE,
13 assignMap,14 assignSet,15 original

perf: proxy assignaleclarsonbefore adoption53e9ce8

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

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

line 17why, one step back
17import {ImmerScope} from "./scope"

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 18why, one step back
18

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 19–21why, one step back
19// Do nothing before being finalized.
20export function willFinalize() {}21

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

lines 22–26why, one step back
22/**
23 * Returns a new draft of the `base` object.24 *25 * The second argument is the parent draft-state (used internally).26 */

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

lines 27–28why, one step back
27export function createProxy(base, parent) {
28 const scope = parent ? parent.scope : ImmerScope.current

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 29why, one step back
29 const state = {

feat: passing drafts to producealeclarsonbefore adoption9c53415

line 30why, one step back
30 // Track which produce call this is associated with.

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

line 31why, one step back
31 scope,

feat: passing drafts to producealeclarsonbefore adoption9c53415

lines 32–38why, one step back
32 // True for both shallow and deep changes.
33 modified: false,34 // Used during finalization.35 finalized: false,36 // Track which properties have been assigned (true) or deleted (false).37 assigned: {},38 // The parent draft state.

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 39why, one step back
39 parent,

feat: passing drafts to producealeclarsonbefore adoption9c53415

line 40why, one step back
40 // The base state.

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 41why, one step back
41 base,

feat: passing drafts to producealeclarsonbefore adoption9c53415

lines 42–46why, one step back
42 // The base proxy.
43 draft: null,44 // Any property proxies.45 drafts: {},46 // The base copy with any updated values.

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 47why, one step back
47 copy: null,

feat: passing drafts to producealeclarsonbefore adoption9c53415

lines 48–49why, one step back
48 // Called by the `produce` function.
49 revoke: null

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 50–51why, one step back
50 }
51

nit: minor refactoringAlec Larsonbefore adoptiona2c4754

line 52why, one step back
52 let target = state

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

lines 53–54why, one step back
53 let traps = objectTraps
54 if (Array.isArray(base)) {

nit: minor refactoringAlec Larsonbefore adoptiona2c4754

line 55why, one step back
55 target = [state]

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

line 56why, one step back
56 traps = arrayTraps

nit: minor refactoringAlec Larsonbefore adoptiona2c4754

lines 57–59why, one step back
57 }
58 // Map drafts must support object keys, so we use Map objects to track changes.59 else if (isMap(base)) {

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

line 60why, one step back
60 traps = mapTraps

feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5

lines 61–62why, one step back
61 state.drafts = new Map()
62 state.assigned = new Map()

nit: minor refactoringAlec Larsonbefore adoptiona2c4754

lines 63–66why, one step back
63 }
64 // Set drafts use a Map object to track which of its values are drafted.65 // And we don't need the "assigned" property, because Set objects have no keys.66 else if (isSet(base)) {

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

lines 67–68why, one step back
67 traps = setTraps
68 state.drafts = new Map()

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

line 69why, one step back
69 }

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 70why, one step back
70

nit: minor refactoringAlec Larsonbefore adoptiona2c4754

line 71why, one step back
71 const {revoke, proxy} = Proxy.revocable(target, traps)

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

line 72why, one step back
72

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 73–75why, one step back
73 state.draft = proxy
74 state.revoke = revoke75

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

line 76why, one step back
76 scope.drafts.push(proxy)

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 77–79why, one step back
77 return proxy
78}79

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

lines 80–83why, one step back
80/**
81 * Object drafts82 */83

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 84why, one step back
84const objectTraps = {

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

lines 85–93why, one step back
85 get(state, prop) {
86 if (prop === DRAFT_STATE) return state87 let {drafts} = state8889 // Check for existing draft in unmodified state.90 if (!state.modified && has(drafts, prop)) {91 return drafts[prop]92 }93

refactor: rename "source" helper to "latest"Alec Larsonbefore adoptioneba300f

line 94why, one step back
94 const value = latest(state)[prop]

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

lines 95–108why, one step back
95 if (state.finalized || !isDraftable(value)) {
96 return value97 }9899 // Check for existing draft in modified state.100 if (state.modified) {101 // Assigned values are never drafted. This catches any drafts we created, too.102 if (value !== peek(state.base, prop)) return value103 // Store drafts on the copy (when one exists).104 drafts = state.copy105 }106107 return (drafts[prop] = createProxy(value, state))108 },

nit: argument namesAlec Larsonbefore adoptiona674a78

line 109why, one step back
109 has(state, prop) {

refactor: rename "source" helper to "latest"Alec Larsonbefore adoptioneba300f

line 110why, one step back
110 return prop in latest(state)

chore: reorganize src/proxy.jsAlec Larsonbefore adoptione809ffc

line 111why, one step back
111 },

nit: argument namesAlec Larsonbefore adoptiona674a78

line 112why, one step back
112 ownKeys(state) {

refactor: rename "source" helper to "latest"Alec Larsonbefore adoptioneba300f

line 113why, one step back
113 return Reflect.ownKeys(latest(state))

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 114why, one step back
114 },

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

lines 115–142why, one step back
115 set(state, prop, value) {
116 if (!state.modified) {117 const baseValue = peek(state.base, prop)118 // Optimize based on value's truthiness. Truthy values are guaranteed to119 // never be undefined, so we can avoid the `in` operator. Lastly, truthy120 // values may be drafts, but falsy values are never drafts.121 const isUnchanged = value122 ? is(baseValue, value) || value === state.drafts[prop]123 : is(baseValue, value) && prop in state.base124 if (isUnchanged) return true125 markChanged(state)126 }127 state.assigned[prop] = true128 state.copy[prop] = value129 return true130 },131 deleteProperty(state, prop) {132 // The `undefined` check is a fast path for pre-existing keys.133 if (peek(state.base, prop) !== undefined || prop in state.base) {134 state.assigned[prop] = false135 markChanged(state)136 }137 if (state.copy) delete state.copy[prop]138 return true139 },140 // Note: We never coerce `desc.value` into an Immer draft, because we can't make141 // the same guarantee in ES5 mode.142 getOwnPropertyDescriptor(state, prop) {

refactor: rename "source" helper to "latest"Alec Larsonbefore adoptioneba300f

line 143why, one step back
143 const owner = latest(state)

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

lines 144–150why, one step back
144 const desc = Reflect.getOwnPropertyDescriptor(owner, prop)
145 if (desc) {146 desc.writable = true147 desc.configurable = !Array.isArray(owner) || prop !== "length"148 }149 return desc150 },

feat: limited class supportaleclarsonbefore adoption9877d64

lines 151–153why, one step back
151 defineProperty() {
152 throw new Error("Object.defineProperty() cannot be used on an Immer draft") // prettier-ignore153 },

nit: argument namesAlec Larsonbefore adoptiona674a78

lines 154–155why, one step back
154 getPrototypeOf(state) {
155 return Object.getPrototypeOf(state.base)

fix: Object.getPrototypeOf with a draftaleclarsonbefore adoptiond5d07e8

line 156why, one step back
156 },

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 157why, one step back
157 setPrototypeOf() {

feat: limited class supportaleclarsonbefore adoption9877d64

line 158why, one step back
158 throw new Error("Object.setPrototypeOf() cannot be used on an Immer draft") // prettier-ignore

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 159–161why, one step back
159 }
160}161

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

lines 162–165why, one step back
162/**
163 * Array drafts164 */165

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 166–173why, one step back
166const arrayTraps = {}
167each(objectTraps, (key, fn) => {168 arrayTraps[key] = function() {169 arguments[0] = arguments[0][0]170 return fn.apply(this, arguments)171 }172})173arrayTraps.deleteProperty = function(state, prop) {

feat: limited class supportaleclarsonbefore adoption9877d64

lines 174–176why, one step back
174 if (isNaN(parseInt(prop))) {
175 throw new Error("Immer only supports deleting array indices") // prettier-ignore176 }

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 177–179why, one step back
177 return objectTraps.deleteProperty.call(this, state[0], prop)
178}179arrayTraps.set = function(state, prop, value) {

feat: limited class supportaleclarsonbefore adoption9877d64

lines 180–182why, one step back
180 if (prop !== "length" && isNaN(parseInt(prop))) {
181 throw new Error("Immer only supports setting array indices and the 'length' property") // prettier-ignore182 }

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 183why, one step back
183 return objectTraps.set.call(this, state[0], prop, value)

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

lines 184–185why, one step back
184}
185

refactor: makeTrapsForGetters functionAlec Larsonbefore adoption5093fd0

lines 186–198why, one step back
186// Used by Map and Set drafts
187const reflectTraps = makeReflectTraps([188 "ownKeys",189 "has",190 "set",191 "deleteProperty",192 "defineProperty",193 "getOwnPropertyDescriptor",194 "preventExtensions",195 "isExtensible",196 "getPrototypeOf"197])198

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

lines 199–202why, one step back
199/**
200 * Map drafts201 */202

refactor: makeTrapsForGetters functionAlec Larsonbefore adoption5093fd0

line 203why, one step back
203const mapTraps = makeTrapsForGetters({

fix: add Map getter for DRAFT_STATE, make isEnumerable more resilientkeenondrumsbefore adoption7bbec0a

line 204why, one step back
204 [DRAFT_STATE]: state => state,

refactor: rename "source" helper to "latest"Alec Larsonbefore adoptioneba300f

lines 205–206why, one step back
205 size: state => latest(state).size,
206 has: state => key => latest(state).has(key),

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

line 207why, one step back
207 set: state => (key, value) => {

refactor: rename "source" helper to "latest"Alec Larsonbefore adoptioneba300f

lines 208–209why, one step back
208 const values = latest(state)
209 if (!values.has(key) || values.get(key) !== value) {

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

line 210why, one step back
210 markChanged(state)

feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5

line 211why, one step back
211 state.assigned.set(key, true)

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

lines 212–213why, one step back
212 state.copy.set(key, value)
213 }

fix: Map.prototype.set should return a draftAndrey Goncharovbefore adoption7001201

line 214why, one step back
214 return state.draft

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

lines 215–216why, one step back
215 },
216 delete: state => key => {

refactor: rename "source" helper to "latest"Alec Larsonbefore adoptioneba300f

line 217why, one step back
217 if (latest(state).has(key)) {

feat: `onDelete` for `Maps`keenondrumsbefore adoption1ca49ef

lines 218–222why, one step back
218 markChanged(state)
219 state.assigned.set(key, false)220 return state.copy.delete(key)221 }222 return false

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

lines 223–225why, one step back
223 },
224 clear: state => () => {225 markChanged(state)

feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5

line 226why, one step back
226 state.assigned = new Map()

refactor: rename "source" helper to "latest"Alec Larsonbefore adoptioneba300f

line 227why, one step back
227 for (const key of latest(state).keys()) {

feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5

line 228why, one step back
228 state.assigned.set(key, false)

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

line 229why, one step back
229 }

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

lines 230–232why, one step back
230 return state.copy.clear()
231 },232 forEach: (state, _, receiver) => (cb, thisArg) =>

refactor: rename "source" helper to "latest"Alec Larsonbefore adoptioneba300f

line 233why, one step back
233 latest(state).forEach((_, key, map) => {

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

lines 234–237why, one step back
234 const value = receiver.get(key)
235 cb.call(thisArg, value, key, map)236 }),237 get: state => key => {

refactor: "get" trap for MapsAlec Larsonbefore adoptionddf5dbb

lines 238–240why, one step back
238 const drafts = state[state.modified ? "copy" : "drafts"]
239 if (drafts.has(key)) {240 return drafts.get(key)

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

line 241why, one step back
241 }

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

line 242why, one step back
242

refactor: rename "source" helper to "latest"Alec Larsonbefore adoptioneba300f

line 243why, one step back
243 const value = latest(state).get(key)

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

lines 244–245why, one step back
244 if (state.finalized || !isDraftable(value)) {
245 return value

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

lines 246–247why, one step back
246 }
247

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

line 248why, one step back
248 const draft = createProxy(value, state)

refactor: "get" trap for MapsAlec Larsonbefore adoptionddf5dbb

line 249why, one step back
249 drafts.set(key, draft)

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

lines 250–251why, one step back
250 return draft
251 },

refactor: rename "source" helper to "latest"Alec Larsonbefore adoptioneba300f

line 252why, one step back
252 keys: state => () => latest(state).keys(),

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

lines 253–255why, one step back
253 values: iterateMapValues,
254 entries: iterateMapValues,255 [Symbol.iterator]: iterateMapValues

refactor: makeTrapsForGetters functionAlec Larsonbefore adoption5093fd0

line 256why, one step back
256})

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

line 257why, one step back
257

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

lines 258–259why, one step back
258/** Map.prototype.values _-or-_ Map.prototype.entries */
259function iterateMapValues(state, prop, receiver) {

nit: minor refactoringAlec Larsonbefore adoptiona2c4754

line 260why, one step back
260 const isEntries = prop !== "values"

fix: remove "function*" syntaxAlec Larsonbefore adoption08d88b8

line 261why, one step back
261 return () => {

refactor: rename "source" helper to "latest"Alec Larsonbefore adoptioneba300f

line 262why, one step back
262 const iterator = latest(state)[Symbol.iterator]()

fix: remove "function*" syntaxAlec Larsonbefore adoption08d88b8

lines 263–267why, one step back
263 return makeIterable(() => {
264 const result = iterator.next()265 if (!result.done) {266 const [key] = result.value267 const value = receiver.get(key)

nit: minor refactoringAlec Larsonbefore adoptiona2c4754

line 268why, one step back
268 result.value = isEntries ? [key, value] : value

fix: remove "function*" syntaxAlec Larsonbefore adoption08d88b8

lines 269–271why, one step back
269 }
270 return result271 })

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

line 272why, one step back
272 }

feat: `Map` supportkeenondrumsbefore adoption8276a6c

lines 273–274why, one step back
273}
274

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

line 275why, one step back
275/**

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

lines 276–278why, one step back
276 * Set drafts
277 */278

refactor: makeTrapsForGetters functionAlec Larsonbefore adoption5093fd0

line 279why, one step back
279const setTraps = makeTrapsForGetters({

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

line 280why, one step back
280 [DRAFT_STATE]: state => state,

refactor: rename "source" helper to "latest"Alec Larsonbefore adoptioneba300f

lines 281–282why, one step back
281 size: state => latest(state).size,
282 has: state => key => latest(state).has(key),

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

line 283why, one step back
283 add: state => value => {

refactor: rename "source" helper to "latest"Alec Larsonbefore adoptioneba300f

line 284why, one step back
284 if (!latest(state).has(value)) {

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

lines 285–309why, one step back
285 markChanged(state)
286 state.copy.add(value)287 }288 return state.draft289 },290 delete: state => value => {291 markChanged(state)292 return state.copy.delete(value)293 },294 clear: state => () => {295 markChanged(state)296 return state.copy.clear()297 },298 forEach: state => (cb, thisArg) => {299 const iterator = iterateSetValues(state)()300 let result = iterator.next()301 while (!result.done) {302 cb.call(thisArg, result.value, result.value, state.draft)303 result = iterator.next()304 }305 },306 keys: iterateSetValues,307 values: iterateSetValues,308 entries: iterateSetValues,309 [Symbol.iterator]: iterateSetValues

refactor: makeTrapsForGetters functionAlec Larsonbefore adoption5093fd0

line 310why, one step back
310})

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

lines 311–312why, one step back
311
312function iterateSetValues(state, prop) {

nit: minor refactoringAlec Larsonbefore adoptiona2c4754

line 313why, one step back
313 const isEntries = prop === "entries"

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

line 314why, one step back
314 return () => {

refactor: rename "source" helper to "latest"Alec Larsonbefore adoptioneba300f

line 315why, one step back
315 const iterator = latest(state)[Symbol.iterator]()

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

lines 316–318why, one step back
316 return makeIterable(() => {
317 const result = iterator.next()318 if (!result.done) {

nit: minor refactoringAlec Larsonbefore adoptiona2c4754

lines 319–320why, one step back
319 const value = wrapSetValue(state, result.value)
320 result.value = isEntries ? [value, value] : value

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

lines 321–343why, one step back
321 }
322 return result323 })324 }325}326327function wrapSetValue(state, value) {328 const key = original(value) || value329 let draft = state.drafts.get(key)330 if (!draft) {331 if (state.finalized || !isDraftable(value)) {332 return value333 }334 draft = createProxy(value, state)335 state.drafts.set(key, draft)336 if (state.modified) {337 state.copy.add(draft)338 }339 }340 return draft341}342343/**

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

lines 344–346why, one step back
344 * Helpers
345 */346

refactor: rename "source" helper to "latest"Alec Larsonbefore adoptioneba300f

lines 347–348why, one step back
347// Retrieve the latest values of the draft.
348function latest(state) {

fix: revert Map supportAlec Larsonbefore adoption47529d3

lines 349–350why, one step back
349 return state.copy || state.base
350}

chore: reorganize src/proxy.jsAlec Larsonbefore adoptione809ffc

line 351why, one step back
351

fix: revert Map supportAlec Larsonbefore adoption47529d3

lines 352–355why, one step back
352// Access a property without creating an Immer draft.
353function peek(draft, prop) {354 const state = draft[DRAFT_STATE]355 const desc = Reflect.getOwnPropertyDescriptor(

refactor: rename "source" helper to "latest"Alec Larsonbefore adoptioneba300f

line 356why, one step back
356 state ? latest(state) : draft,

fix: revert Map supportAlec Larsonbefore adoption47529d3

lines 357–359why, one step back
357 prop
358 )359 return desc && desc.value

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 360–364why, one step back
360}
361362function markChanged(state) {363 if (!state.modified) {364 state.modified = true

refactor: markChanged functionAlec Larsonbefore adoption5857d63

lines 365–376why, one step back
365
366 const {base, drafts, parent} = state367 const copy = shallowCopy(base)368369 if (isSet(base)) {370 // Note: The `drafts` property is preserved for Set objects, since371 // we need to keep track of which values are drafted.372 assignSet(copy, drafts)373 } else {374 // Merge nested drafts into the copy.375 if (isMap(base)) assignMap(copy, drafts)376 else assign(copy, drafts)

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

lines 377–378why, one step back
377 state.drafts = null
378 }

refactor: markChanged functionAlec Larsonbefore adoption5857d63

lines 379–383why, one step back
379
380 state.copy = copy381 if (parent) {382 markChanged(parent)383 }

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 384why, one step back
384 }

fix: remove "function*" syntaxAlec Larsonbefore adoption08d88b8

lines 385–392why, one step back
385}
386387function makeIterable(next) {388 let self389 return (self = {390 [Symbol.iterator]: () => self,391 next392 })

refactor: traps for Maps and Setskeenondrumsbefore adoptiondd82d89

lines 393–394why, one step back
393}
394

refactor: rename "source" helper to "latest"Alec Larsonbefore adoptioneba300f

line 395why, one step back
395/** Create traps that all use the `Reflect` API on the `latest(state)` */

refactor: makeTrapsForGetters functionAlec Larsonbefore adoption5093fd0

lines 396–397why, one step back
396function makeReflectTraps(names) {
397 return names.reduce((traps, name) => {

refactor: rename "source" helper to "latest"Alec Larsonbefore adoptioneba300f

line 398why, one step back
398 traps[name] = (state, ...args) => Reflect[name](latest(state), ...args)

refactor: makeTrapsForGetters functionAlec Larsonbefore adoption5093fd0

lines 399–402why, one step back
399 return traps
400 }, {})401}402

refactor: traps for Maps and Setskeenondrumsbefore adoptiondd82d89

lines 403–404why, one step back
403function makeTrapsForGetters(getters) {
404 return {

refactor: makeTrapsForGetters functionAlec Larsonbefore adoption5093fd0

line 405why, one step back
405 ...reflectTraps,

refactor: traps for Maps and Setskeenondrumsbefore adoptiondd82d89

lines 406–409why, one step back
406 get(state, prop, receiver) {
407 return getters.hasOwnProperty(prop)408 ? getters[prop](state, prop, receiver)409 : Reflect.get(state, prop, receiver)

fix: add more traps for Maps and SetsAndrey Goncharovbefore adoption930b4ab

lines 410–412why, one step back
410 },
411 setPrototypeOf(state) {412 throw new Error("Object.setPrototypeOf() cannot be used on an Immer draft") // prettier-ignore

refactor: traps for Maps and Setskeenondrumsbefore adoptiondd82d89

lines 413–414why, one step back
413 }
414 }

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 415why, one step back
415}