-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
y-indexeddb #26
Comments
I have never tried, but if y-indexeddb provides two-way binding to ydoc, valtio-yjs would just work without knowing about y-indexeddb, at least for some simple cases. Please give it a try. |
Thank you for the quick reply. You are probably correct that y-indexeddb is not special and should just work. I do have problems getting it to work however. I adjusted the codesandbox from the messages array example and replaced the websocket provider with the indexeddb one. https://codesandbox.io/s/valtio-yjs-array-demo-forked-38flhk?file=/src/App.js When you write messages and send them they are indeed reloaded when you refresh. But if you add messages afterwards they are not restored after refreshing. Peek.2022-03-23.11-55.mp4I am at a loss about why this would happen. Could it have something to do with the order of binding and the updates coming from the indexed db? |
Hmm, it can be a bug. You might need to dig deeper. |
Good idea. I will do that tomorrow. |
Ok I am pretty sure this is a bug with the array type specifically. I tried the following:
|
Thanks. Seems like a bug. |
I am not sure about how to reproduce it without y-indexeddb. I do not really have any persistence apart from that so the state would always be empty on refresh. The issue does not exist in the code sandbox examples in the Valtio-yjs readme which are using the websocket provider. |
…ecycle The builder url includes the tree id. This id is used to identify which websocket connection to use. By using react context we can make sure that the YDoc and websocket connection only ever exists when the user is on the respective builder page. - y-indexeddb is not used, because of a possible bug in valtio-yjs valtiojs/valtio-yjs#26
Hm, the bug doesn't seem to be related with indexeddb. #22 and #24 will require some refactors, and having a failing test would be very important. (And, some contributors would be helpful.) |
Sadly the documentation for y-indexeddb alone is not that detailed about the inner workings. I will try to get some help from the y-indexeddb maintainers. I would love to help with the other issues you mentioned. Although I might need some time to familiarise myself with the codebase. I will try to make progress on this in the next two weeks. |
…ecycle The builder url includes the tree id. This id is used to identify which websocket connection to use. By using react context we can make sure that the YDoc and websocket connection only ever exists when the user is on the respective builder page. - y-indexeddb is not used, because of a possible bug in valtio-yjs valtiojs/valtio-yjs#26
Hi all, as mentioned before, valtio is not special and will just sync to y-indexeddb. However, y-indexeddb is not supposed to sync tabs. It needs some kind of provider that syncs tabs. Usually, this is done using cross-tab communication using broadcastchannels. y-websocket and y-webrtc both support broadcastchannel communication. It makes more sense to implement cross-tab communication in the network providers, because they have more knowledge about the network. In y-webrtc, for example, two clients will not open a webrtc connection to each other and only use broadcastchannel. At the moment there is no pure broadcast channel provider for Yjs. However, it might make sense to write a generic broadcastchannel provider that has an interface for network providers. |
So, it's not a bug of |
Nope |
Thank you @dmonad for your answer. Do I understand you correctly that by reloading the codesandbox preview it works like opening another tab? Otherwise I am not sure what tabs have to do with this issue. |
Oh wait. This issue is not about cross-tab communication at all. I have no idea how the bug in the video can happen. There is a lot of stuff going on in the codesandbox. If you can reproduce this issue without valtio, let me know. I think the bug might happen because you manipulate the document in the same transaction that loads the initial y-document (e.g. during an observer call). |
Ok if I understand you correctly the issue is that it causes two transactions simultaneously? This is of course not a real solution and I am wondering if this is a more general bug with how valtio-yjs is supposed to work. Theoretically the binding always happens in the same transaction. The only reason I can see why this does not happen with something like y-websocket is the network delay introducing a natural timeout that results in two transactions. |
So, it's related with #22? |
I am not sure it is related. While it would be good if only one transaction where caused I don't think it matters for this issue. My current thoughts/questions on why this could happen:
|
A transaction happens whenever the Yjs document is modified (also when content loads from a database, or from the network). You can distinguish transaction by their transaction-origin.
This behavior commonly happens if you prefer the content that you receive from some other source (e.g. the "initial state" from the server replaces the current version of the document). I recommend to make Yjs the source of truth.
Applying an update (e.g from y-indexeddb) throws an event. If you modify the document within the event-handler, the changes will happen within the same execution context. However, y-indexeddb will ignore all changes of the current execution context as it thinks that y-indexeddb created all the changes within the current transaction call. This line actually is responsible for ignoring updates. This can be solved by filtering transactions by origin (which was not a thing when I originally created y-indexeddb). I created an issue / feature request for that: yjs/y-indexeddb#19 I still recommend not to manipulate the document within event-handlers as other providers might not handle this correctly as well.
This problem is probably just more visible with Y.Arrays. Other data types will have similar issues. |
Just so I understand correctly. Are we talking about event handlers in the codesandbox or in valtio-yes source code? |
Event handlers in the valtio-yjs source code - you shouldn't modify Yjs in event handlers like Anyway, I just "fixed" the behavior in y-indexeddb |
@dai-shi @PhilGarb I was wondering if this still the case where valtio-yjs is modifying yjs in event handlers?
|
Yeah, I don't think we have changed anything since then. |
I am no longer able to reproduce this issue with the original codesandbox. All messages persist across multiple refreshes. Anybody else? |
It has worked for me for quite a while. I am not sure the issue has ever been caused by valtio-yjs. So I am gonna close this. |
Is y-indexeddb supported by valtio-yjs?
The text was updated successfully, but these errors were encountered: