Skip to content

Remove waitForCollectionCallback from Onyx#799

Open
fabioh8010 wants to merge 8 commits into
Expensify:mainfrom
callstack-internal:feature/onyx-store-pr-1
Open

Remove waitForCollectionCallback from Onyx#799
fabioh8010 wants to merge 8 commits into
Expensify:mainfrom
callstack-internal:feature/onyx-store-pr-1

Conversation

@fabioh8010

@fabioh8010 fabioh8010 commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Details

Removes waitForCollectionCallback — the per-member collection subscription mode — so a collection-key subscription always receives the whole collection. Removing it exposed a latent multi-tab sync bug, which this PR also fixes.

Cross-tab sync fix

Now that every collection subscriber is a whole-collection subscriber, multi-instance (web, multi-tab) sync needed fixing. InstanceSync raised one localStorage event per changed key and the receiving tab read each with its own async getItem, so a mergeCollection of N members arrived as N independent events across N tasks. That meant collection-root subscribers either received nothing cross-tab, or — if notified per member — had the whole collection re-delivered N times (O(N²)), crashing the receiving tab on large collections.

The fix preserves the write's batch boundary across tabs:

  • InstanceSync raises a single SYNC_ONYX event carrying the JSON array of all keys changed in a write (falls back to a single key for the previous format).
  • The receiving tab reads them with one multiGet and hands the whole batch to Onyx via a new batched onStorageKeysChanged(pairs) callback.
  • Onyx processes the batch synchronously, grouping members by collection and firing one keysChanged() per collection (plus keyChanged() for individual keys) — mirroring a local mergeCollection.

Result: a cross-tab collection update notifies each collection-root subscriber once (O(N)) instead of N times or never.

Related Issues

Expensify/App#94331

Linked E/App PR

Expensify/App#93436

Automated Tests

Tests were changed to accomodate the changes.

Manual Tests

We are basically just removing waitForCollectionCallback usage in Expensify/App#93436, functionality should stay the same so we can test only the tab sync issues that are present on main and are being fixed here. Tests are web-only.

  1. With an account logged in, duplicate the current tab so you have 2 tabs with same account for testing.
  2. In Tab A, pin one report from LHN. Go to Tab B and assert the same report was pinned on.
  3. Choose one report in the middle of LHN list, select and draft a message. Go to Tab B and assert the same report has the draft message and the LHN was correctly re-ordered to show the report closer to the top of the list.
  4. Mark one report as unread. Go to Tab B and assert the same report is marked as unread and the Chrome tab displays the notification badge.

Author Checklist

  • I linked the correct issue in the ### Related Issues section above
  • I linked the corresponding Expensify/App PR in the ### Linked E/App PR section above, and verified this change against it (E/App CI passed and manual testing completed)
  • 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
  • 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: mWeb Chrome
iOS: Native
iOS: mWeb Safari
MacOS: Chrome / Safari
Screen.Recording.2026-06-26.at.14.09.06.mov
Screen.Recording.2026-06-26.at.14.12.04.mov

Collection-root subscriptions (via Onyx.connect or useOnyx) now always
deliver the whole collection snapshot to the callback. The legacy
per-member delivery mode for collection-root subscribers is removed;
consumers needing per-member processing subscribe to a single member key.

Collapses the DefaultConnectOptions/CollectionConnectOptions discriminated
union into one ConnectOptions shape (callback typed on TKey), simplifies
generateConnectionID and the keyChanged/keysChanged dispatch, and drops the
option from useOnyx's internal connect. Per-member-mode tests were migrated
to assert snapshot delivery rather than deleted.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@fabioh8010

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 01704211bf

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread lib/OnyxUtils.ts
Comment on lines +680 to 684
if (OnyxKeys.isCollectionKey(subscriber.key)) {
// Collection-root subscribers always receive the whole collection snapshot.
// Skip individual key changes during collection updates to prevent duplicate
// callbacks - the collection update will handle this properly.
if (isProcessingCollectionUpdate) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Don't skip synced collection-member updates

On web with multiple tabs, InstanceSync raises storage events per member key and Onyx.ts calls keyChanged(..., isKeyCollectionMember) with true for those collection-member updates. With this broadened collection-root branch, the receiving tab hits the isProcessingCollectionUpdate continue, and no keysChanged() runs afterward in that tab, so Onyx.connect({key: COLLECTION...}) subscribers never receive the required collection snapshot when another tab changes the collection. Please allow the storage-sync path to notify collection-root subscribers instead of treating it like a local batched collection update.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed in 9263ec9. Basically this flow was already flawed in main – the collection key callbacks with waitForCollectionCallback: true were not notified during tab syncs, only collection key callbacks without waitForCollectionCallback: true (per-member mode) or regular/collection member keys.

The solution here is to batch the writes across tab syncs instead of treating each synced member as an independent update. InstanceSync now raises a single SYNC_ONYX event carrying the JSON array of all keys changed in one write (instead of one event per key), and the receiving tab reads them with a single multiGet and hands Onyx the whole batch via a new batched onStorageKeysChanged(pairs) callback (with a single-key fallback for backwards compatibility).

Onyx then processes that batch synchronously — grouping members by their parent collection and firing exactly one keysChanged() per affected collection (plus keyChanged() for individual keys) — which mirrors how a local mergeCollection works. This makes a cross-tab collection update notify each collection-root subscriber (Onyx.connect/useOnyx on a collection key) once, in O(N), rather than re-delivering the whole collection once per member (O(N^2), which can crash the receiving tab depending on the payload) or never delivering it at all.

@fabioh8010

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9263ec964d

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread lib/types.ts Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3da566e848

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread lib/OnyxConnectionManager.ts
Comment thread lib/storage/InstanceSync/index.web.ts
@mountiny mountiny self-requested a review June 26, 2026 13:52
@fabioh8010 fabioh8010 changed the title [WIP] Remove waitForCollectionCallback from Onyx Remove waitForCollectionCallback from Onyx Jun 26, 2026
@fabioh8010

Copy link
Copy Markdown
Contributor Author

I caught one bug with the tab sync logic when clearing Onyx cache, investigating it.

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.

1 participant