Closed jalasem closed 1 year ago
This is an interesting use-case. This can be something that could be built complementary to the persistence plug-in. But since even Redux Sync State uses a third-party BroadcastChannel (because official API is not yet widely standard on browsers), adding that 3rd party lib would in itself be 4x the size of simpler-state! This could be a good candidate for an "external" (not bundled) plug-in.
Kindly guide me on how I can implement it. I don't mind publishing this as the first external plugin for simpler-state
@arnelenero
@jalasem
My rough idea is:
set
method (see https://simpler-state.js.org/recipe-plugins.html)init
method, so that you can set a listener to the BroadcastChannel. Whenever the listener detects a message that the entity has changed on a different tab, it will need to get the new value from localStorage, then invoke the entity.set
to sync this tab.Something like that I think should work.
(Since BroadcastChannel is not yet supported by all browsers, you will need to use this instead: https://github.com/pubkey/broadcast-channel)
Is your feature request related to a problem? Please describe. I'm currently using simpler-state in an enterprise project and I need to sync the tabs. For example, when a user logs out, I want him/her to be logged out on all tabs automatically.
Describe the solution you'd like I want every tab to receive an update when anything changes in the state
Describe alternatives you've considered Something like redux sync state