proxy.js
This file
Path
src/proxy.js
148 lines · 3.8 KiB · JavaScript
Last changed by
Merge pull request #81 from iruca3/fix/treating_constructor_on_proxyMichel Weststratebefore adoptionad5a86a
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,Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
lines 6–9why, one step back6 isProxyable,7 isProxy,8 freeze,9 PROXY_STATE,Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 10–13why, one step back10 finalize,11 shallowCopy,12 verifyReturnValue,13 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–35why, 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() {31 throw new Error("Don't even try this...")32 }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 {Merge pull request #81 from iruca3/fix/treating_constructor_on_proxyMichel Weststratebefore adoptionad5a86a
lines 68–69why, one step back68 if (prop !== "constructor" && prop in state.proxies)69 return state.proxies[prop]Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 70–79why, one step back70 const value = state.base[prop]71 if (!isProxy(value) && isProxyable(value))72 return (state.proxies[prop] = createProxy(state, value))73 return value74 }75}7677function set(state, prop, value) {78 if (!state.modified) {79 if (Added polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1
line 80why, one step back80 (prop in state.base && is(state.base[prop], value)) ||Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 81–114why, one step back81 (prop in state.proxies && state.proxies[prop] === value)82 )83 return true84 markChanged(state)85 }86 state.copy[prop] = value87 return true88}8990function deleteProperty(state, prop) {91 markChanged(state)92 delete state.copy[prop]93 return true94}9596function getOwnPropertyDescriptor(state, prop) {97 const owner = state.modified98 ? state.copy99 : prop in state.proxies ? state.proxies : state.base100 const descriptor = Reflect.getOwnPropertyDescriptor(owner, prop)101 if (descriptor && !(Array.isArray(owner) && prop === "length"))102 descriptor.configurable = true103 return descriptor104}105106function defineProperty() {107 throw new Error(108 "Immer does currently not support defining properties on draft objects"109 )110}111112function markChanged(state) {113 if (!state.modified) {114 state.modified = trueMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 115–116why, one step back115 state.copy = shallowCopy(state.base)116 // copy the proxies over the base-copyMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 117–124why, one step back117 Object.assign(state.copy, state.proxies) // yup that works for arrays as well118 if (state.parent) markChanged(state.parent)119 }120}121122// creates a proxy for plain objects / arrays123function createProxy(parentState, base) {124 const state = createState(parentState, base)Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 125–128why, one step back125 const proxy = Array.isArray(base)126 ? Proxy.revocable([state], arrayTraps)127 : Proxy.revocable(state, objectTraps)128 proxies.push(proxy)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 129–132why, one step back129 return proxy.proxy130}131132export function produceProxy(baseState, producer) {Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 133–134why, one step back133 const previousProxies = proxies134 proxies = []Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 135–138why, one step back135 try {136 // create proxy for root137 const rootClone = createProxy(undefined, baseState)138 // execute the thunkMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 139why, one step back139 verifyReturnValue(producer(rootClone))Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 140–142why, one step back140 // and finalize the modified proxy141 const res = finalize(rootClone)142 // revoke all proxiesMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 143why, one step back143 each(proxies, (_, p) => p.revoke())Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 144–145why, one step back144 return res145 } finally {Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 146why, one step back146 proxies = previousProxiesMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 147–148why, one step back147 }148}