proxy.js
This file
Path
src/proxy.js
162 lines · 4.0 KiB · JavaScript
Last changed by
more code reuseMichel Weststratebefore adoption93372fc
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–8why, one step back4import {5 isProxyable,6 isProxy,7 freeze,8 PROXY_STATE,more code reuseMichel Weststratebefore adoption93372fc
line 9why, one step back9 finalize,some code cleanup / more reuseMichel Weststratebefore adoption9d393e3
lines 10–12why, one step back10 shallowCopy,11 verifyReturnValue,12 eachMerge branch 'master' into feature/fast-pruneMichel Weststratebefore adoptionf8f402c
line 13why, one step back13} from "./common"less builds is more?Michel Weststratebefore adoption74a97b6
line 14why, one step back14some code cleanup / more reuseMichel Weststratebefore adoption9d393e3
line 15why, one step back15let proxies = nullless builds is more?Michel Weststratebefore adoption74a97b6
lines 16–34why, one step back1617const objectTraps = {18 get,19 has(target, prop) {20 return prop in source(target)21 },22 ownKeys(target) {23 return Reflect.ownKeys(source(target))24 },25 set,26 deleteProperty,27 getOwnPropertyDescriptor,28 defineProperty,29 setPrototypeOf() {30 throw new Error("Don't even try this...")31 }32}3334const arrayTraps = {}some code cleanup / more reuseMichel Weststratebefore adoption9d393e3
line 35why, one step back35each(objectTraps, (key, fn) => {less builds is more?Michel Weststratebefore adoption74a97b6
lines 36–37why, one step back36 arrayTraps[key] = function() {37 arguments[0] = arguments[0][0]some code cleanup / more reuseMichel Weststratebefore adoption9d393e3
line 38why, one step back38 return fn.apply(this, arguments)less builds is more?Michel Weststratebefore adoption74a97b6
line 39why, one step back39 }some code cleanup / more reuseMichel Weststratebefore adoption9d393e3
line 40why, one step back40})less builds is more?Michel Weststratebefore adoption74a97b6
lines 41–110why, one step back4142function createState(parent, base) {43 return {44 modified: false,45 finalized: false,46 parent,47 base,48 copy: undefined,49 proxies: {}50 }51}5253function source(state) {54 return state.modified === true ? state.copy : state.base55}5657function get(state, prop) {58 if (prop === PROXY_STATE) return state59 if (state.modified) {60 const value = state.copy[prop]61 if (!isProxy(value) && isProxyable(value))62 return (state.copy[prop] = createProxy(state, value))63 return value64 } else {65 if (prop in state.proxies) return state.proxies[prop]66 const value = state.base[prop]67 if (!isProxy(value) && isProxyable(value))68 return (state.proxies[prop] = createProxy(state, value))69 return value70 }71}7273function set(state, prop, value) {74 if (!state.modified) {75 if (76 (prop in state.base && Object.is(state.base[prop], value)) ||77 (prop in state.proxies && state.proxies[prop] === value)78 )79 return true80 markChanged(state)81 }82 state.copy[prop] = value83 return true84}8586function deleteProperty(state, prop) {87 markChanged(state)88 delete state.copy[prop]89 return true90}9192function getOwnPropertyDescriptor(state, prop) {93 const owner = state.modified94 ? state.copy95 : prop in state.proxies ? state.proxies : state.base96 const descriptor = Reflect.getOwnPropertyDescriptor(owner, prop)97 if (descriptor && !(Array.isArray(owner) && prop === "length"))98 descriptor.configurable = true99 return descriptor100}101102function defineProperty() {103 throw new Error(104 "Immer does currently not support defining properties on draft objects"105 )106}107108function markChanged(state) {109 if (!state.modified) {110 state.modified = truesmall improvementsMichel Weststratebefore adoption7d0b7e8
line 111why, one step back111 state.copy = shallowCopy(state.base)some code cleanup / more reuseMichel Weststratebefore adoption9d393e3
line 112why, one step back112 // copy the proxies over the base-copyless builds is more?Michel Weststratebefore adoption74a97b6
lines 113–120why, one step back113 Object.assign(state.copy, state.proxies) // yup that works for arrays as well114 if (state.parent) markChanged(state.parent)115 }116}117118// creates a proxy for plain objects / arrays119function createProxy(parentState, base) {120 const state = createState(parentState, base)cleanupMichel Weststratebefore adoptiond536bc9
lines 121–123why, one step back121 const proxy = Array.isArray(base)122 ? Proxy.revocable([state], arrayTraps)123 : Proxy.revocable(state, objectTraps)some code cleanup / more reuseMichel Weststratebefore adoption9d393e3
line 124why, one step back124 proxies.push(proxy)less builds is more?Michel Weststratebefore adoption74a97b6
lines 125–127why, one step back125 return proxy.proxy126}127more code reuseMichel Weststratebefore adoption93372fc
line 128why, one step back128export function finalizeObject(state) {less builds is more?Michel Weststratebefore adoption74a97b6
lines 129–130why, one step back129 const copy = state.copy130 const base = state.basesome code cleanup / more reuseMichel Weststratebefore adoption9d393e3
lines 131–133why, one step back131 each(copy, (prop, value) => {132 if (value !== base[prop]) copy[prop] = finalize(value)133 })less builds is more?Michel Weststratebefore adoption74a97b6
lines 134–136why, one step back134 return freeze(copy)135}136more code reuseMichel Weststratebefore adoption93372fc
line 137why, one step back137export function finalizeArray(state) {less builds is more?Michel Weststratebefore adoption74a97b6
lines 138–139why, one step back138 const copy = state.copy139 const base = state.basesome code cleanup / more reuseMichel Weststratebefore adoption9d393e3
lines 140–142why, one step back140 each(copy, (i, value) => {141 if (value !== base[i]) copy[i] = finalize(value)142 })less builds is more?Michel Weststratebefore adoption74a97b6
lines 143–146why, one step back143 return freeze(copy)144}145146export function produceProxy(baseState, producer) {some code cleanup / more reuseMichel Weststratebefore adoption9d393e3
lines 147–148why, one step back147 const previousProxies = proxies148 proxies = []less builds is more?Michel Weststratebefore adoption74a97b6
lines 149–152why, one step back149 try {150 // create proxy for root151 const rootClone = createProxy(undefined, baseState)152 // execute the thunksome code cleanup / more reuseMichel Weststratebefore adoption9d393e3
line 153why, one step back153 verifyReturnValue(producer(rootClone))less builds is more?Michel Weststratebefore adoption74a97b6
lines 154–156why, one step back154 // and finalize the modified proxy155 const res = finalize(rootClone)156 // revoke all proxiessome code cleanup / more reuseMichel Weststratebefore adoption9d393e3
line 157why, one step back157 each(proxies, (_, p) => p.revoke())less builds is more?Michel Weststratebefore adoption74a97b6
lines 158–159why, one step back158 return res159 } finally {some code cleanup / more reuseMichel Weststratebefore adoption9d393e3
line 160why, one step back160 proxies = previousProxiesless builds is more?Michel Weststratebefore adoption74a97b6
lines 161–162why, one step back161 }162}