#15321: Only one tab writes to the DB#382
Conversation
|
CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅ |
|
I have read the CLA Document and I hereby sign the CLA |
rezkiy37
left a comment
There was a problem hiding this comment.
Left comments. However, in general, great job!
|
@roryabraham Sorry, I had to take care of my other issues, but I'll address the rest of the comments soon |
|
@koko57 thanks for pushing this forward. Please ping me when it's ready for review |
|
@roryabraham I think it's ready for re-review. On separate channels I commented here #382 (comment) - do we really need it? For the unsubscribe logic - as we no longer need to subscribe so many times, I also think it's unnecessary: #382 (comment). Let me know what do you think about it 🙂 |
roryabraham
left a comment
There was a problem hiding this comment.
I still think we need to add some unsubscribe logic. Even if there are fewer subscriptions, we're never cleaning them up which means we're propagating data to tabs that no longer exist unnecessarily.
@roryabraham I checked it once again and if I understand this logic correctly, after closing tabs we no longer propagate the data to them. Let me know what do you think about it and correct me if I'm wrong with the logic 🙂 |
| @@ -0,0 +1,99 @@ | |||
| /** | |||
| * When you have many tabs in one browser, the data of Onyx is shared between all of them. Since we persist write requests in Onyx, we need to ensure that | |||
There was a problem hiding this comment.
Just realizing that this comment is a bit out-of-date. This code no longer has anything to do with processing write requests.
|
Let's follow up ASAP with a PR to upgrade Onyx in E/App |
|
@roryabraham While trying to bump Onyx version in Expensify App I got this error I've opened a draft PR for this - let me know if we can fix it this way |
|
@roryabraham wdyt? |
|
Approved #434 |
|
This PR caused a regression after the Onyx bump. Fixing PR created here: #455 |
|
During the development of this PR, we had the sense that the SharedWorker API would be the best way to solve the problem this set out to solve (multi-tab support for Onyx). The reason that wasn't a viable solution is that SharedWorkers aren't supported by Android Chrome. I think that's the only mainstream browser we'd care about that doesn't support that API. However, I've been tracking the Chromium issue to add that support, and it's starting an origin trial. If I'm understanding this correctly, I believe that this means that SharedWorker support for Android chrome could enter the mainstream in Android Chrome 145. (current version is 139). ChatGPT estimates that might be around Feb 2026 |

Details
This is the 1st part solution to fix the problem of the linked issue. The links to the broader discussion are below. This PR is big because it contains a RN app that contains an example for the e2e tests.
The goal of this PR is to change Onyx execution so that when there are several tabs open, only one of them can write to the DB. This is done by defining a leader tab, and all other tabs defer their writes to this one leader.
Changes included in this PR:
ActiveClientManagerto react-native-onyx and usedBroadcastAPIinstead of the indexedDB to reach a consensus between tabs about who is the leader (the last tab to be open). It uses timestamps in the messages to check that the last tab to send the message is the leader. If for a change the timestamps are the same, the client ID of the tab is used as a tiebreaker.The GH comment with the explanation: Expensify/App#15321 (comment)
The doc with the discussion: https://docs.google.com/document/d/1Psr4Q4A2uH2b7l9CKBuB_dBT-15V4lJ7SOaH4uQOve4/edit
Related Issues
GH_LINK: Expensify/App#15321
Automated Tests
Added e2e tests using playwright. To run them:
npm install(should install onyx dependencies and the e2e app as well)npm run e2eornpm run e2e-uito run the e2e testsCurrent result:
Linked PRs
This will require future changes in newDot, since
ActiveClientManageris now available in react-native-onyx.