Skip to content

Conversation

@VickyStash
Copy link
Contributor

@VickyStash VickyStash commented Aug 4, 2025

Details

Before, the batch updates logic in the onyx was working for withOnyx subscribers only.
This PR extends that batching mechanism to also include useOnyx subscribers.
Previously, useOnyx subscribers could receive Onyx.update updates slightly out of sync, as some updates would arrive a bit earlier than others, causing extra re-renders and data missing -> it was reported here. It should be fixed with this PR.

Related Issues

#571

The issue was reported here: Expensify/App#66801 (comment)

Automated Tests

Manual Tests

Feel free to test updates over this PR.
That PR has E/App using onyx version 2.0.127 + this PR updates. I've done it this way since onyx upgrade to v2.0.130 was reverted.

  1. Open the app.
  2. Create a DM report that holds at least two expenses.
  3. Go to Settings -> Troubleshoot -> Clear cache and restart
  4. Go to Reports > Reports
  5. Find the DM report and enter it.
  6. The OpenReport API call will be triggered. Make sure that in the SearchMoneyRequestReportPage the response successData (ONYXKEYS.COLLECTION.REPORT_METADATA key specifically) is loaded at the same time as the returned by API report (ONYXKEYS.COLLECTION.REPORT) data.
Before After
image image
Successful data hasOnceLoadedReportActions: true is applied in metadata, but the report from the response isn't loaded yet. It appears a little later, though this data was provided in one Onyx.update 1. The report isn't loaded, isLoadingInitialReportActions: true in the metadata. 2. The report is loaded. Successful data hasOnceLoadedReportActions: true is applied in metadata

Besides that, let's make sure the basic app functionality works the same as before:

  • Switch between chats. Make sure the data is loading as expected.
  • Switch between app tabs. Make sure the data is loading as expected.
  • Try to send several expenses. Update/delete them.
  • Try to send several messages.

Author Checklist

  • I linked the correct issue in the ### Related Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android / native
    • Android / Chrome
    • iOS / native
    • iOS / Safari
    • MacOS / Chrome / Safari
    • MacOS / Desktop
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • I verified that the left part of a conditional rendering a React component is a boolean and NOT a string, e.g. myBool && <MyComponent />.
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • I verified that if a function's arguments changed that all usages have also been updated correctly
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • If we are not using the full Onyx data that we loaded, I've added the proper selector in order to ensure the component only re-renders when the data it is using changes
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR author checklist, including those that don't apply to this PR.

Screenshots/Videos

Android: Native
android.mp4
Android: mWeb Chrome
android-web.mp4
iOS: Native
ios.mp4
iOS: mWeb Safari
ios-web.mp4
MacOS: Chrome / Safari
web.mp4
MacOS: Desktop
desktop.mp4

fabioh8010 added a commit to callstack-internal/react-native-onyx that referenced this pull request Aug 5, 2025
@VickyStash VickyStash changed the title [WIP] Update batching to be applied to useOnyx updates as well Update batching to be applied to useOnyx updates as well Aug 7, 2025
@VickyStash VickyStash marked this pull request as ready for review August 7, 2025 15:05
@VickyStash VickyStash requested a review from a team as a code owner August 7, 2025 15:05
@melvin-bot melvin-bot bot requested review from mollfpr and removed request for a team August 7, 2025 15:05
@fabioh8010
Copy link
Contributor

Please dont merge this yet, we are performing more tests...

@VickyStash VickyStash changed the title Update batching to be applied to useOnyx updates as well [DO NOT MERGE] Update batching to be applied to useOnyx updates as well Aug 7, 2025
@mountiny mountiny self-requested a review August 8, 2025 22:16
@VickyStash VickyStash changed the title [DO NOT MERGE] Update batching to be applied to useOnyx updates as well Update batching to be applied to useOnyx updates as well Aug 12, 2025
@mountiny
Copy link
Contributor

@TMisiukiewicz What is the merge strategy now? I would like to make sure we can first update Onyx in App without regressions. Was the previously found regression fixed already in main (sorry I cant recall now)

@TMisiukiewicz
Copy link
Contributor

@mountiny Solution with last regression fixed waits for a merge there: #670 I think we can merge it and bump Onyx in E/App before moving forward with this one

@mountiny
Copy link
Contributor

Merged that PR

…dates

# Conflicts:
#	tests/unit/useOnyxTest.ts
@VickyStash
Copy link
Contributor Author

Great, let's then wait till onyx version is bumped in the E/App - PR

Copy link

@mollfpr mollfpr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes look good to me and have tested well!

@VickyStash
Copy link
Contributor Author

Note: I'll be OOO tomorrow!

@shubham1206agra
Copy link
Contributor

Add #571 as fixed issue.

@mountiny
Copy link
Contributor

Waiting for the onyx bump go through

@VickyStash
Copy link
Contributor Author

It looks like the previous bump PR was closed and we are waiting for a new one...

Copy link
Contributor

@mountiny mountiny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@mountiny mountiny merged commit 12c4762 into Expensify:main Aug 20, 2025
5 checks passed
@os-botify
Copy link
Contributor

os-botify bot commented Aug 20, 2025

🚀 Published to npm in 2.0.135 🎉

): Promise<void> {
const promise = Promise.resolve().then(() => keyChanged(key, value, previousValue, canUpdateSubscriber, true, false));
batchUpdates(() => keyChanged(key, value, previousValue, canUpdateSubscriber, false, true));
const promise = Promise.resolve().then(() => keyChanged(key, value, previousValue, canUpdateSubscriber, true, false, false));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@VickyStash I am not sure why but this false seems to cause below issue(passing true worked 😅 ), could you plz check once

Bug: Toggling the language preference back doesn't apply to the application.

Screen.Recording.2025-08-25.at.19.26.46.mov

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me check!

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I was testing PR and came to know about this issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Pujan92 I was able to reproduce it on my end, but I need some time to figure out why it happens. I'll return to you tomorrow morning!

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I've found out why it happens.

When we change the language, the app loads it and changes ONYXKEYS.ARE_TRANSLATIONS_LOADING flag here (to true) and here (to false).

The thing is that when the translations were already loaded before, they are cached and both ONYXKEYS.ARE_TRANSLATIONS_LOADING happen almost at the same moment, so they are batched together.

Due to that -> this hook doesn't work as expected -> causing the bug.


I think there can be other places like that in the app...
One of the ideas that I have is apply batching only if updates are coming from Onyx.update.
It also can easily be fixed on the app side, but I don't think it's a good idea

VickyStash added a commit to callstack-internal/react-native-onyx that referenced this pull request Aug 27, 2025
…yStash/refactor/batch-all-subscribers-updates"

This reverts commit 12c4762, reversing
changes made to 1886f02.
cristipaval added a commit that referenced this pull request Aug 27, 2025
…evert-batch-updates

Revert "Merge pull request #669 from callstack-internal/VickyStash/refactor/batch-all-subscribers-updates"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants