Skip to content
Myr

proxy.js

This file

Path

src/proxy.js

299 lines · 8.2 KiB · JavaScript

Last changed by

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

History of this file

Take it

The walk stopped after 200 commits: lines older than 1f93af2 point at it.

fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2

lines 1–2why, one step back
1"use strict"
2import {

fix: always use `assign` polyfillaleclarsonbefore adoption241a648

lines 3–4why, one step back
3 assign,
4 each,

fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2

line 5why, one step back
5 has,

fix: always use `assign` polyfillaleclarsonbefore adoption241a648

line 6why, one step back
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,

fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2

line 10why, one step back
10 shallowCopy,

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 11why, one step back
11 DRAFT_STATE

fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2

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

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

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

fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2

line 14why, one step back
14

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 15–17why, one step back
15// Do nothing before being finalized.
16export function willFinalize() {}17

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

lines 18–22why, one step back
18/**
19 * Returns a new draft of the `base` object.20 *21 * The second argument is the parent draft-state (used internally).22 */

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

feat: passing drafts to producealeclarsonbefore adoption9c53415

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

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

line 27why, one step back
27 scope,

feat: passing drafts to producealeclarsonbefore adoption9c53415

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 35why, one step back
35 parent,

feat: passing drafts to producealeclarsonbefore adoption9c53415

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 37why, one step back
37 base,

feat: passing drafts to producealeclarsonbefore adoption9c53415

lines 38–42why, one step back
38 // The base proxy.
39 draft: null,40 // Any property proxies.41 drafts: {},42 // The base copy with any updated values.

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

feat: passing drafts to producealeclarsonbefore adoption9c53415

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 46–47why, one step back
46 }
47

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

lines 48–55why, one step back
48 let proxyTarget = state
49 let traps = objectTraps50 if (Array.isArray(base)) {51 proxyTarget = [state]52 traps = arrayTraps53 } else if (isMap(base)) {54 traps = mapTraps55 }

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 56why, one step back
56

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

lines 57–58why, one step back
57 const {revoke, proxy} = Proxy.revocable(proxyTarget, traps)
58

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 59–61why, one step back
59 state.draft = proxy
60 state.revoke = revoke61

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 63–65why, one step back
63 return proxy
64}65

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

lines 66–69why, one step back
66/**
67 * Object drafts68 */69

fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2

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

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

lines 71–94why, one step back
71 get(state, prop) {
72 if (prop === DRAFT_STATE) return state73 let {drafts} = state7475 // Check for existing draft in unmodified state.76 if (!state.modified && has(drafts, prop)) {77 return drafts[prop]78 }7980 const value = source(state)[prop]81 if (state.finalized || !isDraftable(value)) {82 return value83 }8485 // Check for existing draft in modified state.86 if (state.modified) {87 // Assigned values are never drafted. This catches any drafts we created, too.88 if (value !== peek(state.base, prop)) return value89 // Store drafts on the copy (when one exists).90 drafts = state.copy91 }9293 return (drafts[prop] = createProxy(value, state))94 },

fix: revert Map supportAlec Larsonbefore adoption47529d3

lines 95–96why, one step back
95 has(target, prop) {
96 return prop in source(target)

chore: reorganize src/proxy.jsAlec Larsonbefore adoptione809ffc

line 97why, one step back
97 },

fix: revert Map supportAlec Larsonbefore adoption47529d3

lines 98–99why, one step back
98 ownKeys(target) {
99 return Reflect.ownKeys(source(target))

fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2

line 100why, one step back
100 },

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

lines 101–136why, one step back
101 set(state, prop, value) {
102 if (!state.modified) {103 const baseValue = peek(state.base, prop)104 // Optimize based on value's truthiness. Truthy values are guaranteed to105 // never be undefined, so we can avoid the `in` operator. Lastly, truthy106 // values may be drafts, but falsy values are never drafts.107 const isUnchanged = value108 ? is(baseValue, value) || value === state.drafts[prop]109 : is(baseValue, value) && prop in state.base110 if (isUnchanged) return true111 markChanged(state)112 }113 state.assigned[prop] = true114 state.copy[prop] = value115 return true116 },117 deleteProperty(state, prop) {118 // The `undefined` check is a fast path for pre-existing keys.119 if (peek(state.base, prop) !== undefined || prop in state.base) {120 state.assigned[prop] = false121 markChanged(state)122 }123 if (state.copy) delete state.copy[prop]124 return true125 },126 // Note: We never coerce `desc.value` into an Immer draft, because we can't make127 // the same guarantee in ES5 mode.128 getOwnPropertyDescriptor(state, prop) {129 const owner = source(state)130 const desc = Reflect.getOwnPropertyDescriptor(owner, prop)131 if (desc) {132 desc.writable = true133 desc.configurable = !Array.isArray(owner) || prop !== "length"134 }135 return desc136 },

feat: limited class supportaleclarsonbefore adoption9877d64

lines 137–139why, one step back
137 defineProperty() {
138 throw new Error("Object.defineProperty() cannot be used on an Immer draft") // prettier-ignore139 },

fix: revert Map supportAlec Larsonbefore adoption47529d3

lines 140–141why, one step back
140 getPrototypeOf(target) {
141 return Object.getPrototypeOf(target.base)

fix: Object.getPrototypeOf with a draftaleclarsonbefore adoptiond5d07e8

line 142why, one step back
142 },

fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2

lines 145–147why, one step back
145 }
146}147

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

lines 148–151why, one step back
148/**
149 * Array drafts150 */151

fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2

lines 152–159why, one step back
152const arrayTraps = {}
153each(objectTraps, (key, fn) => {154 arrayTraps[key] = function() {155 arguments[0] = arguments[0][0]156 return fn.apply(this, arguments)157 }158})159arrayTraps.deleteProperty = function(state, prop) {

feat: limited class supportaleclarsonbefore adoption9877d64

lines 160–162why, one step back
160 if (isNaN(parseInt(prop))) {
161 throw new Error("Immer only supports deleting array indices") // prettier-ignore162 }

fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2

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

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

lines 170–171why, one step back
170}
171

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

lines 172–175why, one step back
172/**
173 * Map drafts174 */175

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

lines 176–177why, one step back
176const mapTraps = {
177 get(state, prop, receiver) {

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

lines 178–184why, one step back
178 const getter = mapGetters[prop]
179 return getter180 ? getter(state, prop, receiver)181 : Reflect.get(state, prop, receiver)182 }183}184const mapGetters = {

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

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

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

lines 186–193why, one step back
186 size: state => source(state).size,
187 has(state, prop) {188 state = source(state)189 return state.has.bind(state)190 },191 set: state => (key, value) => {192 markChanged(state)193 state.assigned[key] = true

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

lines 194–195why, one step back
194 state.copy.set(key, value)
195 return state.draft

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

lines 196–206why, one step back
196 },
197 delete: state => key => {198 markChanged(state)199 state.assigned[key] = false200 return state.copy.delete(key)201 },202 clear: state => () => {203 markChanged(state)204 state.assigned = {}205 for (const key of source(state).keys()) {206 state.assigned[key] = false

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

line 207why, one step back
207 }

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

lines 208–217why, one step back
208 return state.copy.clear()
209 },210 forEach: (state, _, receiver) => (cb, thisArg) =>211 source(state).forEach((_, key, map) => {212 const value = receiver.get(key)213 cb.call(thisArg, value, key, map)214 }),215 get: state => key => {216 if (!state.modified && has(state.drafts, key)) {217 return state.drafts[key]

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

line 218why, one step back
218 }

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

lines 219–221why, one step back
219
220 if (state.modified && state.copy.has(key)) {221 return state.copy.get(key)

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

line 222why, one step back
222 }

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

lines 223–227why, one step back
223
224 const value = source(state).get(key)225226 if (state.finalized || !isDraftable(value)) {227 return value

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

lines 228–229why, one step back
228 }
229

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

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

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

line 231why, one step back
231

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

lines 232–236why, one step back
232 if (!state.modified) {
233 state.drafts[key] = draft234 } else {235 state.copy.set(key, draft)236 }

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

line 237why, one step back
237

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

lines 238–239why, one step back
238 return draft
239 },

refactor: simplify mapGetters.keysAlec Larsonbefore adoption3298a77

line 240why, one step back
240 keys: state => () => source(state).keys(),

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

lines 241–244why, one step back
241 values: iterateMapValues,
242 entries: iterateMapValues,243 [Symbol.iterator]: iterateMapValues244}

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

line 245why, one step back
245

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

lines 246–250why, one step back
246/** Map.prototype.values _-or-_ Map.prototype.entries */
247function iterateMapValues(state, prop, receiver) {248 const getYieldable =249 prop === "values" ? (key, value) => value : (key, value) => [key, value]250

fix: remove "function*" syntaxAlec Larsonbefore adoption08d88b8

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

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

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

fix: remove "function*" syntaxAlec Larsonbefore adoption08d88b8

lines 253–261why, one step back
253 return makeIterable(() => {
254 const result = iterator.next()255 if (!result.done) {256 const [key] = result.value257 const value = receiver.get(key)258 result.value = getYieldable(key, value)259 }260 return result261 })

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

line 262why, one step back
262 }

feat: `Map` supportkeenondrumsbefore adoption8276a6c

lines 263–264why, one step back
263}
264

chore: reorganize src/proxy.jsAlec Larsonbefore adoption3163a7d

lines 265–268why, one step back
265/**
266 * Helpers267 */268

fix: revert Map supportAlec Larsonbefore adoption47529d3

lines 269–272why, one step back
269// returns the object we should be reading the current value from, which is base, until some change has been made
270function source(state) {271 return state.copy || state.base272}

chore: reorganize src/proxy.jsAlec Larsonbefore adoptione809ffc

line 273why, one step back
273

fix: revert Map supportAlec Larsonbefore adoption47529d3

lines 274–281why, one step back
274// Access a property without creating an Immer draft.
275function peek(draft, prop) {276 const state = draft[DRAFT_STATE]277 const desc = Reflect.getOwnPropertyDescriptor(278 state ? source(state) : draft,279 prop280 )281 return desc && desc.value

fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2

lines 282–286why, one step back
282}
283284function markChanged(state) {285 if (!state.modified) {286 state.modified = true

feat: passing drafts to producealeclarsonbefore adoption9c53415

lines 287–288why, one step back
287 state.copy = assign(shallowCopy(state.base), state.drafts)
288 state.drafts = null

fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2

lines 289–290why, one step back
289 if (state.parent) markChanged(state.parent)
290 }

fix: remove "function*" syntaxAlec Larsonbefore adoption08d88b8

lines 291–298why, one step back
291}
292293function makeIterable(next) {294 let self295 return (self = {296 [Symbol.iterator]: () => self,297 next298 })

fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2

line 299why, one step back
299}