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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149"use strict"// @ts-check
import { is, isProxyable, isProxy, freeze, PROXY_STATE, finalize, shallowCopy, verifyReturnValue, each} from "./common"
let proxies = null
const objectTraps = { get, has(target, prop) { return prop in source(target) }, ownKeys(target) { return Reflect.ownKeys(source(target)) }, set, deleteProperty, getOwnPropertyDescriptor, defineProperty, setPrototypeOf() { throw new Error("Don't even try this...") }}
const arrayTraps = {}each(objectTraps, (key, fn) => { arrayTraps[key] = function() { arguments[0] = arguments[0][0] return fn.apply(this, arguments) }})
function createState(parent, base) { return { modified: false, finalized: false, parent, base, copy: undefined, proxies: {} }}
function source(state) { return state.modified === true ? state.copy : state.base}
function get(state, prop) { if (prop === PROXY_STATE) return state if (state.modified) { const value = state.copy[prop] if (value === state.base[prop] && isProxyable(value)) // only create proxy if it is not yet a proxy, and not a new object // (new objects don't need proxying, they will be processed in finalize anyway) return (state.copy[prop] = createProxy(state, value)) return value } else { if (prop !== "constructor" && prop in state.proxies) return state.proxies[prop] const value = state.base[prop] if (!isProxy(value) && isProxyable(value)) return (state.proxies[prop] = createProxy(state, value)) return value }}
function set(state, prop, value) { if (!state.modified) { if ( (prop in state.base && is(state.base[prop], value)) || (prop in state.proxies && state.proxies[prop] === value) ) return true markChanged(state) } state.copy[prop] = value return true}
function deleteProperty(state, prop) { markChanged(state) delete state.copy[prop] return true}
function getOwnPropertyDescriptor(state, prop) { const owner = state.modified ? state.copy : prop in state.proxies ? state.proxies : state.base const descriptor = Reflect.getOwnPropertyDescriptor(owner, prop) if (descriptor && !(Array.isArray(owner) && prop === "length")) descriptor.configurable = true return descriptor}
function defineProperty() { throw new Error( "Immer does currently not support defining properties on draft objects" )}
function markChanged(state) { if (!state.modified) { state.modified = true state.copy = shallowCopy(state.base) // copy the proxies over the base-copy Object.assign(state.copy, state.proxies) // yup that works for arrays as well if (state.parent) markChanged(state.parent) }}
// creates a proxy for plain objects / arraysfunction createProxy(parentState, base) { const state = createState(parentState, base) const proxy = Array.isArray(base) ? Proxy.revocable([state], arrayTraps) : Proxy.revocable(state, objectTraps) proxies.push(proxy) return proxy.proxy}
export function produceProxy(baseState, producer) { const previousProxies = proxies proxies = [] try { // create proxy for root const rootClone = createProxy(undefined, baseState) // execute the thunk verifyReturnValue(producer(rootClone)) // and finalize the modified proxy const res = finalize(rootClone) // revoke all proxies each(proxies, (_, p) => p.revoke()) return res } finally { proxies = previousProxies }}