Jelmerro / Vieb

Vim Inspired Electron Browser - Vim bindings for the web by design
https://vieb.dev
GNU General Public License v3.0
1.29k stars 61 forks source link

Enough State Export+Import to Build Live Sync #515

Open mentalisttraceur opened 10 months ago

mentalisttraceur commented 10 months ago

Checklist

Addition or change One of the biggest quality-of-life improvements for me in browsers is Real-Time Tab Sync.

I don't want Vieb to get in the business of syncing state between devices. I have Syncthing for that and it does a great job.

But it would be wonderful if Vieb one day had a feature that at least writes the relevant state to a folder. Ideally in a way that works well with Syncthing. So for example, writing a list of open URLs into a file would regularly cause Syncthing sync conflicts, but writing each open URL into its own file would be great for Syncthing.

It would be really nice, but probably not strictly necessary, to also watch that folder for changes (with f.e. inotify on Linux, etc) and open/close tabs accordingly (near as I can tell, it would be possible to design the state folder structure/contents so that watching for changes and syncing the opening of tabs could be done by an external program... I haven't exhaustively thought if syncing the closing of tabs could be handled reasonably well by an external program or not).

I intend to eventually write up a more concrete+complete design for this, but not sure when I'll have the time to do that.

Alternatives considered Alternative 1: less sync-enabling -> sad face. Alternative 2: sync support inside Vieb itself - > more code, complexity, errors to deal with, API+UI surface area, would require deciding on sync servers+protocols and either providing a sync server or having users self-host.

KunaPrime commented 10 months ago

you can specify --datafolder=<dir> for the Vieb state including opened tabs see vieb://help#datafolder

mentalisttraceur commented 10 months ago

@KunaPrime is the format/layout of the datafolder guaranteed to remain stable?

KunaPrime commented 10 months ago

i'm not a Vieb developer so i can not answer that, but i'm using multiple data folders for multiple instances of Vieb for over a year now and i had never any issues with them even between versions.

KunaPrime commented 10 months ago

and i have never used Syncthing (i seem to be the tool i'm looking for actually) so i don't know how data folder would interact with it but i will try that for sure.

Jelmerro commented 10 months ago

It is stable in that it only has changed in V2 and V3, the latter of which was released in november 2020 which was almost 3 years ago, and that I would only dare to make breaking changes in major releases, as Vieb uses semantic versioning. To answer your question more directly, syncthing seems to be a valid option, if you make sure to quit instances on one device and let it sync before opening it on another device, as otherwise you might lose things like localstorage and such, since those cannot be spread over multiple instances (it's also the reason for the 1 window per datafolder in Vieb). The tab file is updated on interaction with the browser, in that it gets written every so often, though existing instances do not read updates from it. So again, if you only use Vieb on one device at a time this will work, otherwise prepare for dataloss.

What you are looking for regarding the open tabs is the tabs file in the datafolder, which has the caveats as described above, but has been unchanged/stable since V2 (released april 2020, and would be a major release if broken with deprecation/migration, and no current plans exist to migrate it).

mentalisttraceur commented 10 months ago

It is stable in that [...] I would only dare to make breaking changes in major releases, as Vieb uses semantic versioning.

Yep, that sounds like Stable API to me, thanks!

if you make sure to quit instances on one device and let it sync before opening it on another device

Sadly my switching is rarely so pre-planned. Either I've stretched "I'm almost done..." so long that I ran out of battery, or I've stepped away from / put down one device and when I next come back to that task another device is more opportune.

But I think I can work with this!

I could write my own program to watch the tab file to mirror those changes to a directory with a file per tab (for Syncthing, since that can't merge concurrent in-the-same-file changes), and watch that directory for changes to programmatically tell Vieb to open/close tabs.

Jelmerro commented 10 months ago

I think to achieve this sync functionality using syncthing, all we would need would be a file listener (possibly in Vieb) that updates the current tabs based on updates to the file. It already writes the tabs to the file and that can already be synced, it's just that the file is only read on startup and not when running, which is the only part missing I believe. This could potentially be as easy as re-running the full tab startup sequence with the added feature that tabs are first matched to existing ones before being opened.

Jelmerro commented 9 months ago

So I briefly spend some time on this, and it appeared to be mostly working at first, until it ran into infinite loops of loading and writing. This happens because once the file watcher triggers for the file and the tabs are to be set to the correct state, we must close and open tabs accordingly, but this in turn means we changes the tabs, which at the moment is kind of hard-wired to writing the file, which then means the watcher triggers again. One could try wrapping this in debouncers or other type of de-loop wrappers, but the problem is clear: this either needs to be built-in or it will cause problems. Here is the filewatch.patch.zip for those interested, I will not continue work on this for now since it seems like it would need a major rewrite to the tab loading and especially writing system.

For your request of sync status with syncthing, I think an external wrapper that manages this could make it work, but reading the tabs file on update into Vieb does not seem like a viable solution at the moment.