elgerlambert / redux-localstorage

Store enhancer that syncs (a subset) of your Redux store state to localstorage.
MIT License
1.32k stars 107 forks source link

Synchronization? #54

Open aij opened 8 years ago

aij commented 8 years ago

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.)

vicentedealencar commented 7 years ago

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.

NeXTs commented 7 years ago

This advice causes to install and depend on whole pouchDB library

@aij did you found satisfying solution?

elgerlambert commented 7 years ago

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?

kostia1st commented 7 years ago

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.

aij commented 7 years ago

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.

kostia1st commented 7 years ago

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