Infinite loop when setting a nested object
See original GitHub issueI’m trying to use valtio-yjs
to bind a Valtio store with nested objects to YJS. I noticed that if I have 2 levels of nesting in the proxy, and set the inner object to a new plain object, this results in an infinite loop in valtio-yjs
:
const ydoc = new Y.Doc();
const ymap = ydoc.getMap("objects");
const state = proxy({ items: { item1: { title: "hello" } } });
bindProxyAndYMap(state, ymap);
state.items.item1 = { title: "goodbye" }; // Page becomes unresponsive!
After setting item1
, the library seems to keep alternating between setPValueToY
and setYValueToP
and the whole page becomes unresponsive.
Here’s a codesandbox repro for the issue: https://codesandbox.io/s/valtio-yjs-demo-forked-w3otm?file=/src/App.js (press “change title” to trigger the infinite loop).
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:5 (5 by maintainers)
Top Results From Across the Web
Infinite loop with nested object - reactjs - Stack Overflow
When you are creating the object the way you have done in the code will ... It will be creating the new object...
Read more >RTK Query - Infinite loop when arg is nested object #1526
The problem. The problem infinite loop with multiple calls occurs when filter is an object inside another object, which assembles arg. The " ......
Read more >Object of infinite size getting created - Oracle Communities
Hi, The problem i m facing is that I have an Object A that is created through ".invoke" using reflection. Consider it object...
Read more >Loop through nested object javascript - idkuu.com
How do I map a nested object in react JS? Use the map() method to iterate over the outer array. On each iteration,...
Read more >How to solve the React useEffect Hook's infinite loop patterns
Solve the issue of infinite loops when using the useEffect Hook in React to more smoothly utilize the Hook for your app's side...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ve added a call to
deepEqual
insubscribe
as a workaround/hack for now so we can keep using the library and work on other features in the interim. https://github.com/dai-shi/valtio-yjs/pull/15Alright, spent a bunch of time debugging, here’s what I found: The library adds multiple (2 or more) observers to nested objects. I think this is due to these recursive calls to
bindProxyAndYMap
: https://github.com/dai-shi/valtio-yjs/blob/bb5704540e38093228861426c43530de9fa58670/src/index.ts#L126 https://github.com/dai-shi/valtio-yjs/blob/bb5704540e38093228861426c43530de9fa58670/src/index.ts#L103Each call to
bindProxyAndYMap
adds one Valtio subscription and one YJS observer.The more deeply nested the object is, the more subscriptions/observers get added. For example, if the proxy is
then
{ item1: { color: 'blue' } }
is observed by 2 event handlers and{ color: 'blue' }
by 3 event handlers.Each call to
bindProxyAndYMap
also creates a newWeakMap
cache (pv2yvCache
), so even if one event handler has handled the update, the others don’t have knowledge of it.I don’t have more time to debug this right now, but wanted to share this braindump in case it’s of any help.