aohua/redux-state-sync

Locally syncing the results of Async actions.. how does this work here?

Closed this issue · 6 comments

Not sure how this works as I have not looked into details yet. The documentation says it will "...dispatch exactly the same actions dispatched in other tabs.."

Does this mean if one of my tab dispatches an async action (say fetching user data from backend).. the same action will be dispatched in all other tabs (requiring multiple api fetch calls to backend from all open tabs to receive the same user data)?? Would it not be more efficient to dispatch actions from only the source tab and on state change in that tab, sync the resulting state in other tabs ??

aohua commented

You are right, you can use blacklist or whitelist or predict to prevent that.

aohua commented

Let's say your async action is called GET_USER_REQUEST, If you don't want this to be triggered in other tabs, you can put this inside your blacklist, or create a predicate function to block this actions. After GET_USER_REQUEST is done, sync GET_USER_SUCCESS or GET_USER_FAILED in other tabs.

Well, of course, but that's kinda half the solution. Say, you trigger FETCH_USER_REQUEST from one tab but blacklist this action so it won't be dispatched in other tabs. In the source tab, it will change some state, say, it will make loading: true (or clear existing data so as to expect new data) for users.. this change won't be seen in other tabs as the responsible action FETCH_USER_REQUEST was not dispatched from those other tabs.

The question now is to sync actions, but not the application side effects. Could you provide a way to achieve this using this library?

aohua commented

This middleware doesn't support the use case that you mentioned. However, I think you may consider using redux-persist together with redux-persist-crosstab

Just found & realized that you can use the $isSync property flag in action object to prevent application side effects using your library too (e.g. in your saga, if the [action].$isSync is true, return the saga and do nothing). Just letting the future readers know :)

aohua commented

Yes, you are right, I will add this to the doc. Thanks. 👍