Skip to content

Commit 6ba7810

Browse files
fix(proxy-state-tree): avoid replicating proxies that already exists
1 parent cb91bc4 commit 6ba7810

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

packages/node_modules/proxy-state-tree/src/index.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,4 +965,16 @@ describe('SCOPED', () => {
965965
expect(tree.get().foo).toBe('bar2')
966966
expect(scoped.get().foo).toBe('bar2')
967967
})
968+
it('should not recreate proxy when exists', () => {
969+
const tree = new ProxyStateTree({
970+
foo: {
971+
bar: 'baz',
972+
},
973+
})
974+
975+
const state = tree.get()
976+
977+
// eslint-disable-next-line
978+
expect(state.foo === state.foo).toBe(true)
979+
})
968980
})

packages/node_modules/proxy-state-tree/src/proxify.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,15 +186,16 @@ function getValue(value, tree) {
186186
return value
187187
}
188188

189-
if (value[IS_PROXY]) {
190-
return value[VALUE][tree.PROXY] || value[VALUE]
189+
if (value[tree.PROXY]) {
190+
return value[tree.PROXY]
191191
}
192192

193193
return value
194194
}
195195

196196
export function proxify(tree, value, path?) {
197197
value = getValue(value, tree)
198+
198199
if (value) {
199200
if (value[IS_PROXY] && value[PATH] !== path) {
200201
delete value[VALUE][tree.PROXY]
@@ -210,6 +211,7 @@ export function proxify(tree, value, path?) {
210211
configurable: true,
211212
value: createObjectProxy(tree, value, path),
212213
})
214+
213215
return value[tree.PROXY]
214216
} else if (Array.isArray(value)) {
215217
Object.defineProperty(value, tree.PROXY, {

0 commit comments

Comments
 (0)