Skip to content
Myr

proxy.js

This file

Path

src/proxy.js

192 lines · 5.9 KiB · JavaScript

Last changed by

perf(proxy): deletion of an undeclared propertyaleclarsonbefore adoptionc0ce252

History of this file

Take it

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 1–3why, one step back
1"use strict"
2// @ts-check3

Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a

line 4why, one step back
4import {

fix: always use `assign` polyfillaleclarsonbefore adoption241a648

lines 5–6why, one step back
5 assign,
6 each,

`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760

line 7why, one step back
7 has,

fix: always use `assign` polyfillaleclarsonbefore adoption241a648

line 8why, one step back
8 is,

Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a

lines 9–10why, one step back
9 isProxyable,
10 isProxy,

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

lines 11–12why, one step back
11 finalize,
12 shallowCopy,

fix: always use `assign` polyfillaleclarsonbefore adoption241a648

lines 13–14why, one step back
13 PROXY_STATE,
14 RETURNED_AND_MODIFIED_ERROR

Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a

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

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

line 16why, one step back
16

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

line 17why, one step back
17let proxies = null

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 18–31why, one step back
18
19const objectTraps = {20 get,21 has(target, prop) {22 return prop in source(target)23 },24 ownKeys(target) {25 return Reflect.ownKeys(source(target))26 },27 set,28 deleteProperty,29 getOwnPropertyDescriptor,30 defineProperty,31 setPrototypeOf() {

Merge pull request #144 from btnwtn/patch-1Michel Weststratebefore adoptionaaa12eb

line 32why, one step back
32 throw new Error("Immer does not support `setPrototypeOf()`.")

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 33–36why, one step back
33 }
34}3536const arrayTraps = {}

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

line 37why, one step back
37each(objectTraps, (key, fn) => {

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 38–39why, one step back
38 arrayTraps[key] = function() {
39 arguments[0] = arguments[0][0]

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

line 40why, one step back
40 return fn.apply(this, arguments)

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

line 41why, one step back
41 }

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

line 42why, one step back
42})

Be explicit about not supporting arbitrary properties on arraysMichel Weststratebefore adoption0a80c2c

lines 43–57why, one step back
43arrayTraps.deleteProperty = function(state, prop) {
44 if (isNaN(parseInt(prop)))45 throw new Error(46 "Immer does not support deleting properties from arrays: " + prop47 )48 return objectTraps.deleteProperty.call(this, state[0], prop)49}50arrayTraps.set = function(state, prop, value) {51 if (prop !== "length" && isNaN(parseInt(prop)))52 throw new Error(53 "Immer does not support setting non-numeric properties on arrays: " +54 prop55 )56 return objectTraps.set.call(this, state[0], prop, value)57}

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 58–60why, one step back
58
59function createState(parent, base) {60 return {

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 61–62why, one step back
61 modified: false, // this tree is modified (either this object or one of it's children)
62 assigned: {}, // true: value was assigned to these props, false: was removed

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 63–78why, one step back
63 finalized: false,
64 parent,65 base,66 copy: undefined,67 proxies: {}68 }69}7071function source(state) {72 return state.modified === true ? state.copy : state.base73}7475function get(state, prop) {76 if (prop === PROXY_STATE) return state77 if (state.modified) {78 const value = state.copy[prop]

Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092

lines 79–81why, one step back
79 if (value === state.base[prop] && isProxyable(value))
80 // only create proxy if it is not yet a proxy, and not a new object81 // (new objects don't need proxying, they will be processed in finalize anyway)

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 82–84why, one step back
82 return (state.copy[prop] = createProxy(state, value))
83 return value84 } else {

`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760

line 85why, one step back
85 if (has(state.proxies, prop)) return state.proxies[prop]

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 86–94why, one step back
86 const value = state.base[prop]
87 if (!isProxy(value) && isProxyable(value))88 return (state.proxies[prop] = createProxy(state, value))89 return value90 }91}9293function set(state, prop, value) {94 if (!state.modified) {

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 95–101why, one step back
95 // Optimize based on value's truthiness. Truthy values are guaranteed to
96 // never be undefined, so we can avoid the `in` operator. Lastly, truthy97 // values may be proxies, but falsy values are never proxies.98 const isUnchanged = value99 ? is(state.base[prop], value) || value === state.proxies[prop]100 : is(state.base[prop], value) && prop in state.base101 if (isUnchanged) return true

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 102–103why, one step back
102 markChanged(state)
103 }

perf: proxy assignaleclarsonbefore adoption53e9ce8

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

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 105–109why, one step back
105 state.copy[prop] = value
106 return true107}108109function deleteProperty(state, prop) {

perf(proxy): deletion of an undeclared propertyaleclarsonbefore adoptionc0ce252

lines 110–115why, one step back
110 // The `undefined` check is a fast path for pre-existing keys.
111 if (state.base[prop] !== undefined || prop in state.base) {112 state.assigned[prop] = false113 markChanged(state)114 }115 if (state.copy) delete state.copy[prop]

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 116–121why, one step back
116 return true
117}118119function getOwnPropertyDescriptor(state, prop) {120 const owner = state.modified121 ? state.copy

Merge pull request #200 from stefanwille/upgrade-prettierMichel Weststratebefore adoption1139f57

line 122why, one step back
122 : has(state.proxies, prop)

perf(proxy): deletion of an undeclared propertyaleclarsonbefore adoptionc0ce252

lines 123–124why, one step back
123 ? state.proxies
124 : state.base

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 125–132why, one step back
125 const descriptor = Reflect.getOwnPropertyDescriptor(owner, prop)
126 if (descriptor && !(Array.isArray(owner) && prop === "length"))127 descriptor.configurable = true128 return descriptor129}130131function defineProperty() {132 throw new Error(

Merge pull request #144 from btnwtn/patch-1Michel Weststratebefore adoptionaaa12eb

line 133why, one step back
133 "Immer does not support defining properties on draft objects."

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 134–139why, one step back
134 )
135}136137function markChanged(state) {138 if (!state.modified) {139 state.modified = true

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

lines 140–141why, one step back
140 state.copy = shallowCopy(state.base)
141 // copy the proxies over the base-copy

fix: always use `assign` polyfillaleclarsonbefore adoption241a648

line 142why, one step back
142 assign(state.copy, state.proxies) // yup that works for arrays as well

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 143–147why, one step back
143 if (state.parent) markChanged(state.parent)
144 }145}146147// creates a proxy for plain objects / arrays

Remove unused parameter (#206)Stevenbefore adoption09d69ae

line 148why, one step back
148function createProxy(parentState, base) {

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 149why, one step back
149 if (isProxy(base)) throw new Error("Immer bug. Plz report.")

Remove unused parameter (#206)Stevenbefore adoption09d69ae

line 150why, one step back
150 const state = createState(parentState, base)

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

lines 151–154why, one step back
151 const proxy = Array.isArray(base)
152 ? Proxy.revocable([state], arrayTraps)153 : Proxy.revocable(state, objectTraps)154 proxies.push(proxy)

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 155–157why, one step back
155 return proxy.proxy
156}157

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 158why, one step back
158export function produceProxy(baseState, producer, patchListener) {

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

lines 159–160why, one step back
159 const previousProxies = proxies
160 proxies = []

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 161–162why, one step back
161 const patches = patchListener && []
162 const inversePatches = patchListener && []

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 163–164why, one step back
163 try {
164 // create proxy for root

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

line 165why, one step back
165 const rootProxy = createProxy(undefined, baseState)

nit: dedupe some codealeclarsonbefore adoptionc9e1d98

line 166why, one step back
166 // execute the producer function

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

line 167why, one step back
167 const returnValue = producer.call(rootProxy, rootProxy)

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

line 168why, one step back
168 // and finalize the modified proxy

Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145

line 169why, one step back
169 let result

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

lines 170–174why, one step back
170 // check whether the draft was modified and/or a value was returned
171 if (returnValue !== undefined && returnValue !== rootProxy) {172 // something was returned, and it wasn't the proxy itself173 if (rootProxy[PROXY_STATE].modified)174 throw new Error(RETURNED_AND_MODIFIED_ERROR)

Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145

line 175why, one step back
175

nit: dedupe some codealeclarsonbefore adoptionc9e1d98

line 176why, one step back
176 // we need to finalize the return value in case it's a subset of the draft

Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145

line 177why, one step back
177 result = finalize(returnValue)

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 178–181why, one step back
178 if (patches) {
179 patches.push({op: "replace", path: [], value: result})180 inversePatches.push({op: "replace", path: [], value: baseState})181 }

Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145

line 182why, one step back
182 } else {

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 183why, one step back
183 result = finalize(rootProxy, [], patches, inversePatches)

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

line 184why, one step back
184 }

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

line 185why, one step back
185 // revoke all proxies

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

line 186why, one step back
186 each(proxies, (_, p) => p.revoke())

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 187why, one step back
187 patchListener && patchListener(patches, inversePatches)

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

line 188why, one step back
188 return result

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

line 189why, one step back
189 } finally {

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

line 190why, one step back
190 proxies = previousProxies

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 191–192why, one step back
191 }
192}