remotestorage / remotestorage.js

⬡ JavaScript client library for integrating remoteStorage in apps
https://remotestoragejs.readthedocs.io
MIT License
2.33k stars 141 forks source link

connect() overwrites local data and disconnect() clears local data, why? #1170

Open dienluong opened 5 years ago

dienluong commented 5 years ago

Hello,

First, thank you for the project!

rs.js is advertised as "offline-first by design". But how can an app be offline-first when local data is not kept?

To give a simple example: let's say we have a notes taking app. When not connected to a remote storage, the user can still create, delete and update notes. Once connected to online storage, the new or modified notes get sync'd with the data in online storage. That's not possible with rs.js because, based on my testing, when you connect, the online version of your data overwrites the local copy (i.e. your local modifications are lost). Furthermore, when the user disconnects from the online storage, the local copy is destroyed. So the user can no longer work on the existing notes, when disconnected.

So it seems to me that rs.js, despite the claimed "offline-first design", does not enable an app to be offline-frst.

Update: I think I realize now what "offline-first design" means in the context of rs.js. It means no Internet/remote connection but still "logged in" to a remote storage account (as opposed being in "anonymous mode").

raucao commented 5 years ago

Hi Dien,

Thanks for your question. The answer to this is two-fold, and after testing this for a bit in one of my apps, it seems you may have found a bug in the library. Let me explain:

Connecting a remote storage with existing local data

Data created or edited while being offline/disconnected should not overwrite remote data by default. When connecting a remote storage, the data should be synced exactly the same way as if your app had been connected, but temporarily lost Internet connectivity.

The way the sync works is generally "last write wins", but the client will know if the same object/file has been modified on the server since last synchronizing it (by way of storing the ETAG), and in case the file has been modified both in the local and remote storage, a conflict event should be emitted, which apps can then use to resolve such conflicts.

When I tested this in Webmarks just now, it worked fine with new data/bookmarks, but there seems to be a bug when adding the same bookmark before connecting. After the sync, I can see the changes in the UI, but when reloading the page, or re-connecting the storage, it turns out that the changes haven't actually been synced. This can either be a bug in Webmarks or remoteStorage.js. I will investigate further to find out what is happening exactly.

Disconnecting a remote storage

When disconnecting a remote storage from an app, the local cache is purged intentionally. It has been designed this way, because the most common use case for disconnecting a storage for us was usually wanting to connect a different account and/or to clear the local cache for other reasons.

However, I can see why you may want to keep the local data around, even while it's not synchronizing a remote storage. In fact, I think this could be used for easy data migration between accounts, provided the app developer has implemented full caching, as well as designed the storage usage in a way that doesn't break app data in case the data from two different accounts is mixed randomly upon such a sync procedure.

Perhaps it makes sense to explore this a bit further, and potentially provide a library and widget option for app developers and users to be able to choose not to purge the cache on disconnect.

raucao commented 5 years ago

Just FYI: added the docs label, because it seems like we should document the current and intended behavior better.

dienluong commented 4 years ago

Hi Sebastian,

Thank you for the response. Very appreciated.

When disconnecting a remote storage from an app, the local cache is purged intentionally. It has been designed this way, because the most common use case for disconnecting a storage for us was usually wanting to connect a different account and/or to clear the local cache for other reasons.

Perhaps purging the local cache only upon connecting to a different account, rather than purging right after disconnecting from the current account?

Regards!

galfert commented 4 years ago

I don't think that's a good idea, at least not as a default behavior. When using an app on any other than your own computer or device, disconnecting your account would still keep your data on that machine.

dienluong commented 4 years ago

You are right. (I tend to forget about this use case, which rarely applies to me, i.e. using shared desktop account or devices.)

On Tue, Dec 3, 2019 at 06:20 Garret Alfert notifications@github.com wrote:

I don't think that's a good idea, at least not as a default behavior. When using an app on any other than your own computer or device, disconnecting your account would still keep your data on that machine.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/remotestorage/remotestorage.js/issues/1170?email_source=notifications&email_token=ABEILQEQWK6XTUR26UP5WU3QWY6HBA5CNFSM4JIXBOIKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEFZBEXQ#issuecomment-561123934, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABEILQAFBI6XWINR2IEV7HTQWY6HBANCNFSM4JIXBOIA .