proxy.js
This file
Path
src/proxy.js
148 lines · 3.8 KiB · JavaScript
Last changed by
Immer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1
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–10why, one step back7 isProxyable,8 isProxy,9 freeze,10 PROXY_STATE,Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 11–14why, one step back11 finalize,12 shallowCopy,13 verifyReturnValue,14 eachFixed #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–36why, 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() {32 throw new Error("Don't even try this...")33 }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})Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 43–62why, one step back4344function createState(parent, base) {45 return {46 modified: false,47 finalized: false,48 parent,49 base,50 copy: undefined,51 proxies: {}52 }53}5455function source(state) {56 return state.modified === true ? state.copy : state.base57}5859function get(state, prop) {60 if (prop === PROXY_STATE) return state61 if (state.modified) {62 const value = state.copy[prop]Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
lines 63–65why, one step back63 if (value === state.base[prop] && isProxyable(value))64 // only create proxy if it is not yet a proxy, and not a new object65 // (new objects don't need proxying, they will be processed in finalize anyway)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 66–68why, one step back66 return (state.copy[prop] = createProxy(state, value))67 return value68 } else {`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760
line 69why, one step back69 if (has(state.proxies, prop)) 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)) ||`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760
line 81why, one step back81 (has(state.proxies, prop) && state.proxies[prop] === value)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 82–98why, one step back82 )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.copy`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760
line 99why, one step back99 : has(state.proxies, prop) ? state.proxies : state.baseMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 100–114why, one step back100 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 thunkImmer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1
line 139why, one step back139 verifyReturnValue(producer.call(rootClone, 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}