proxy.js
This file
Path
src/proxy.js
182 lines · 4.6 KiB · JavaScript
Last changed by
some code cleanup / more reuseMichel Weststratebefore adoption9d393e3
History of this fileTake it
less builds is more?Michel Weststratebefore adoption74a97b6
lines 1–3why, one step back1"use strict"2// @ts-check3Merge branch 'master' into feature/fast-pruneMichel Weststratebefore adoptionf8f402c
lines 4–9why, one step back4import {5 autoFreeze,6 isProxyable,7 isProxy,8 freeze,9 PROXY_STATE,small improvementsMichel Weststratebefore adoption7d0b7e8
line 10why, one step back10 finalizeNonProxiedObject,some code cleanup / more reuseMichel Weststratebefore adoption9d393e3
lines 11–13why, one step back11 shallowCopy,12 verifyReturnValue,13 eachMerge branch 'master' into feature/fast-pruneMichel Weststratebefore adoptionf8f402c
line 14why, one step back14} from "./common"less builds is more?Michel Weststratebefore adoption74a97b6
line 15why, one step back15some code cleanup / more reuseMichel Weststratebefore adoption9d393e3
line 16why, one step back16let proxies = nullless builds is more?Michel Weststratebefore adoption74a97b6
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 = {}some code cleanup / more reuseMichel Weststratebefore adoption9d393e3
line 36why, one step back36each(objectTraps, (key, fn) => {less builds is more?Michel Weststratebefore adoption74a97b6
lines 37–38why, one step back37 arrayTraps[key] = function() {38 arguments[0] = arguments[0][0]some code cleanup / more reuseMichel Weststratebefore adoption9d393e3
line 39why, one step back39 return fn.apply(this, arguments)less builds is more?Michel Weststratebefore adoption74a97b6
line 40why, one step back40 }some code cleanup / more reuseMichel Weststratebefore adoption9d393e3
line 41why, one step back41})less builds is more?Michel Weststratebefore adoption74a97b6
lines 42–111why, 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]62 if (!isProxy(value) && isProxyable(value))63 return (state.copy[prop] = createProxy(state, value))64 return value65 } else {66 if (prop in state.proxies) return state.proxies[prop]67 const value = state.base[prop]68 if (!isProxy(value) && isProxyable(value))69 return (state.proxies[prop] = createProxy(state, value))70 return value71 }72}7374function set(state, prop, value) {75 if (!state.modified) {76 if (77 (prop in state.base && Object.is(state.base[prop], value)) ||78 (prop in state.proxies && state.proxies[prop] === value)79 )80 return true81 markChanged(state)82 }83 state.copy[prop] = value84 return true85}8687function deleteProperty(state, prop) {88 markChanged(state)89 delete state.copy[prop]90 return true91}9293function getOwnPropertyDescriptor(state, prop) {94 const owner = state.modified95 ? state.copy96 : prop in state.proxies ? state.proxies : state.base97 const descriptor = Reflect.getOwnPropertyDescriptor(owner, prop)98 if (descriptor && !(Array.isArray(owner) && prop === "length"))99 descriptor.configurable = true100 return descriptor101}102103function defineProperty() {104 throw new Error(105 "Immer does currently not support defining properties on draft objects"106 )107}108109function markChanged(state) {110 if (!state.modified) {111 state.modified = truesmall improvementsMichel Weststratebefore adoption7d0b7e8
line 112why, one step back112 state.copy = shallowCopy(state.base)some code cleanup / more reuseMichel Weststratebefore adoption9d393e3
line 113why, one step back113 // copy the proxies over the base-copyless builds is more?Michel Weststratebefore adoption74a97b6
lines 114–127why, one step back114 Object.assign(state.copy, state.proxies) // yup that works for arrays as well115 if (state.parent) markChanged(state.parent)116 }117}118119// creates a proxy for plain objects / arrays120function createProxy(parentState, base) {121 const state = createState(parentState, base)122 let proxy123 if (Array.isArray(base)) {124 proxy = Proxy.revocable([state], arrayTraps)125 } else {126 proxy = Proxy.revocable(state, objectTraps)127 }some code cleanup / more reuseMichel Weststratebefore adoption9d393e3
line 128why, one step back128 proxies.push(proxy)less builds is more?Michel Weststratebefore adoption74a97b6
lines 129–142why, one step back129 return proxy.proxy130}131132// given a base object, returns it if unmodified, or return the changed cloned if modified133function finalize(base) {134 if (isProxy(base)) {135 const state = base[PROXY_STATE]136 if (state.modified === true) {137 if (state.finalized === true) return state.copy138 state.finalized = true139 if (Array.isArray(state.base)) return finalizeArray(state)140 return finalizeObject(state)141 } else return state.base142 } else if (base !== null && typeof base === "object") {Merge branch 'master' into feature/fast-pruneMichel Weststratebefore adoptionf8f402c
line 143why, one step back143 finalizeNonProxiedObject(base, finalize)less builds is more?Michel Weststratebefore adoption74a97b6
lines 144–150why, one step back144 }145 return base146}147148function finalizeObject(state) {149 const copy = state.copy150 const base = state.basesome code cleanup / more reuseMichel Weststratebefore adoption9d393e3
lines 151–153why, one step back151 each(copy, (prop, value) => {152 if (value !== base[prop]) copy[prop] = finalize(value)153 })less builds is more?Michel Weststratebefore adoption74a97b6
lines 154–159why, one step back154 return freeze(copy)155}156157function finalizeArray(state) {158 const copy = state.copy159 const base = state.basesome code cleanup / more reuseMichel Weststratebefore adoption9d393e3
lines 160–162why, one step back160 each(copy, (i, value) => {161 if (value !== base[i]) copy[i] = finalize(value)162 })less builds is more?Michel Weststratebefore adoption74a97b6
lines 163–166why, one step back163 return freeze(copy)164}165166export function produceProxy(baseState, producer) {some code cleanup / more reuseMichel Weststratebefore adoption9d393e3
lines 167–168why, one step back167 const previousProxies = proxies168 proxies = []less builds is more?Michel Weststratebefore adoption74a97b6
lines 169–172why, one step back169 try {170 // create proxy for root171 const rootClone = createProxy(undefined, baseState)172 // execute the thunksome code cleanup / more reuseMichel Weststratebefore adoption9d393e3
line 173why, one step back173 verifyReturnValue(producer(rootClone))less builds is more?Michel Weststratebefore adoption74a97b6
lines 174–176why, one step back174 // and finalize the modified proxy175 const res = finalize(rootClone)176 // revoke all proxiessome code cleanup / more reuseMichel Weststratebefore adoption9d393e3
line 177why, one step back177 each(proxies, (_, p) => p.revoke())less builds is more?Michel Weststratebefore adoption74a97b6
lines 178–179why, one step back178 return res179 } finally {some code cleanup / more reuseMichel Weststratebefore adoption9d393e3
line 180why, one step back180 proxies = previousProxiesless builds is more?Michel Weststratebefore adoption74a97b6
lines 181–182why, one step back181 }182}