Skip to content
Myr

proxy.js

This file

Path

src/proxy.js

289 lines · 8.5 KiB · JavaScript

Last changed by

feat: `Map` supportkeenondrumsbefore adoption8276a6c

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` supportkeenondrumsbefore adoption8276a6c

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` supportkeenondrumsbefore adoption8276a6c

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` supportkeenondrumsbefore adoption8276a6c

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

lines 61–71why, one step back
61const objectTraps = {
62 get,63 has(target, prop) {64 return prop in source(target)65 },66 ownKeys(target) {67 return Reflect.ownKeys(source(target))68 },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: Object.getPrototypeOf with a draftaleclarsonbefore adoptiond5d07e8

lines 75–77why, one step back
75 getPrototypeOf(target) {
76 return Object.getPrototypeOf(target.base)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` supportkeenondrumsbefore adoption8276a6c

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 }

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

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

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

line 203why, one step back
203// returns the object we should be reading the current value from, which is base, until some change has been made

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

line 204why, one step back
204function source(state) {

feat: passing drafts to producealeclarsonbefore adoption9c53415

line 205why, one step back
205 return state.copy || state.base

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

lines 206–207why, one step back
206}
207

fix: unnecessary draft creationAlec Larsonbefore adoptionec00ee4

lines 208–217why, 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.value216}217

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

line 218why, one step back
218function get(state, prop) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 219why, one step back
219 if (prop === DRAFT_STATE) return state

feat: passing drafts to producealeclarsonbefore adoption9c53415

lines 220–227why, one step back
220 let {drafts} = state
221222 // Check for existing draft in unmodified state.223 if (!state.modified && has(drafts, prop)) {224 return drafts[prop]225 }226227 const value = source(state)[prop]

chore: comments and formattingAlec Larsonbefore adoptionff96173

lines 228–230why, one step back
228 if (state.finalized || !isDraftable(value)) {
229 return value230 }

feat: passing drafts to producealeclarsonbefore adoption9c53415

lines 231–232why, one step back
231
232 // Check for existing draft in modified state.

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

line 233why, one step back
233 if (state.modified) {

feat: passing drafts to producealeclarsonbefore adoption9c53415

line 234why, one step back
234 // Assigned values are never drafted. This catches any drafts we created, too.

fix: unnecessary draft creationAlec Larsonbefore adoptionec00ee4

line 235why, one step back
235 if (value !== peek(state.base, prop)) return value

feat: passing drafts to producealeclarsonbefore adoption9c53415

lines 236–237why, one step back
236 // Store drafts on the copy (when one exists).
237 drafts = state.copy

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

line 238why, one step back
238 }

feat: passing drafts to producealeclarsonbefore adoption9c53415

line 239why, one step back
239

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

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

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

lines 241–244why, one step back
241}
242243function set(state, prop, value) {244 if (!state.modified) {

fix: unnecessary draft creationAlec Larsonbefore adoptionec00ee4

line 245why, one step back
245 const baseValue = peek(state.base, prop)

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 246–247why, one step back
246 // Optimize based on value's truthiness. Truthy values are guaranteed to
247 // never be undefined, so we can avoid the `in` operator. Lastly, truthy

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 248why, one step back
248 // values may be drafts, but falsy values are never drafts.

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 249why, one step back
249 const isUnchanged = value

fix: unnecessary draft creationAlec Larsonbefore adoptionec00ee4

lines 250–251why, one step back
250 ? is(baseValue, value) || value === state.drafts[prop]
251 : is(baseValue, value) && prop in state.base

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 252why, one step back
252 if (isUnchanged) return true

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

lines 253–254why, one step back
253 markChanged(state)
254 }

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 255why, one step back
255 state.assigned[prop] = true

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

lines 256–260why, one step back
256 state.copy[prop] = value
257 return true258}259260function deleteProperty(state, prop) {

perf(proxy): deletion of an undeclared propertyaleclarsonbefore adoptionc0ce252

line 261why, one step back
261 // The `undefined` check is a fast path for pre-existing keys.

fix: unnecessary draft creationAlec Larsonbefore adoptionec00ee4

line 262why, one step back
262 if (peek(state.base, prop) !== undefined || prop in state.base) {

perf(proxy): deletion of an undeclared propertyaleclarsonbefore adoptionc0ce252

lines 263–266why, one step back
263 state.assigned[prop] = false
264 markChanged(state)265 }266 if (state.copy) delete state.copy[prop]

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

lines 267–269why, one step back
267 return true
268}269

chore: comments and formattingAlec Larsonbefore adoptionff96173

lines 270–271why, 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.

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

line 272why, one step back
272function getOwnPropertyDescriptor(state, prop) {

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

lines 273–279why, one step back
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}