LegendApp / legend-state

Legend-State is a super fast and powerful state library that enables fine-grained reactivity and easy automatic persistence
https://legendapp.com/open-source/state/
MIT License
2.89k stars 82 forks source link

[Feature Request] Yjs Persistence Plugin #215

Open andrictham opened 11 months ago

andrictham commented 11 months ago

I’m building an app with rich text editing and am using Legend State to handle application state outside of Prosemirror (entities that can be “mentioned” from the editor, like a hashtag. It doesn’t make sense to store this state in Prosemirror because multiple editors can mention the same entities).

Everything works fine right now, except, I want to enable users to do offline-first editing while also allowing them to collaborate with others (multiplayer editing), so I'll need to bring in a CRDT like Yjs.

The good news is that TipTap (a Prosemirror wrapper) natively supports Yjs, so setting that up is trivial. However, I can’t figure out how to get Legend State to sync with the same Yjs doc, as well.

I'll need the editor state and my application state to be in the same Yjs doc, since both pieces of state are interdependent. That allows me to ensure state consistency across users and devices.

I really like Legend, and don't want to switch to another state management solution that supports Yjs (like SyncedStore, Valtio, or Tinybase).

How easy would it be to sync Legend to Yjs using the new persistence plugins introduced in 2.0? If I’m understanding correctly, I can sync Legend State to Yjs using a local persistence plugin?

I’m new to both Yjs and Legend State’s persistence plugin, so any assistance is greatly appreciated!


Related: https://github.com/LegendApp/legend-state/discussions/172

jmeistrich commented 11 months ago

I haven't used Yjs myself so I'm not 100% sure, but assuming a plugin like in https://github.com/LegendApp/legend-state/discussions/172 worked, it would just work straight away to cache locally with persistence. If you or @raymonddaikon could submit a plugin with some tests in a PR or otherwise an example repo that hooks it into an editor that I could play with, I could try to clean it up and get it to 100%?

I would actually quite like to support yjs and TipTap so I am excited about this! I just don't currently have the bandwidth to build a test project from scratch. So if you already have something going that I could start with, I could either finish it up or at least give guidance.