proxy.js
This file
Path
src/proxy.js
162 lines · 3.9 KiB · JavaScript
Last changed by
Added polyfill for Object.is, fixes #77Michel Weststratebefore adoption25d7274
History of this fileTake it
Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 1–3why, one step back1"use strict"2// @ts-check3Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
line 4why, one step back4import {Added polyfill for Object.is, fixes #77Michel Weststratebefore adoption25d7274
line 5why, one step back5 is,Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
lines 6–9why, one step back6 isProxyable,7 isProxy,8 freeze,9 PROXY_STATE,Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 10–13why, one step back10 finalize,11 shallowCopy,12 verifyReturnValue,13 eachFixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
line 14why, one step back14} from "./common"Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 15why, one step back15Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 16why, one step back16let proxies = nullMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 17–35why, one step back1718const 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 = {}Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 36why, one step back36each(objectTraps, (key, fn) => {Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 37–38why, one step back37 arrayTraps[key] = function() {38 arguments[0] = arguments[0][0]Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 39why, one step back39 return fn.apply(this, arguments)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 40why, one step back40 }Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 41why, one step back41})Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 42–76why, one step back4243function 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 (Added polyfill for Object.is, fixes #77Michel Weststratebefore adoption25d7274
line 77why, one step back77 (prop in state.base && is(state.base[prop], value)) ||Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 78–111why, one step back78 (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 = trueMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 112–113why, one step back112 state.copy = shallowCopy(state.base)113 // copy the proxies over the base-copyMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 114–121why, one step back114 Object.assign(state.copy, state.proxies) // yup that works for arrays as well115 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)Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 122–125why, one step back122 const proxy = Array.isArray(base)123 ? Proxy.revocable([state], arrayTraps)124 : Proxy.revocable(state, objectTraps)125 proxies.push(proxy)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 126–128why, one step back126 return proxy.proxy127}128Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 129why, one step back129export function finalizeObject(state) {Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 130–131why, one step back130 const copy = state.copy131 const base = state.baseMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 132–134why, one step back132 each(copy, (prop, value) => {133 if (value !== base[prop]) copy[prop] = finalize(value)134 })Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 135–137why, one step back135 return freeze(copy)136}137Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 138why, one step back138export function finalizeArray(state) {Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 139why, one step back139 const copy = state.copyMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 140why, one step back140 each(copy, (i, value) => {remove extra conditioniruca3before adoptionac122b7
line 141why, one step back141 copy[i] = finalize(value)Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 142why, one step back142 })Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 143–146why, one step back143 return freeze(copy)144}145146export function produceProxy(baseState, producer) {Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 147–148why, one step back147 const previousProxies = proxies148 proxies = []Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 149–152why, one step back149 try {150 // create proxy for root151 const rootClone = createProxy(undefined, baseState)152 // execute the thunkMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 153why, one step back153 verifyReturnValue(producer(rootClone))Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 154–156why, one step back154 // and finalize the modified proxy155 const res = finalize(rootClone)156 // revoke all proxiesMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 157why, one step back157 each(proxies, (_, p) => p.revoke())Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 158–159why, one step back158 return res159 } finally {Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 160why, one step back160 proxies = previousProxiesMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 161–162why, one step back161 }162}