redux-localstorage icon indicating copy to clipboard operation
redux-localstorage copied to clipboard

Synchronization?

Open aij opened this issue 7 years ago • 6 comments

I was wondering how this library handles synchronization. It would appear the answer is that it doesn't, but please correct me if I'm wrong. (In which case, documentation would be nice. :) )

For example, with two tabs using localStorage, each tab will read from localStorage when the middleware is initialized, after which point they will blindly write over each other's writes. (So, whichever tab writes last wins, and the data is not synchronized to the other tab until the app is reloaded.)

aij avatar Aug 30 '16 17:08 aij

I don't think the changes feed is been listened to.

You should checkout redux-pouchdb, it is simpler than this but may suits your needs.

vicentedealencar avatar Sep 03 '16 06:09 vicentedealencar

This advice causes to install and depend on whole pouchDB library

@aij did you found satisfying solution?

NeXTs avatar Sep 28 '16 21:09 NeXTs

HI @aij,

You're right; the behaviour you describe is indeed what I would expect to happen.

I hadn't considered this use case/scenario before, my first reaction would be to write a piece of middleware that listens to the 'storage' event and dispatch those changes to update the store state of the other tab(s). Your reducer(s) would consequently determine how to merge these changes.

Is this something you considered? Are there issues you run into with this approach?

elgerlambert avatar Oct 30 '16 16:10 elgerlambert

I think this improvement should be considered essential for the library. Otherwise, it's limited to a very few use cases, and you basically cannot use it for storing auth tokens for instance.

kostia1st avatar May 19 '17 10:05 kostia1st

FWIW, I ended up using redux-persist with redux-persist-crosstab, and handling the the REHYDRATE action explicitly to merge the changes in my reducer. I also had to work around an infinite loop in IE.

aij avatar May 19 '17 16:05 aij

@aij thanks, that helped. We just implemented the same approach, exactly what we needed.

kostia1st avatar May 25 '17 08:05 kostia1st