Skip to content
Myr

proxy.js

This file

Path

src/proxy.js

177 lines · 5.3 KiB · JavaScript

Last changed by

WIP on ES5 implMichel Weststratebefore adoption11a2d24

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 adoptioncf0b1e1

line 5why, one step back
5 is,

`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760

line 6why, one step back
6 has,

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

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

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

lines 10–11why, one step back
10 finalize,
11 shallowCopy,

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

line 12why, one step back
12 RETURNED_AND_MODIFIED_ERROR,

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

line 13why, one step back
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–30why, 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() {

Merge pull request #144 from btnwtn/patch-1Michel Weststratebefore adoptionaaa12eb

line 31why, one step back
31 throw new Error("Immer does not support `setPrototypeOf()`.")

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

lines 32–35why, one step back
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–44why, one step back
42
43function createState(parent, base) {44 return {

Initial work on supporting patchesMichel Weststratebefore adoptiond821dda

line 45why, one step back
45 modified: false, // this tree is modified (either this object or one of it's children)

WIP on ES5 implMichel Weststratebefore adoption11a2d24

line 46why, one step back
46 assigned: {}, // true: value was assigned to these props, false: was removed

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

lines 47–62why, one step back
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]

Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092

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–68why, one step back
66 return (state.copy[prop] = createProxy(state, value))
67 return value68 } else {

`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760

line 69why, one step back
69 if (has(state.proxies, prop)) return state.proxies[prop]

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

lines 70–77why, one step back
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) {

Initial work on supporting patchesMichel Weststratebefore adoptiond821dda

lines 78–79why, one step back
78 // TODO: optimize
79 state.assigned[prop] = true

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

lines 80–81why, one step back
80 if (!state.modified) {
81 if (

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

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

`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760

line 83why, one step back
83 (has(state.proxies, prop) && state.proxies[prop] === value)

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

lines 84–92why, one step back
84 )
85 return true86 markChanged(state)87 }88 state.copy[prop] = value89 return true90}9192function deleteProperty(state, prop) {

Initial work on supporting patchesMichel Weststratebefore adoptiond821dda

line 93why, one step back
93 state.assigned[prop] = false

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

lines 94–101why, one step back
94 markChanged(state)
95 delete state.copy[prop]96 return true97}9899function getOwnPropertyDescriptor(state, prop) {100 const owner = state.modified101 ? state.copy

`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760

line 102why, one step back
102 : has(state.proxies, prop) ? state.proxies : state.base

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

lines 103–110why, one step back
103 const descriptor = Reflect.getOwnPropertyDescriptor(owner, prop)
104 if (descriptor && !(Array.isArray(owner) && prop === "length"))105 descriptor.configurable = true106 return descriptor107}108109function defineProperty() {110 throw new Error(

Merge pull request #144 from btnwtn/patch-1Michel Weststratebefore adoptionaaa12eb

line 111why, one step back
111 "Immer does not support defining properties on draft objects."

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

lines 112–117why, one step back
112 )
113}114115function markChanged(state) {116 if (!state.modified) {117 state.modified = true

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

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

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

lines 120–125why, one step back
120 Object.assign(state.copy, state.proxies) // yup that works for arrays as well
121 if (state.parent) markChanged(state.parent)122 }123}124125// creates a proxy for plain objects / arrays

Initial work on supporting patchesMichel Weststratebefore adoptiond821dda

line 126why, one step back
126function createProxy(parentState, base, key) {

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 127why, one step back
127 if (isProxy(base)) throw new Error("Immer bug. Plz report.")

Initial work on supporting patchesMichel Weststratebefore adoptiond821dda

line 128why, one step back
128 const state = createState(parentState, base, key)

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

lines 129–132why, one step back
129 const proxy = Array.isArray(base)
130 ? Proxy.revocable([state], arrayTraps)131 : Proxy.revocable(state, objectTraps)132 proxies.push(proxy)

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

lines 133–135why, one step back
133 return proxy.proxy
134}135

Initial work on supporting patchesMichel Weststratebefore adoptiond821dda

line 136why, one step back
136export function produceProxy(baseState, producer, patchListener) {

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

lines 137–141why, one step back
137 if (isProxy(baseState)) {
138 // See #100, don't nest producers139 const returnValue = producer.call(baseState, baseState)140 return returnValue === undefined ? baseState : returnValue141 }

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

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

More tests, inverse patchesMichel Weststratebefore adoption8aa82bb

lines 144–145why, one step back
144 const patches = patchListener && []
145 const inversePatches = patchListener && []

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

lines 146–147why, one step back
146 try {
147 // create proxy for root

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

line 148why, one step back
148 const rootProxy = createProxy(undefined, baseState)

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

line 149why, one step back
149 // execute the thunk

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

line 150why, one step back
150 const returnValue = producer.call(rootProxy, rootProxy)

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

line 151why, one step back
151 // and finalize the modified proxy

Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145

line 152why, one step back
152 let result

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

lines 153–157why, one step back
153 // check whether the draft was modified and/or a value was returned
154 if (returnValue !== undefined && returnValue !== rootProxy) {155 // something was returned, and it wasn't the proxy itself156 if (rootProxy[PROXY_STATE].modified)157 throw new Error(RETURNED_AND_MODIFIED_ERROR)

Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145

lines 158–162why, one step back
158
159 // See #117160 // Should we just throw when returning a proxy which is not the root, but a subset of the original state?161 // Looks like a wrongly modeled reducer162 result = finalize(returnValue)

More tests, inverse patchesMichel Weststratebefore adoption8aa82bb

lines 163–166why, one step back
163 if (patches) {
164 patches.push({op: "replace", path: [], value: result})165 inversePatches.push({op: "replace", path: [], value: baseState})166 }

Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145

line 167why, one step back
167 } else {

More tests, inverse patchesMichel Weststratebefore adoption8aa82bb

line 168why, one step back
168 result = finalize(rootProxy, [], patches, inversePatches)

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

line 169why, one step back
169 }

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

line 170why, one step back
170 // revoke all proxies

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

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

More tests, inverse patchesMichel Weststratebefore adoption8aa82bb

line 172why, one step back
172 patchListener && patchListener(patches, inversePatches)

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

line 173why, one step back
173 return result

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

line 174why, one step back
174 } finally {

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

line 175why, one step back
175 proxies = previousProxies

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

lines 176–177why, one step back
176 }
177}