Reputation: 819
I ned some direction from the community as to how to prevent race-condition in ReactJS w/ Redux.
Supposedly two people are looking at the same record, and both would like to make change to it, what is the action flow suppose to look like? I am not sure how to google this query so i decided to ask the community to point me in the right direction.
Initially I thought that if i do a setState and persist it into DB, it will automatically refresh in the eyes of the other users (although I cannot confirm that), otherwise how would I lock the item for change so that other users cannot manipulate the value?
Upvotes: 0
Views: 903
Reputation: 1448
You can use websockets along with react redux for this. If user A
is looking at a record, send a message to server to lock the record. Then broadcast a locked
message via websocket to other subscribed users who are currently vieweing it. Once the users receive the locked
websocket message, update react/redux state to show appropriate status.
And as for the race-condition, it could happen and needs to be handled at the server according to the use case. For example, if user A
's request reaches first and User B
's reach second and if you want to give preference to the first user, lock the resource record, but set the access true
only for User A
. Once the user A
stops editing, remove the lock.
If you want to make the websocket message watching part a separate concern outside the react component, you can look into the redux middleware redux-saga.
Upvotes: 1