proxy.js
This file
Path
src/proxy.js
160 lines · 4.4 KiB · JavaScript
Last changed by
Merge pull request #144 from btnwtn/patch-1Michel Weststratebefore adoptionaaa12eb
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})Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 42–61why, one step back4243function createState(parent, base) {44 return {45 modified: false,46 finalized: false,47 parent,48 base,49 copy: undefined,50 proxies: {}51 }52}5354function source(state) {55 return state.modified === true ? state.copy : state.base56}5758function get(state, prop) {59 if (prop === PROXY_STATE) return state60 if (state.modified) {61 const value = state.copy[prop]Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
lines 62–64why, one step back62 if (value === state.base[prop] && isProxyable(value))63 // only create proxy if it is not yet a proxy, and not a new object64 // (new objects don't need proxying, they will be processed in finalize anyway)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 65–67why, one step back65 return (state.copy[prop] = createProxy(state, value))66 return value67 } else {`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760
line 68why, one step back68 if (has(state.proxies, prop)) return state.proxies[prop]Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 69–78why, one step back69 const value = state.base[prop]70 if (!isProxy(value) && isProxyable(value))71 return (state.proxies[prop] = createProxy(state, value))72 return value73 }74}7576function set(state, prop, value) {77 if (!state.modified) {78 if (Added polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1
line 79why, one step back79 (prop in state.base && is(state.base[prop], value)) ||`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760
line 80why, one step back80 (has(state.proxies, prop) && state.proxies[prop] === value)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 81–97why, one step back81 )82 return true83 markChanged(state)84 }85 state.copy[prop] = value86 return true87}8889function deleteProperty(state, prop) {90 markChanged(state)91 delete state.copy[prop]92 return true93}9495function getOwnPropertyDescriptor(state, prop) {96 const owner = state.modified97 ? state.copy`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760
line 98why, one step back98 : has(state.proxies, prop) ? state.proxies : state.baseMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 99–106why, one step back99 const descriptor = Reflect.getOwnPropertyDescriptor(owner, prop)100 if (descriptor && !(Array.isArray(owner) && prop === "length"))101 descriptor.configurable = true102 return descriptor103}104105function defineProperty() {106 throw new Error(Merge pull request #144 from btnwtn/patch-1Michel Weststratebefore adoptionaaa12eb
line 107why, one step back107 "Immer does not support defining properties on draft objects."Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 108–113why, one step back108 )109}110111function markChanged(state) {112 if (!state.modified) {113 state.modified = trueMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 114–115why, one step back114 state.copy = shallowCopy(state.base)115 // copy the proxies over the base-copyMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 116–123why, one step back116 Object.assign(state.copy, state.proxies) // yup that works for arrays as well117 if (state.parent) markChanged(state.parent)118 }119}120121// creates a proxy for plain objects / arrays122function createProxy(parentState, base) {123 const state = createState(parentState, base)Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 124–127why, one step back124 const proxy = Array.isArray(base)125 ? Proxy.revocable([state], arrayTraps)126 : Proxy.revocable(state, objectTraps)127 proxies.push(proxy)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 128–131why, one step back128 return proxy.proxy129}130131export function produceProxy(baseState, producer) {Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 132–133why, one step back132 const previousProxies = proxies133 proxies = []Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 134–135why, one step back134 try {135 // create proxy for rootImplemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
line 136why, one step back136 const rootProxy = createProxy(undefined, baseState)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 137why, one step back137 // execute the thunkImplemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
line 138why, one step back138 const returnValue = producer.call(rootProxy, rootProxy)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 139why, one step back139 // and finalize the modified proxyFixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145
line 140why, one step back140 let resultImplemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
lines 141–145why, one step back141 // check whether the draft was modified and/or a value was returned142 if (returnValue !== undefined && returnValue !== rootProxy) {143 // something was returned, and it wasn't the proxy itself144 if (rootProxy[PROXY_STATE].modified)145 throw new Error(RETURNED_AND_MODIFIED_ERROR)Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145
lines 146–152why, one step back146147 // See #117148 // Should we just throw when returning a proxy which is not the root, but a subset of the original state?149 // Looks like a wrongly modeled reducer150 result = finalize(returnValue)151 } else {152 result = finalize(rootProxy)Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
line 153why, one step back153 }Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 154why, one step back154 // revoke all proxiesMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 155why, one step back155 each(proxies, (_, p) => p.revoke())Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
line 156why, one step back156 return resultMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 157why, one step back157 } finally {Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 158why, one step back158 proxies = previousProxiesMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 159–160why, one step back159 }160}