proxy.js
This file
Path
src/proxy.js
188 lines · 5.5 KiB · JavaScript
Last changed by
fix: always use `assign` polyfillaleclarsonbefore adoption241a648
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 {fix: always use `assign` polyfillaleclarsonbefore adoption241a648
lines 5–6why, one step back5 assign,6 each,`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760
line 7why, one step back7 has,fix: always use `assign` polyfillaleclarsonbefore adoption241a648
line 8why, one step back8 is,Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
lines 9–10why, one step back9 isProxyable,10 isProxy,Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 11–12why, one step back11 finalize,12 shallowCopy,fix: always use `assign` polyfillaleclarsonbefore adoption241a648
lines 13–14why, one step back13 PROXY_STATE,14 RETURNED_AND_MODIFIED_ERRORFixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
line 15why, one step back15} from "./common"Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 16why, one step back16Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 17why, one step back17let proxies = nullMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 18–31why, one step back1819const 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 back32 throw new Error("Immer does not support `setPrototypeOf()`.")Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 33–36why, one step back33 }34}3536const arrayTraps = {}Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 37why, one step back37each(objectTraps, (key, fn) => {Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 38–39why, one step back38 arrayTraps[key] = function() {39 arguments[0] = arguments[0][0]Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 40why, one step back40 return fn.apply(this, arguments)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 41why, one step back41 }Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 42why, one step back42})Be explicit about not supporting arbitrary properties on arraysMichel Weststratebefore adoption0a80c2c
lines 43–57why, one step back43arrayTraps.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 back5859function createState(parent, base) {60 return {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 61–62why, one step back61 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 removedMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 63–78why, one step back63 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 back79 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 back82 return (state.copy[prop] = createProxy(state, value))83 return value84 } else {`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760
line 85why, one step back85 if (has(state.proxies, prop)) return state.proxies[prop]Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 86–93why, one step back86 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) {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 94–95why, one step back94 // TODO: optimize95 state.assigned[prop] = trueMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 96–97why, one step back96 if (!state.modified) {97 if (Added polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1
line 98why, one step back98 (prop in state.base && is(state.base[prop], value)) ||`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760
line 99why, one step back99 (has(state.proxies, prop) && state.proxies[prop] === value)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 100–108why, one step back100 )101 return true102 markChanged(state)103 }104 state.copy[prop] = value105 return true106}107108function deleteProperty(state, prop) {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 109why, one step back109 state.assigned[prop] = falseMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 110–117why, one step back110 markChanged(state)111 delete state.copy[prop]112 return true113}114115function getOwnPropertyDescriptor(state, prop) {116 const owner = state.modified117 ? state.copyMerge pull request #200 from stefanwille/upgrade-prettierMichel Weststratebefore adoption1139f57
lines 118–120why, one step back118 : has(state.proxies, prop)119 ? state.proxies120 : state.baseMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 121–128why, one step back121 const descriptor = Reflect.getOwnPropertyDescriptor(owner, prop)122 if (descriptor && !(Array.isArray(owner) && prop === "length"))123 descriptor.configurable = true124 return descriptor125}126127function defineProperty() {128 throw new Error(Merge pull request #144 from btnwtn/patch-1Michel Weststratebefore adoptionaaa12eb
line 129why, one step back129 "Immer does not support defining properties on draft objects."Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 130–135why, one step back130 )131}132133function markChanged(state) {134 if (!state.modified) {135 state.modified = trueMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 136–137why, one step back136 state.copy = shallowCopy(state.base)137 // copy the proxies over the base-copyfix: always use `assign` polyfillaleclarsonbefore adoption241a648
line 138why, one step back138 assign(state.copy, state.proxies) // yup that works for arrays as wellMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 139–143why, one step back139 if (state.parent) markChanged(state.parent)140 }141}142143// creates a proxy for plain objects / arraysRemove unused parameter (#206)Stevenbefore adoption09d69ae
line 144why, one step back144function createProxy(parentState, base) {Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b
line 145why, one step back145 if (isProxy(base)) throw new Error("Immer bug. Plz report.")Remove unused parameter (#206)Stevenbefore adoption09d69ae
line 146why, one step back146 const state = createState(parentState, base)Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 147–150why, one step back147 const proxy = Array.isArray(base)148 ? Proxy.revocable([state], arrayTraps)149 : Proxy.revocable(state, objectTraps)150 proxies.push(proxy)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 151–153why, one step back151 return proxy.proxy152}153Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 154why, one step back154export function produceProxy(baseState, producer, patchListener) {Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 155–156why, one step back155 const previousProxies = proxies156 proxies = []Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 157–158why, one step back157 const patches = patchListener && []158 const inversePatches = patchListener && []Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 159–160why, one step back159 try {160 // create proxy for rootImplemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
line 161why, one step back161 const rootProxy = createProxy(undefined, baseState)nit: dedupe some codealeclarsonbefore adoptionc9e1d98
line 162why, one step back162 // execute the producer functionImplemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
line 163why, one step back163 const returnValue = producer.call(rootProxy, rootProxy)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 164why, one step back164 // and finalize the modified proxyFixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145
line 165why, one step back165 let resultImplemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
lines 166–170why, one step back166 // check whether the draft was modified and/or a value was returned167 if (returnValue !== undefined && returnValue !== rootProxy) {168 // something was returned, and it wasn't the proxy itself169 if (rootProxy[PROXY_STATE].modified)170 throw new Error(RETURNED_AND_MODIFIED_ERROR)Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145
line 171why, one step back171nit: dedupe some codealeclarsonbefore adoptionc9e1d98
line 172why, one step back172 // we need to finalize the return value in case it's a subset of the draftFixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145
line 173why, one step back173 result = finalize(returnValue)Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 174–177why, one step back174 if (patches) {175 patches.push({op: "replace", path: [], value: result})176 inversePatches.push({op: "replace", path: [], value: baseState})177 }Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145
line 178why, one step back178 } else {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 179why, one step back179 result = finalize(rootProxy, [], patches, inversePatches)Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
line 180why, one step back180 }Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 181why, one step back181 // revoke all proxiesMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 182why, one step back182 each(proxies, (_, p) => p.revoke())Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 183why, one step back183 patchListener && patchListener(patches, inversePatches)Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
line 184why, one step back184 return resultMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 185why, one step back185 } finally {Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 186why, one step back186 proxies = previousProxiesMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 187–188why, one step back187 }188}