Skip to content
Myr

proxy.js

This file

Path

src/proxy.js

157 lines · 4.0 KiB · JavaScript

Last changed by

Removed some duplicate codeMichel Weststratebefore adoptionee544cc

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–12why, one step back
10 finalize,
11 shallowCopy,12 verifyReturnValue,

Fixed bug where new objects where proxiedMichel Weststratebefore adoption4ad622f

lines 13–14why, one step back
13 each,
14 finalizeNonProxiedObject

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

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

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

line 16why, one step back
16

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

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

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

lines 18–36why, one step back
18
19const objectTraps = {20 get,21 has(target, prop) {22 return prop in source(target)23 },24 ownKeys(target) {25 return Reflect.ownKeys(source(target))26 },27 set,28 deleteProperty,29 getOwnPropertyDescriptor,30 defineProperty,31 setPrototypeOf() {32 throw new Error("Don't even try this...")33 }34}3536const arrayTraps = {}

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

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

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

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

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

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

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

line 41why, one step back
41 }

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

line 42why, one step back
42})

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

lines 43–62why, one step back
43
44function createState(parent, base) {45 return {46 modified: false,47 finalized: false,48 parent,49 base,50 copy: undefined,51 proxies: {}52 }53}5455function source(state) {56 return state.modified === true ? state.copy : state.base57}5859function get(state, prop) {60 if (prop === PROXY_STATE) return state61 if (state.modified) {62 const value = state.copy[prop]

Fixed bug where new objects where proxiedMichel Weststratebefore adoption4ad622f

lines 63–65why, one step back
63 if (value === state.base[prop] && isProxyable(value))
64 // only create proxy if it is not yet a proxy, and not a new object65 // (new objects don't need proxying, they will be processed in finalize anyway)

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

lines 66–79why, one step back
66 return (state.copy[prop] = createProxy(state, value))
67 return value68 } else {69 if (prop in state.proxies) return state.proxies[prop]70 const value = state.base[prop]71 if (!isProxy(value) && isProxyable(value))72 return (state.proxies[prop] = createProxy(state, value))73 return value74 }75}7677function set(state, prop, value) {78 if (!state.modified) {79 if (

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

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

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

lines 81–114why, one step back
81 (prop in state.proxies && state.proxies[prop] === value)
82 )83 return true84 markChanged(state)85 }86 state.copy[prop] = value87 return true88}8990function deleteProperty(state, prop) {91 markChanged(state)92 delete state.copy[prop]93 return true94}9596function getOwnPropertyDescriptor(state, prop) {97 const owner = state.modified98 ? state.copy99 : prop in state.proxies ? state.proxies : state.base100 const descriptor = Reflect.getOwnPropertyDescriptor(owner, prop)101 if (descriptor && !(Array.isArray(owner) && prop === "length"))102 descriptor.configurable = true103 return descriptor104}105106function defineProperty() {107 throw new Error(108 "Immer does currently not support defining properties on draft objects"109 )110}111112function markChanged(state) {113 if (!state.modified) {114 state.modified = true

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

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

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

lines 117–124why, one step back
117 Object.assign(state.copy, state.proxies) // yup that works for arrays as well
118 if (state.parent) markChanged(state.parent)119 }120}121122// creates a proxy for plain objects / arrays123function createProxy(parentState, base) {124 const state = createState(parentState, base)

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

lines 125–128why, one step back
125 const proxy = Array.isArray(base)
126 ? Proxy.revocable([state], arrayTraps)127 : Proxy.revocable(state, objectTraps)128 proxies.push(proxy)

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

lines 129–131why, one step back
129 return proxy.proxy
130}131

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

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

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

lines 133–134why, one step back
133 const copy = state.copy
134 const base = state.base

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

lines 135–137why, one step back
135 each(copy, (prop, value) => {
136 if (value !== base[prop]) copy[prop] = finalize(value)137 })

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

lines 138–141why, one step back
138 return freeze(copy)
139}140141export function produceProxy(baseState, producer) {

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

lines 142–143why, one step back
142 const previousProxies = proxies
143 proxies = []

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

lines 144–147why, one step back
144 try {
145 // create proxy for root146 const rootClone = createProxy(undefined, baseState)147 // execute the thunk

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

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

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

lines 149–151why, one step back
149 // and finalize the modified proxy
150 const res = finalize(rootClone)151 // revoke all proxies

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

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

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

lines 153–154why, one step back
153 return res
154 } finally {

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

line 155why, one step back
155 proxies = previousProxies

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

lines 156–157why, one step back
156 }
157}