Skip to content

Add public Onyx.get - mutable - #850

Open
LukasMod wants to merge 9 commits into
Expensify:mainfrom
callstack-internal:feat/onyx-get-async-mutable
Open

LukasMod wants to merge 9 commits into
Expensify:mainfrom
callstack-internal:feat/onyx-get-async-mutable

Conversation

@LukasMod

@LukasMod LukasMod commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Details

Adds Onyx.get(key), a public one-shot asynchronous read on the Onyx object, so callers can read a key without opening a subscription.

What it does

  1. Onyx.get(key) and Onyx.multiGet(keys) join the Onyx object (lib/Onyx.ts:772 and lib/Onyx.ts:793, registered at lib/Onyx.ts:812-813) and come out of the default export. Both read once and open no subscription, so a caller can read a key without registering a listener.
  2. Both read only from the cache, through onyxSubscriptionManager.getState(key). No storage read and no wait for an in-flight GET. init() hydrates the cache from storage before it releases the deferred init task, and both methods run their body inside OnyxUtils.afterInit, so a call made before init() finishes still resolves with the hydrated value. Same value a subscription to that key would deliver.
  3. A collection key returns every member as one object, which the old internal OnyxUtils.get could not do. A single key or collection member returns its value. Passing report_ gives all reports, passing report_123 gives one.
  4. Results are the cached objects, not copies, and the type is mutable so a value passes straight to set, merge, update, and multiSet. The JSDoc and API.md say to treat them as read-only: a member is the live cached object, so an in-place edit skips notification, and a collection is a frozen snapshot, so adding a member needs a copy first.
  5. multiGet maps over the keys and returns a positional tuple through a cast. Each slot is undefined where its key has no value, without failing the other slots. multiGet([]) resolves to [].

Related Issues

Expensify/App#101910

Linked E/App PR

Expensify/App#102135

Automated Tests

This is a library read API with no UI surface, and the suite already asserts the behavior, so most of it needs no manual re-check. New cases added per method:

  • Onyx.get: 12 unit cases (10 in describe('get'), 2 in the after-init block), 4 type checks in tests/types/get.ts, and 2 perf benchmarks in tests/perf-test/Onyx.perf-test.ts.
  • Onyx.multiGet: 5 unit cases in describe('multiGet') and 4 type checks in tests/types/multiGet.ts.

Manual Tests

This is a library API addition with no UI surface, so it is exercised through the linked E/App PR rather than in isolation:

  1. Check out the linked E/App PR, which pins react-native-onyx to this PR's HEAD.

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 that 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-09-24.at.11.08.10.mov
Screenshot 2026-09-24 at 11 08 01

Adds Onyx.get(key), a public one-shot async read that resolves a key's current value without opening a subscription. The body runs inside OnyxUtils.afterInit, so a call made before init() finishes resolves with the hydrated value instead of undefined. A single key delegates to OnyxUtils.get and normalises null to undefined. A collection key returns the cached collection, or assembles one from getAllKeys and multiGet over the member keys, which also warms the cache. This closes the gap where the internal OnyxUtils.get cannot return a whole collection.

Returns Promise<OnyxValue<TKey>>, typed mutable like useOnyx, so a value read with get() goes straight back into set, merge, mergeCollection, and setCollection without a read-only wrapper. No new storage machinery; these are the same reads useOnyx already performs.
@LukasMod
LukasMod force-pushed the feat/onyx-get-async-mutable branch from ec1fe5b to 6edd3fc Compare September 24, 2026 07:00
@LukasMod
LukasMod marked this pull request as ready for review September 25, 2026 08:47
@LukasMod
LukasMod requested a review from a team as a code owner September 25, 2026 08:47
@melvin-bot
melvin-bot Bot requested review from mollfpr and removed request for a team September 25, 2026 08:48
@LukasMod

Copy link
Copy Markdown
Contributor Author

@MelvinBot Assign reviewer @aimane-chnaif as requested in Expensify/App#101910 (comment)

cc @mollfpr

@mollfpr

mollfpr commented Sep 25, 2026

Copy link
Copy Markdown

Assigned @aimane-chnaif

@mountiny
mountiny requested a review from chuckdries September 25, 2026 13:54
@fabioh8010

Copy link
Copy Markdown
Contributor

I'm going to review later today as well

Comment thread lib/Onyx.ts
Comment thread lib/Onyx.ts Outdated
Comment thread lib/Onyx.ts Outdated
Comment thread lib/Onyx.ts Outdated
Comment thread tests/types/get.ts
Comment thread tests/unit/onyxTest.ts Outdated
Comment thread tests/unit/onyxTest.ts Outdated
Comment thread tests/unit/onyxTest.ts Outdated
Comment thread tests/unit/onyxTest.ts Outdated
Comment thread tests/unit/onyxTest.ts
@fabioh8010

Copy link
Copy Markdown
Contributor

@chuckdries @aimane-chnaif @tgolen FYI I made a change here in both Onyx.get and Onyx.multiGet APIs to make them read only for cache instead of falling back to Storage. The reason is that:

  1. All storage data is loaded into cache during startup and Onyx.get/Onyx.multiGet are wrapped with OnyxUtils.afterInit, which guarantee they only execute once Onyx is fully initialised. In reality, the storage's fallback today is essentially dead code as it's never reached.
  2. The store-based solution is already following the same approach for useOnyx/connect, so it makes sense that Onyx.get would do the same for the sake of correctness.

Apart from that the API will stay the same so it won't affect the refactor plan. If in the future we decide to bring back storage fallback to Onyx, it's something we only need to do in its internals and won't require changes in E/App.

@tgolen

tgolen commented Sep 29, 2026 via email

Copy link
Copy Markdown
Collaborator

@fabioh8010

Copy link
Copy Markdown
Contributor

OK, thanks for the heads up! Just in case, are there any logs for when it
would have fallen back to storage? It would at least be nice to know if we
ever hit that log, or how often.

@tgolen I will have a look into that 👍

Also please hold merge here as #834 just got merged today and I have the check if we have conflicts

@aimane-chnaif aimane-chnaif 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.

PR description is out of date with the code

The description says get() falls back to storage through OnyxUtils.get / OnyxUtils.multiGet and cites old line numbers and test counts, but the current head reads only from the cache via onyxSubscriptionManager.getState.

Please update the description to match the current implementation.

Comment thread tests/types/get.ts Outdated
The type test wrote through the cached value while the JSDoc says read-only. A draft copy keeps the
mutability check via the typeof annotation, since a spread alone drops readonly modifiers. The exports
were never needed, tsconfig.test.json checks the file without them.
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.

5 participants