Skip to content
Myr

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 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

line 4why, one step back
4import {

Added polyfill for Object.is, fixes #77Michel Weststratebefore adoption25d7274

line 5why, one step back
5 is,

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

lines 6–9why, one step back
6 isProxyable,
7 isProxy,8 freeze,9 PROXY_STATE,

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

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

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

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

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

line 15why, one step back
15

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

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

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

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 = {}

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

line 36why, one step back
36each(objectTraps, (key, fn) => {

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

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

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

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

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

line 40why, one step back
40 }

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

line 41why, one step back
41})

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

lines 42–76why, 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 (

Added polyfill for Object.is, fixes #77Michel Weststratebefore adoption25d7274

line 77why, one step back
77 (prop in state.base && is(state.base[prop], value)) ||

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

lines 78–111why, one step back
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

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

lines 112–113why, one step back
112 state.copy = shallowCopy(state.base)
113 // copy the proxies over the base-copy

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

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)

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

lines 122–125why, one step back
122 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 back
126 return proxy.proxy
127}128

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

line 129why, one step back
129export function finalizeObject(state) {

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

lines 130–131why, one step back
130 const copy = state.copy
131 const base = state.base

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

lines 132–134why, one step back
132 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 back
135 return freeze(copy)
136}137

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

line 138why, one step back
138export function finalizeArray(state) {

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

line 139why, one step back
139 const copy = state.copy

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

line 140why, one step back
140 each(copy, (i, value) => {

remove extra conditioniruca3before adoptionac122b7

line 141why, one step back
141 copy[i] = finalize(value)

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

line 142why, one step back
142 })

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

lines 143–146why, one step back
143 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 back
147 const previousProxies = proxies
148 proxies = []

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

lines 149–152why, one step back
149 try {
150 // create proxy for root151 const rootClone = createProxy(undefined, baseState)152 // execute the thunk

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

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

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

lines 154–156why, one step back
154 // and finalize the modified proxy
155 const res = finalize(rootClone)156 // revoke all proxies

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

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

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

lines 158–159why, one step back
158 return res
159 } finally {

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

line 160why, one step back
160 proxies = previousProxies

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

lines 161–162why, one step back
161 }
162}