Open StepanDyubin opened 1 month ago
Thanks for reporting.
I can't guarantee if a PR will be accepted, but if you are fine with it, can you open one please? Is it possible to add a failing test case first?
I'm also curious if your proposal is about omitting target cache if needsToCopyTargetObject
returns false. So, everything isn't yet clear. We also need to run tests with valtio and others.
Hello and thanks for the answer!
I opened a PR with failing test case and my solution. https://github.com/dai-shi/proxy-compare/pull/71/files
It works fine in my case. And I'll be very glad if you can check this for your cases.
Also if you have any doubts or questions, I'm ready to help you.
Thanks. I'm busy at the moment, and will take a look later. I will also run tests in react-tracked, valtio, and proxy-memoize.
In my case I can fix it if I guarantee that an object is frozen when call
createProxy
first time.
After reviewing #71, I think your workaround seems reasonable.
Or, you can delete it from the target cache when you call Object.freeze
.
Or, create a wrapper:
import { createProxy as createProxyOrig } from 'proxy-compare';
export const createProxy = <T>(
obj: T,
affected: WeakMap<object, unknown>,
proxyCache?: WeakMap<object, unknown>,
targetCache?: WeakMap<object, unknown>,
): T => {
if (targetCache?.has(obj) && Object.isFrozen(obj) && !Object.isFrozen(targetCache.get(obj)[0])) {
targetCache.delete(obj)
}
return createProxyOrig(obj, affected,proxyCache, targetCache);
};
Hi! I caught an error when trying to pass
targetCache
.Problem appears if call
createProxy
with a regular object, then with the same (by link) but frozen object.Error:
https://codesandbox.io/p/sandbox/proxy-compare-test-forked-ksskgg
First time we call
createProxy
, it stores target without a copy. And then if callcreateProxy
with frozen object, it hits in totargetCache
and receives that target without copy. https://github.com/dai-shi/proxy-compare/blob/1c0f138cbaa5bb0dd736f305d43e74bb1ecf0c74/src/index.ts#L209C1-L219C1In my case I can fix it if I guarantee that an object is frozen when call
createProxy
first time. But you can fix it if checkneedsToCopyTargetObject
before get from the targetCache.Example of solution:
Do you think is there a reason to fix that case at your library? I can open a PR if you are.