proxy.js
This file
Path
src/proxy.js
192 lines · 5.8 KiB · JavaScript
Last changed by
Be explicit about not supporting arbitrary properties on arraysMichel Weststratebefore adoption0a80c2c
History of this fileTake it
Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 1–3why, one step back1"use strict"2// @ts-check3Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
line 4why, one step back4import {Added polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1
line 5why, one step back5 is,`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760
line 6why, one step back6 has,Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
lines 7–9why, one step back7 isProxyable,8 isProxy,9 PROXY_STATE,Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 10–11why, one step back10 finalize,11 shallowCopy,Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
line 12why, one step back12 RETURNED_AND_MODIFIED_ERROR,Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 13why, one step back13 eachFixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
line 14why, one step back14} from "./common"Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 15why, one step back15Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 16why, one step back16let proxies = nullMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 17–30why, one step back1718const objectTraps = {19 get,20 has(target, prop) {21 return prop in source(target)22 },23 ownKeys(target) {24 return Reflect.ownKeys(source(target))25 },26 set,27 deleteProperty,28 getOwnPropertyDescriptor,29 defineProperty,30 setPrototypeOf() {Merge pull request #144 from btnwtn/patch-1Michel Weststratebefore adoptionaaa12eb
line 31why, one step back31 throw new Error("Immer does not support `setPrototypeOf()`.")Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 32–35why, one step back32 }33}3435const arrayTraps = {}Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 36why, one step back36each(objectTraps, (key, fn) => {Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 37–38why, one step back37 arrayTraps[key] = function() {38 arguments[0] = arguments[0][0]Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 39why, one step back39 return fn.apply(this, arguments)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 40why, one step back40 }Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 41why, one step back41})Be explicit about not supporting arbitrary properties on arraysMichel Weststratebefore adoption0a80c2c
lines 42–56why, one step back42arrayTraps.deleteProperty = function(state, prop) {43 if (isNaN(parseInt(prop)))44 throw new Error(45 "Immer does not support deleting properties from arrays: " + prop46 )47 return objectTraps.deleteProperty.call(this, state[0], prop)48}49arrayTraps.set = function(state, prop, value) {50 if (prop !== "length" && isNaN(parseInt(prop)))51 throw new Error(52 "Immer does not support setting non-numeric properties on arrays: " +53 prop54 )55 return objectTraps.set.call(this, state[0], prop, value)56}Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 57–59why, one step back5758function createState(parent, base) {59 return {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 60–61why, one step back60 modified: false, // this tree is modified (either this object or one of it's children)61 assigned: {}, // true: value was assigned to these props, false: was removedMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 62–77why, one step back62 finalized: false,63 parent,64 base,65 copy: undefined,66 proxies: {}67 }68}6970function source(state) {71 return state.modified === true ? state.copy : state.base72}7374function get(state, prop) {75 if (prop === PROXY_STATE) return state76 if (state.modified) {77 const value = state.copy[prop]Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
lines 78–80why, one step back78 if (value === state.base[prop] && isProxyable(value))79 // only create proxy if it is not yet a proxy, and not a new object80 // (new objects don't need proxying, they will be processed in finalize anyway)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 81–83why, one step back81 return (state.copy[prop] = createProxy(state, value))82 return value83 } else {`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760
line 84why, one step back84 if (has(state.proxies, prop)) return state.proxies[prop]Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 85–92why, one step back85 const value = state.base[prop]86 if (!isProxy(value) && isProxyable(value))87 return (state.proxies[prop] = createProxy(state, value))88 return value89 }90}9192function set(state, prop, value) {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 93–94why, one step back93 // TODO: optimize94 state.assigned[prop] = trueMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 95–96why, one step back95 if (!state.modified) {96 if (Added polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1
line 97why, one step back97 (prop in state.base && is(state.base[prop], value)) ||`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760
line 98why, one step back98 (has(state.proxies, prop) && state.proxies[prop] === value)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 99–107why, one step back99 )100 return true101 markChanged(state)102 }103 state.copy[prop] = value104 return true105}106107function deleteProperty(state, prop) {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 108why, one step back108 state.assigned[prop] = falseMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 109–116why, one step back109 markChanged(state)110 delete state.copy[prop]111 return true112}113114function getOwnPropertyDescriptor(state, prop) {115 const owner = state.modified116 ? state.copy`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760
line 117why, one step back117 : has(state.proxies, prop) ? state.proxies : state.baseMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 118–125why, one step back118 const descriptor = Reflect.getOwnPropertyDescriptor(owner, prop)119 if (descriptor && !(Array.isArray(owner) && prop === "length"))120 descriptor.configurable = true121 return descriptor122}123124function defineProperty() {125 throw new Error(Merge pull request #144 from btnwtn/patch-1Michel Weststratebefore adoptionaaa12eb
line 126why, one step back126 "Immer does not support defining properties on draft objects."Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 127–132why, one step back127 )128}129130function markChanged(state) {131 if (!state.modified) {132 state.modified = trueMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 133–134why, one step back133 state.copy = shallowCopy(state.base)134 // copy the proxies over the base-copyMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 135–140why, one step back135 Object.assign(state.copy, state.proxies) // yup that works for arrays as well136 if (state.parent) markChanged(state.parent)137 }138}139140// creates a proxy for plain objects / arraysMerge branch 'patches'Michel Weststratebefore adoption2513fb8
line 141why, one step back141function createProxy(parentState, base, key) {Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b
line 142why, one step back142 if (isProxy(base)) throw new Error("Immer bug. Plz report.")Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 143why, one step back143 const state = createState(parentState, base, key)Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 144–147why, one step back144 const proxy = Array.isArray(base)145 ? Proxy.revocable([state], arrayTraps)146 : Proxy.revocable(state, objectTraps)147 proxies.push(proxy)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 148–150why, one step back148 return proxy.proxy149}150Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 151why, one step back151export function produceProxy(baseState, producer, patchListener) {Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b
lines 152–156why, one step back152 if (isProxy(baseState)) {153 // See #100, don't nest producers154 const returnValue = producer.call(baseState, baseState)155 return returnValue === undefined ? baseState : returnValue156 }Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 157–158why, one step back157 const previousProxies = proxies158 proxies = []Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 159–160why, one step back159 const patches = patchListener && []160 const inversePatches = patchListener && []Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 161–162why, one step back161 try {162 // create proxy for rootImplemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
line 163why, one step back163 const rootProxy = createProxy(undefined, baseState)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 164why, one step back164 // execute the thunkImplemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
line 165why, one step back165 const returnValue = producer.call(rootProxy, rootProxy)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 166why, one step back166 // and finalize the modified proxyFixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145
line 167why, one step back167 let resultImplemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
lines 168–172why, one step back168 // check whether the draft was modified and/or a value was returned169 if (returnValue !== undefined && returnValue !== rootProxy) {170 // something was returned, and it wasn't the proxy itself171 if (rootProxy[PROXY_STATE].modified)172 throw new Error(RETURNED_AND_MODIFIED_ERROR)Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145
lines 173–177why, one step back173174 // See #117175 // Should we just throw when returning a proxy which is not the root, but a subset of the original state?176 // Looks like a wrongly modeled reducer177 result = finalize(returnValue)Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 178–181why, one step back178 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 back182 } else {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 183why, one step back183 result = finalize(rootProxy, [], patches, inversePatches)Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
line 184why, one step back184 }Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 185why, one step back185 // revoke all proxiesMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 186why, one step back186 each(proxies, (_, p) => p.revoke())Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 187why, one step back187 patchListener && patchListener(patches, inversePatches)Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
line 188why, one step back188 return resultMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 189why, one step back189 } finally {Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 190why, one step back190 proxies = previousProxiesMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 191–192why, one step back191 }192}