Skip to content
Myr

proxy.js

This file

Path

src/proxy.js

289 lines · 8.5 KiB · JavaScript

Last changed by

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

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

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

feat: passing drafts to producealeclarsonbefore adoption9c53415

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

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

line 22why, one step back
22 scope,

feat: passing drafts to producealeclarsonbefore adoption9c53415

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 30why, one step back
30 parent,

feat: passing drafts to producealeclarsonbefore adoption9c53415

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 32why, one step back
32 base,

feat: passing drafts to producealeclarsonbefore adoption9c53415

lines 33–37why, one step back
33 // The base proxy.
34 draft: null,35 // Any property proxies.36 drafts: {},37 // The base copy with any updated values.

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

feat: passing drafts to producealeclarsonbefore adoption9c53415

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 41–42why, one step back
41 }
42

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

lines 43–50why, one step back
43 let proxyTarget = state
44 let traps = objectTraps45 if (Array.isArray(base)) {46 proxyTarget = [state]47 traps = arrayTraps48 } else if (isMap(base)) {49 traps = mapTraps50 }

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 51why, one step back
51

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 54–56why, one step back
54 state.draft = proxy
55 state.revoke = revoke56

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 58–60why, one step back
58 return proxy
59}60

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

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

fix: revert Map supportAlec Larsonbefore adoption47529d3

lines 62–64why, one step back
62 get,
63 has(target, prop) {64 return prop in source(target)

chore: reorganize src/proxy.jsAlec Larsonbefore adoptione809ffc

line 65why, one step back
65 },

fix: revert Map supportAlec Larsonbefore adoption47529d3

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

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

line 68why, one step back
68 },

fix: revert Map supportAlec Larsonbefore adoption47529d3

lines 69–71why, one step back
69 set,
70 deleteProperty,71 getOwnPropertyDescriptor,

feat: limited class supportaleclarsonbefore adoption9877d64

lines 72–74why, one step back
72 defineProperty() {
73 throw new Error("Object.defineProperty() cannot be used on an Immer draft") // prettier-ignore74 },

fix: revert Map supportAlec Larsonbefore adoption47529d3

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

fix: Object.getPrototypeOf with a draftaleclarsonbefore adoptiond5d07e8

line 77why, one step back
77 },

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

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

feat: limited class supportaleclarsonbefore adoption9877d64

line 79why, one step back
79 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 80–90why, one step back
80 }
81}8283const arrayTraps = {}84each(objectTraps, (key, fn) => {85 arrayTraps[key] = function() {86 arguments[0] = arguments[0][0]87 return fn.apply(this, arguments)88 }89})90arrayTraps.deleteProperty = function(state, prop) {

feat: limited class supportaleclarsonbefore adoption9877d64

lines 91–93why, one step back
91 if (isNaN(parseInt(prop))) {
92 throw new Error("Immer only supports deleting array indices") // prettier-ignore93 }

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

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

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

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

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

lines 101–200why, one step back
101}
102103const mapTraps = {104 get(state, prop, receiver) {105 if (prop === DRAFT_STATE) return state106 if (prop === "size") return source(state)[prop]107 if (prop === "has") {108 const stateCurrent = source(state)109 return stateCurrent[prop].bind(stateCurrent)110 }111 if (prop === "set") {112 return function(key, value) {113 markChanged(state)114 state.assigned[key] = true115 return state.copy.set(key, value)116 }117 }118 if (prop === "delete") {119 return function(key) {120 markChanged(state)121 state.assigned[key] = false122 return state.copy.delete(key)123 }124 }125 if (prop === "clear") {126 return function() {127 markChanged(state)128 state.assigned = {}129 for (const key of source(state).keys()) {130 state.assigned[key] = false131 }132 return state.copy.clear()133 }134 }135 if (prop === "forEach") {136 return function(cb) {137 return source(state).forEach((_, key, map) => {138 const value = receiver.get(key)139 cb(value, key, map)140 }, this)141 }142 }143 if (prop === "get") {144 return function(key) {145 if (!state.modified && has(state.drafts, key)) {146 return state.drafts[key]147 }148 if (state.modified && state.copy.has(key)) {149 return state.copy.get(key)150 }151152 const value = source(state).get(key)153154 if (state.finalized || !isDraftable(value)) {155 return value156 }157158 const valueProxied = createProxy(value, state)159 if (!state.modified) {160 state.drafts[key] = valueProxied161 } else {162 state.copy.set(key, valueProxied)163 }164 return valueProxied165 }166 }167 if (168 prop === Symbol.iterator ||169 prop === "entries" ||170 prop === "values"171 ) {172 let getYieldable = (key, value) => [key, value]173 if (prop === "values") {174 getYieldable = (key, value) => value175 }176 return function*() {177 const iterator = source(state)[Symbol.iterator]()178 let result = iterator.next()179 while (!result.done) {180 const [key] = result.value181 const value = receiver.get(key)182 yield getYieldable(key, value)183 result = iterator.next()184 }185 }186 }187 if (prop === "keys") {188 return function*() {189 const iterator = source(state).keys()190 let result = iterator.next()191 while (!result.done) {192 const key = result.value193 yield key194 result = iterator.next()195 }196 }197 }198199 return Reflect.get(state, prop, receiver)200 }

feat: `Map` supportkeenondrumsbefore adoption8276a6c

lines 201–202why, one step back
201}
202

fix: revert Map supportAlec Larsonbefore adoption47529d3

lines 203–206why, one step back
203// returns the object we should be reading the current value from, which is base, until some change has been made
204function source(state) {205 return state.copy || state.base206}

chore: reorganize src/proxy.jsAlec Larsonbefore adoptione809ffc

line 207why, one step back
207

fix: revert Map supportAlec Larsonbefore adoption47529d3

lines 208–215why, one step back
208// Access a property without creating an Immer draft.
209function peek(draft, prop) {210 const state = draft[DRAFT_STATE]211 const desc = Reflect.getOwnPropertyDescriptor(212 state ? source(state) : draft,213 prop214 )215 return desc && desc.value

chore: reorganize src/proxy.jsAlec Larsonbefore adoptione809ffc

lines 216–217why, one step back
216}
217

fix: revert Map supportAlec Larsonbefore adoption47529d3

lines 218–220why, one step back
218function get(state, prop) {
219 if (prop === DRAFT_STATE) return state220 let {drafts} = state

chore: reorganize src/proxy.jsAlec Larsonbefore adoptione809ffc

line 221why, one step back
221

fix: revert Map supportAlec Larsonbefore adoption47529d3

lines 222–225why, one step back
222 // Check for existing draft in unmodified state.
223 if (!state.modified && has(drafts, prop)) {224 return drafts[prop]225 }

chore: reorganize src/proxy.jsAlec Larsonbefore adoptione809ffc

line 226why, one step back
226

fix: revert Map supportAlec Larsonbefore adoption47529d3

lines 227–230why, one step back
227 const value = source(state)[prop]
228 if (state.finalized || !isDraftable(value)) {229 return value230 }

feat: `Map` supportkeenondrumsbefore adoption8276a6c

line 231why, one step back
231

fix: revert Map supportAlec Larsonbefore adoption47529d3

lines 232–238why, one step back
232 // Check for existing draft in modified state.
233 if (state.modified) {234 // Assigned values are never drafted. This catches any drafts we created, too.235 if (value !== peek(state.base, prop)) return value236 // Store drafts on the copy (when one exists).237 drafts = state.copy238 }

feat: `Map` supportkeenondrumsbefore adoption8276a6c

line 239why, one step back
239

fix: revert Map supportAlec Larsonbefore adoption47529d3

line 240why, one step back
240 return (drafts[prop] = createProxy(value, state))

chore: reorganize src/proxy.jsAlec Larsonbefore adoptione809ffc

line 241why, one step back
241}

feat: `Map` supportkeenondrumsbefore adoption8276a6c

line 242why, one step back
242

fix: revert Map supportAlec Larsonbefore adoption47529d3

lines 243–253why, one step back
243function set(state, prop, value) {
244 if (!state.modified) {245 const baseValue = peek(state.base, prop)246 // Optimize based on value's truthiness. Truthy values are guaranteed to247 // never be undefined, so we can avoid the `in` operator. Lastly, truthy248 // values may be drafts, but falsy values are never drafts.249 const isUnchanged = value250 ? is(baseValue, value) || value === state.drafts[prop]251 : is(baseValue, value) && prop in state.base252 if (isUnchanged) return true253 markChanged(state)

feat: `Map` supportkeenondrumsbefore adoption8276a6c

line 254why, one step back
254 }

fix: revert Map supportAlec Larsonbefore adoption47529d3

lines 255–257why, one step back
255 state.assigned[prop] = true
256 state.copy[prop] = value257 return true

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

lines 258–259why, one step back
258}
259

fix: revert Map supportAlec Larsonbefore adoption47529d3

lines 260–267why, one step back
260function deleteProperty(state, prop) {
261 // The `undefined` check is a fast path for pre-existing keys.262 if (peek(state.base, prop) !== undefined || prop in state.base) {263 state.assigned[prop] = false264 markChanged(state)265 }266 if (state.copy) delete state.copy[prop]267 return true

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

lines 268–269why, one step back
268}
269

fix: revert Map supportAlec Larsonbefore adoption47529d3

lines 270–279why, one step back
270// Note: We never coerce `desc.value` into an Immer draft, because we can't make
271// the same guarantee in ES5 mode.272function getOwnPropertyDescriptor(state, prop) {273 const owner = source(state)274 const desc = Reflect.getOwnPropertyDescriptor(owner, prop)275 if (desc) {276 desc.writable = true277 desc.configurable = !Array.isArray(owner) || prop !== "length"278 }279 return desc

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

lines 280–284why, one step back
280}
281282function markChanged(state) {283 if (!state.modified) {284 state.modified = true

feat: passing drafts to producealeclarsonbefore adoption9c53415

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

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

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