Skip to content
Myr

proxy.js

This file

Path

src/proxy.js

179 lines · 4.6 KiB · JavaScript

Last changed by

cleanupMichel Weststratebefore adoptiond536bc9

History of this file

Take it

less builds is more?Michel Weststratebefore adoption74a97b6

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

Merge branch 'master' into feature/fast-pruneMichel Weststratebefore adoptionf8f402c

lines 4–9why, one step back
4import {
5 autoFreeze,6 isProxyable,7 isProxy,8 freeze,9 PROXY_STATE,

small improvementsMichel Weststratebefore adoption7d0b7e8

line 10why, one step back
10 finalizeNonProxiedObject,

some code cleanup / more reuseMichel Weststratebefore adoption9d393e3

lines 11–13why, one step back
11 shallowCopy,
12 verifyReturnValue,13 each

Merge branch 'master' into feature/fast-pruneMichel Weststratebefore adoptionf8f402c

line 14why, one step back
14} from "./common"

less builds is more?Michel Weststratebefore adoption74a97b6

line 15why, one step back
15

some code cleanup / more reuseMichel Weststratebefore adoption9d393e3

line 16why, one step back
16let proxies = null

less builds is more?Michel Weststratebefore adoption74a97b6

lines 17–35why, one step back
17
18const 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 back
36each(objectTraps, (key, fn) => {

less builds is more?Michel Weststratebefore adoption74a97b6

lines 37–38why, one step back
37 arrayTraps[key] = function() {
38 arguments[0] = arguments[0][0]

some code cleanup / more reuseMichel Weststratebefore adoption9d393e3

line 39why, one step back
39 return fn.apply(this, arguments)

less builds is more?Michel Weststratebefore adoption74a97b6

line 40why, one step back
40 }

some code cleanup / more reuseMichel Weststratebefore adoption9d393e3

line 41why, one step back
41})

less builds is more?Michel Weststratebefore adoption74a97b6

lines 42–111why, one step back
42
43function 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 = true

small improvementsMichel Weststratebefore adoption7d0b7e8

line 112why, one step back
112 state.copy = shallowCopy(state.base)

some code cleanup / more reuseMichel Weststratebefore adoption9d393e3

line 113why, one step back
113 // copy the proxies over the base-copy

less builds is more?Michel Weststratebefore adoption74a97b6

lines 114–121why, one step back
114 Object.assign(state.copy, state.proxies) // yup that works for arrays as well
115 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)

cleanupMichel Weststratebefore adoptiond536bc9

lines 122–124why, one step back
122 const proxy = Array.isArray(base)
123 ? Proxy.revocable([state], arrayTraps)124 : Proxy.revocable(state, objectTraps)

some code cleanup / more reuseMichel Weststratebefore adoption9d393e3

line 125why, one step back
125 proxies.push(proxy)

less builds is more?Michel Weststratebefore adoption74a97b6

lines 126–139why, one step back
126 return proxy.proxy
127}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 back
140 finalizeNonProxiedObject(base, finalize)

less builds is more?Michel Weststratebefore adoption74a97b6

lines 141–147why, one step back
141 }
142 return base143}144145function finalizeObject(state) {146 const copy = state.copy147 const base = state.base

some code cleanup / more reuseMichel Weststratebefore adoption9d393e3

lines 148–150why, one step back
148 each(copy, (prop, value) => {
149 if (value !== base[prop]) copy[prop] = finalize(value)150 })

less builds is more?Michel Weststratebefore adoption74a97b6

lines 151–156why, one step back
151 return freeze(copy)
152}153154function finalizeArray(state) {155 const copy = state.copy156 const base = state.base

some code cleanup / more reuseMichel Weststratebefore adoption9d393e3

lines 157–159why, one step back
157 each(copy, (i, value) => {
158 if (value !== base[i]) copy[i] = finalize(value)159 })

less builds is more?Michel Weststratebefore adoption74a97b6

lines 160–163why, one step back
160 return freeze(copy)
161}162163export function produceProxy(baseState, producer) {

some code cleanup / more reuseMichel Weststratebefore adoption9d393e3

lines 164–165why, one step back
164 const previousProxies = proxies
165 proxies = []

less builds is more?Michel Weststratebefore adoption74a97b6

lines 166–169why, one step back
166 try {
167 // create proxy for root168 const rootClone = createProxy(undefined, baseState)169 // execute the thunk

some code cleanup / more reuseMichel Weststratebefore adoption9d393e3

line 170why, one step back
170 verifyReturnValue(producer(rootClone))

less builds is more?Michel Weststratebefore adoption74a97b6

lines 171–173why, one step back
171 // and finalize the modified proxy
172 const res = finalize(rootClone)173 // revoke all proxies

some code cleanup / more reuseMichel Weststratebefore adoption9d393e3

line 174why, one step back
174 each(proxies, (_, p) => p.revoke())

less builds is more?Michel Weststratebefore adoption74a97b6

lines 175–176why, one step back
175 return res
176 } finally {

some code cleanup / more reuseMichel Weststratebefore adoption9d393e3

line 177why, one step back
177 proxies = previousProxies

less builds is more?Michel Weststratebefore adoption74a97b6

lines 178–179why, one step back
178 }
179}