proxy-compare: `copyTargetObject` seems not work as expexted when the target is an frozen array object with symbol properties.
The key source code in this package is as follows, it use Array.from to copy array shallowly when the array is frozen.
When the obj is an array object, Array.from & [... obj] will remove all the symbol properties, which causes the following code not to work properly:
import { createProxy } from 'proxy-compare'
const symbol = Symbol('test')
const arr = [1, 2, 3, 4]
arr[symbol] = () => arr // define a symbol property in array object
const obj = { arr }
// deep freeze
Object.freeze(obj)
Object.freeze(obj.arr)
const proxy = createProxy(obj, new WeakMap())
// undefined, can't access symbol property
console.log('proxy.arr[symbol]:', proxy.arr[symbol]?.())
But a frozen plain obj with symbol properties (not an array) works as expected.
So, is it expected? Is there any workaround to solve it? Or might we need to implement a specific method to copy array properly?
Thank you for taking the time to consider this issue.
btw, I like this package so much, thanks your excellent work on it.
About this issue
- Original URL
- State: closed
- Created 8 months ago
- Comments: 20 (10 by maintainers)
Just a note to myself:
getOwnPropertyDescriptors doesn’t tell if a property is an index or not. We can copy all properties after
Array.from, but that worsen the performance. We should be super careful about the performance.You are exactly right, I missed it.
I think I might need to rethink my work. Thanks for your patience!