proxy.js
This file
Path
src/proxy.js
184 lines · 4.9 KiB · JavaScript
Last changed by
small improvementsMichel Weststratebefore adoption7d0b7e8
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
lines 10–11why, one step back10 finalizeNonProxiedObject,11 shallowCopyMerge branch 'master' into feature/fast-pruneMichel Weststratebefore adoptionf8f402c
line 12why, one step back12} from "./common"less builds is more?Michel Weststratebefore adoption74a97b6
lines 13–109why, one step back1314let revocableProxies = null1516const objectTraps = {17 get,18 has(target, prop) {19 return prop in source(target)20 },21 ownKeys(target) {22 return Reflect.ownKeys(source(target))23 },24 set,25 deleteProperty,26 getOwnPropertyDescriptor,27 defineProperty,28 setPrototypeOf() {29 throw new Error("Don't even try this...")30 }31}3233const arrayTraps = {}34for (let key in objectTraps) {35 arrayTraps[key] = function() {36 arguments[0] = arguments[0][0]37 return objectTraps[key].apply(this, arguments)38 }39}4041function createState(parent, base) {42 return {43 modified: false,44 finalized: false,45 parent,46 base,47 copy: undefined,48 proxies: {}49 }50}5152function source(state) {53 return state.modified === true ? state.copy : state.base54}5556function get(state, prop) {57 if (prop === PROXY_STATE) return state58 if (state.modified) {59 const value = state.copy[prop]60 if (!isProxy(value) && isProxyable(value))61 return (state.copy[prop] = createProxy(state, value))62 return value63 } else {64 if (prop in state.proxies) return state.proxies[prop]65 const value = state.base[prop]66 if (!isProxy(value) && isProxyable(value))67 return (state.proxies[prop] = createProxy(state, value))68 return value69 }70}7172function set(state, prop, value) {73 if (!state.modified) {74 if (75 (prop in state.base && Object.is(state.base[prop], value)) ||76 (prop in state.proxies && state.proxies[prop] === value)77 )78 return true79 markChanged(state)80 }81 state.copy[prop] = value82 return true83}8485function deleteProperty(state, prop) {86 markChanged(state)87 delete state.copy[prop]88 return true89}9091function getOwnPropertyDescriptor(state, prop) {92 const owner = state.modified93 ? state.copy94 : prop in state.proxies ? state.proxies : state.base95 const descriptor = Reflect.getOwnPropertyDescriptor(owner, prop)96 if (descriptor && !(Array.isArray(owner) && prop === "length"))97 descriptor.configurable = true98 return descriptor99}100101function defineProperty() {102 throw new Error(103 "Immer does currently not support defining properties on draft objects"104 )105}106107function markChanged(state) {108 if (!state.modified) {109 state.modified = truesmall improvementsMichel Weststratebefore adoption7d0b7e8
line 110why, one step back110 state.copy = shallowCopy(state.base)less builds is more?Michel Weststratebefore adoption74a97b6
lines 111–139why, one step back111 Object.assign(state.copy, state.proxies) // yup that works for arrays as well112 if (state.parent) markChanged(state.parent)113 }114}115116// creates a proxy for plain objects / arrays117function createProxy(parentState, base) {118 const state = createState(parentState, base)119 let proxy120 if (Array.isArray(base)) {121 proxy = Proxy.revocable([state], arrayTraps)122 } else {123 proxy = Proxy.revocable(state, objectTraps)124 }125 revocableProxies.push(proxy)126 return proxy.proxy127}128129// given a base object, returns it if unmodified, or return the changed cloned if modified130function finalize(base) {131 if (isProxy(base)) {132 const state = base[PROXY_STATE]133 if (state.modified === true) {134 if (state.finalized === true) return state.copy135 state.finalized = true136 if (Array.isArray(state.base)) return finalizeArray(state)137 return finalizeObject(state)138 } else return state.base139 } else if (base !== null && typeof base === "object") {Merge branch 'master' into feature/fast-pruneMichel Weststratebefore adoptionf8f402c
line 140why, one step back140 finalizeNonProxiedObject(base, finalize)less builds is more?Michel Weststratebefore adoption74a97b6
lines 141–184why, one step back141 }142 return base143}144145function finalizeObject(state) {146 const copy = state.copy147 const base = state.base148 for (var prop in copy) {149 if (copy[prop] !== base[prop]) copy[prop] = finalize(copy[prop])150 }151 return freeze(copy)152}153154function finalizeArray(state) {155 const copy = state.copy156 const base = state.base157 for (let i = 0; i < copy.length; i++) {158 if (copy[i] !== base[i]) copy[i] = finalize(copy[i])159 }160 return freeze(copy)161}162163export function produceProxy(baseState, producer) {164 const previousProxies = revocableProxies165 revocableProxies = []166 try {167 // create proxy for root168 const rootClone = createProxy(undefined, baseState)169 // execute the thunk170 const maybeVoidReturn = producer(rootClone)171 //values either than undefined will trigger warning;172 !Object.is(maybeVoidReturn, undefined) &&173 console.warn(174 `Immer callback expects no return value. However ${typeof maybeVoidReturn} was returned`175 )176 // and finalize the modified proxy177 const res = finalize(rootClone)178 // revoke all proxies179 revocableProxies.forEach(p => p.revoke())180 return res181 } finally {182 revocableProxies = previousProxies183 }184}