Skip to content
Myr

proxy.js

This file

Path

src/proxy.js

185 lines · 5.0 KiB · JavaScript

Last changed by

Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a

History of this file

Take it

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 1–3why, one step back
1"use strict"
2// @ts-check3

Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a

lines 4–11why, one step back
4import {
5 autoFreeze,6 isProxyable,7 isProxy,8 freeze,9 PROXY_STATE,10 finalizeNonProxiedObject11} from "./common"

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 12–140why, one step back
12
13let revocableProxies = null1415const objectTraps = {16 get,17 has(target, prop) {18 return prop in source(target)19 },20 ownKeys(target) {21 return Reflect.ownKeys(source(target))22 },23 set,24 deleteProperty,25 getOwnPropertyDescriptor,26 defineProperty,27 setPrototypeOf() {28 throw new Error("Don't even try this...")29 }30}3132const arrayTraps = {}33for (let key in objectTraps) {34 arrayTraps[key] = function() {35 arguments[0] = arguments[0][0]36 return objectTraps[key].apply(this, arguments)37 }38}3940function createState(parent, base) {41 return {42 modified: false,43 finalized: false,44 parent,45 base,46 copy: undefined,47 proxies: {}48 }49}5051function source(state) {52 return state.modified === true ? state.copy : state.base53}5455function get(state, prop) {56 if (prop === PROXY_STATE) return state57 if (state.modified) {58 const value = state.copy[prop]59 if (!isProxy(value) && isProxyable(value))60 return (state.copy[prop] = createProxy(state, value))61 return value62 } else {63 if (prop in state.proxies) return state.proxies[prop]64 const value = state.base[prop]65 if (!isProxy(value) && isProxyable(value))66 return (state.proxies[prop] = createProxy(state, value))67 return value68 }69}7071function set(state, prop, value) {72 if (!state.modified) {73 if (74 (prop in state.base && Object.is(state.base[prop], value)) ||75 (prop in state.proxies && state.proxies[prop] === value)76 )77 return true78 markChanged(state)79 }80 state.copy[prop] = value81 return true82}8384function deleteProperty(state, prop) {85 markChanged(state)86 delete state.copy[prop]87 return true88}8990function getOwnPropertyDescriptor(state, prop) {91 const owner = state.modified92 ? state.copy93 : prop in state.proxies ? state.proxies : state.base94 const descriptor = Reflect.getOwnPropertyDescriptor(owner, prop)95 if (descriptor && !(Array.isArray(owner) && prop === "length"))96 descriptor.configurable = true97 return descriptor98}99100function defineProperty() {101 throw new Error(102 "Immer does currently not support defining properties on draft objects"103 )104}105106function markChanged(state) {107 if (!state.modified) {108 state.modified = true109 state.copy = Array.isArray(state.base)110 ? state.base.slice()111 : Object.assign({}, state.base) // TODO: eliminate those isArray checks?112 Object.assign(state.copy, state.proxies) // yup that works for arrays as well113 if (state.parent) markChanged(state.parent)114 }115}116117// creates a proxy for plain objects / arrays118function createProxy(parentState, base) {119 const state = createState(parentState, base)120 let proxy121 if (Array.isArray(base)) {122 proxy = Proxy.revocable([state], arrayTraps)123 } else {124 proxy = Proxy.revocable(state, objectTraps)125 }126 revocableProxies.push(proxy)127 return proxy.proxy128}129130// given a base object, returns it if unmodified, or return the changed cloned if modified131function finalize(base) {132 if (isProxy(base)) {133 const state = base[PROXY_STATE]134 if (state.modified === true) {135 if (state.finalized === true) return state.copy136 state.finalized = true137 if (Array.isArray(state.base)) return finalizeArray(state)138 return finalizeObject(state)139 } else return state.base140 } else if (base !== null && typeof base === "object") {

Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a

line 141why, one step back
141 finalizeNonProxiedObject(base, finalize)

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 142–185why, one step back
142 }
143 return base144}145146function finalizeObject(state) {147 const copy = state.copy148 const base = state.base149 for (var prop in copy) {150 if (copy[prop] !== base[prop]) copy[prop] = finalize(copy[prop])151 }152 return freeze(copy)153}154155function finalizeArray(state) {156 const copy = state.copy157 const base = state.base158 for (let i = 0; i < copy.length; i++) {159 if (copy[i] !== base[i]) copy[i] = finalize(copy[i])160 }161 return freeze(copy)162}163164export function produceProxy(baseState, producer) {165 const previousProxies = revocableProxies166 revocableProxies = []167 try {168 // create proxy for root169 const rootClone = createProxy(undefined, baseState)170 // execute the thunk171 const maybeVoidReturn = producer(rootClone)172 //values either than undefined will trigger warning;173 !Object.is(maybeVoidReturn, undefined) &&174 console.warn(175 `Immer callback expects no return value. However ${typeof maybeVoidReturn} was returned`176 )177 // and finalize the modified proxy178 const res = finalize(rootClone)179 // revoke all proxies180 revocableProxies.forEach(p => p.revoke())181 return res182 } finally {183 revocableProxies = previousProxies184 }185}