question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Infinite loop when setting a nested object

See original GitHub issue

I’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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
nccommented, Nov 26, 2021

I’ve added a call to deepEqual in subscribe 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/15

1reaction
fsoncommented, Nov 26, 2021

Alright, 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#L103

Each 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

{ items: { item1: { color: 'blue' } } }

then { item1: { color: 'blue' } } is observed by 2 event handlers and { color: 'blue' } by 3 event handlers.

Each call to bindProxyAndYMap also creates a new WeakMap 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found