Minimum interval between disk-pressure alerts. One disk-pressure burst fails every queued operation
with the identical error, so per-operation logging would amplify the very storm it reports.
Sentinel for "nothing delivered yet" in connect()'s per-subscription dedup. A Symbol
+can't collide with any real Onyx value, so the first Object.is check never matches and
+the initial fire runs even when a key's genuine first value is undefined. It only needs
+to be distinct from real values, not unique per subscription, so one module-level instance
+is reused by every connection.
+
## Functions
@@ -17,6 +24,21 @@ with the identical error, so per-operation logging would amplify the very storm
Registers an in-flight write so scheduleInitialFire can wait for it. Returns the same
+promise so callers can wrap a write's return value inline. The write is removed from the
+pending set once it settles (success or failure).
Resolves once no write operations are in flight. Re-checks after each drain because a
+settling write can apply cache changes that spawn further writes (e.g. Onyx.update
+fans out into per-item merges), and those must be awaited too. Write failures are
+swallowed here: this only cares that writes have settled, not that they succeeded.
@@ -62,33 +84,20 @@ The resulting collection will only contain items that are returned by the select
to the values for those keys (correctly typed) such as [OnyxCollection<Report>, OnyxEntry<string>]
Note: just using .map, you'd end up with Array<OnyxCollection<Report>|OnyxEntry<string>>, which is not what we want. This preserves the order of the keys provided.
Tries to get a value from the cache. If the value is not present in cache it will return the default value or undefined.
-If the requested key is a collection, it will return an object with all the collection members.
Notify subscribers of a batch collection update. Wrapper over
+onyxSubscriptionManager.notifyCollection() that also performs LRU bookkeeping per
+changed member.
Handles storage operation failures based on the error class (see lib/storage/errors.ts).
@@ -136,12 +145,6 @@ It will also mark deep nested objects that need to be entirely replaced during t
Writes a value to our store with the given key.
Serves as core implementation for Onyx.set() public function, the difference being
@@ -179,12 +182,46 @@ Retries on failure.
Minimum interval between disk-pressure alerts. One disk-pressure burst fails every queued operation
with the identical error, so per-operation logging would amplify the very storm it reports.
+**Kind**: global constant
+
+
+## NOT\_DELIVERED
+Sentinel for "nothing delivered yet" in `connect()`'s per-subscription dedup. A Symbol
+can't collide with any real Onyx value, so the first `Object.is` check never matches and
+the initial fire runs even when a key's genuine first value is `undefined`. It only needs
+to be distinct from real values, not unique per subscription, so one module-level instance
+is reused by every connection.
+
**Kind**: global constant
## resetDiskPressureLogThrottle()
Test-only: clears the disk-pressure log throttle so each test observes its own alert.
+**Kind**: global function
+
+
+## trackPendingWrite()
+Registers an in-flight write so `scheduleInitialFire` can wait for it. Returns the same
+promise so callers can wrap a write's return value inline. The write is removed from the
+pending set once it settles (success or failure).
+
+**Kind**: global function
+
+
+## whenWritesSettled()
+Resolves once no write operations are in flight. Re-checks after each drain because a
+settling write can apply cache changes that spawn further writes (e.g. `Onyx.update`
+fans out into per-item merges), and those must be awaited too. Write failures are
+swallowed here: this only cares that writes have settled, not that they succeeded.
+
+**Kind**: global function
+
+
+## scheduleInitialFire()
+Defer a `Onyx.connect` callback's initial fire until writes issued in the same tick have
+applied, so it reads post-write cache.
+
**Kind**: global function
@@ -284,70 +321,31 @@ to the values for those keys (correctly typed) such as `[OnyxCollection,
Note: just using `.map`, you'd end up with `Array|OnyxEntry>`, which is not what we want. This preserves the order of the keys provided.
**Kind**: global function
-
-
-## storeKeyBySubscriptions(subscriptionID, key)
-Stores a subscription ID associated with a given key.
-
-**Kind**: global function
-
-| Param | Description |
-| --- | --- |
-| subscriptionID | A subscription ID of the subscriber. |
-| key | A key that the subscriber is subscribed to. |
-
-
-
-## deleteKeyBySubscriptions(subscriptionID)
-Deletes a subscription ID associated with its corresponding key.
-
-**Kind**: global function
-
-| Param | Description |
-| --- | --- |
-| subscriptionID | The subscription ID to be deleted. |
-
## getAllKeys()
Returns current key names stored in persisted storage
**Kind**: global function
-
-
-## tryGetCachedValue()
-Tries to get a value from the cache. If the value is not present in cache it will return the default value or undefined.
-If the requested key is a collection, it will return an object with all the collection members.
-
-**Kind**: global function
-
-
-## keysChanged()
-When a collection of keys change, search for any callbacks matching the collection key and trigger those callbacks
-
-**Kind**: global function
-
-
-## keyChanged()
-When a key change happens, search for any callbacks matching the key or collection key and trigger those callbacks
-
-**Kind**: global function
-
+
-## sendDataToConnection()
-Sends the data obtained from the keys to the connection.
+## notifyKey()
+Notify subscribers of a single-key write. Wrapper over `onyxSubscriptionManager.notifyKey()`
+that also performs LRU bookkeeping for eviction.
**Kind**: global function
-
+
-## getCollectionDataAndSendAsObject()
-Gets the data for a given an array of matching keys, combines them into an object, and sends the result back to the subscriber.
+## notifyCollection()
+Notify subscribers of a batch collection update. Wrapper over
+`onyxSubscriptionManager.notifyCollection()` that also performs LRU bookkeeping per
+changed member.
**Kind**: global function
## remove()
-Remove a key from Onyx and update the subscribers
+Remove a key from Onyx and update the subscribers.
**Kind**: global function
@@ -439,29 +437,6 @@ Validate the collection is not empty and has a correct type before applying merg
Verify if all the collection keys belong to the same parent
**Kind**: global function
-
-
-## subscribeToKey(connectOptions) ⇒
-Subscribes to an Onyx key and listens to its changes.
-
-**Kind**: global function
-**Returns**: The subscription ID to use when calling `OnyxUtils.unsubscribeFromKey()`.
-
-| Param | Description |
-| --- | --- |
-| connectOptions | The options object that will define the behavior of the connection. |
-
-
-
-## unsubscribeFromKey(subscriptionID)
-Disconnects and removes the listener from the Onyx key.
-
-**Kind**: global function
-
-| Param | Description |
-| --- | --- |
-| subscriptionID | Subscription ID returned by calling `OnyxUtils.subscribeToKey()`. |
-
## setWithRetry(params, retryAttempt)
diff --git a/API.md b/API.md
index e766e551f..717fab8bd 100644
--- a/API.md
+++ b/API.md
@@ -73,36 +73,40 @@ Connects to an Onyx key given the options passed and listens to its changes.
This method will be deprecated soon. Please use `Onyx.connectWithoutView()` instead.
**Kind**: global function
-**Returns**: The connection object to use when calling `Onyx.disconnect()`.
+**Returns**: The `Connection` handle to use when calling `Onyx.disconnect()`.
| Param | Description |
| --- | --- |
| connectOptions | The options object that will define the behavior of the connection. |
| connectOptions.key | The Onyx key to subscribe to. |
| connectOptions.callback | A function that will be called when the Onyx data we are subscribed changes. |
-| connectOptions.selector | This will be used to subscribe to a subset of an Onyx key's data. **Only used inside `useOnyx()` hook.** Using this setting on `useOnyx()` can have very positive performance benefits because the component will only re-render when the subset of data changes. Otherwise, any change of data on any property would normally cause the component to re-render (and that can be expensive from a performance standpoint). |
**Example**
```ts
-const connection = Onyx.connectWithoutView({
+const connection = Onyx.connect({
key: ONYXKEYS.SESSION,
callback: onSessionChange,
});
```
+
+For a collection root key, the callback fires with the entire frozen collection
+object whenever any member changes; signature `(collection, collectionKey)`.
+For any other key, the callback fires with the value at that key; signature
+`(value, key)`. Initial fire is deferred via `scheduleInitialFire` so it reads
+cache after any same-tick writes have applied.
## connectWithoutView(connectOptions) ⇒
Connects to an Onyx key given the options passed and listens to its changes.
**Kind**: global function
-**Returns**: The connection object to use when calling `Onyx.disconnect()`.
+**Returns**: The `Connection` handle to use when calling `Onyx.disconnect()`.
| Param | Description |
| --- | --- |
| connectOptions | The options object that will define the behavior of the connection. |
| connectOptions.key | The Onyx key to subscribe to. |
| connectOptions.callback | A function that will be called when the Onyx data we are subscribed changes. |
-| connectOptions.selector | This will be used to subscribe to a subset of an Onyx key's data. **Only used inside `useOnyx()` hook.** Using this setting on `useOnyx()` can have very positive performance benefits because the component will only re-render when the subset of data changes. Otherwise, any change of data on any property would normally cause the component to re-render (and that can be expensive from a performance standpoint). |
**Example**
```ts
@@ -111,6 +115,12 @@ const connection = Onyx.connectWithoutView({
callback: onSessionChange,
});
```
+
+For a collection root key, the callback fires with the entire frozen collection
+object whenever any member changes; signature `(collection, collectionKey)`.
+For any other key, the callback fires with the value at that key; signature
+`(value, key)`. Initial fire is deferred via `scheduleInitialFire` so it reads
+cache after any same-tick writes have applied.
## disconnect(connection)
diff --git a/README.md b/README.md
index 56820a456..1d5f73c52 100644
--- a/README.md
+++ b/README.md
@@ -9,7 +9,7 @@ Awesome persistent storage solution wrapped in a Pub/Sub library.
- Onyx allows other code to subscribe to changes in data, and then publishes change events whenever data is changed
- Anything needing to read Onyx data needs to:
1. Know what key the data is stored in (for web, you can find this by looking in the JS console > Application > local storage)
- 2. Subscribe to changes of the data for a particular key or set of keys. React function components use the `useOnyx()` hook and non-React libs use `Onyx.connect()`.
+ 2. Subscribe to changes of the data for a particular key or set of keys. React function components use the `useOnyx()` hook and non-React libs use `Onyx.connectWithoutView()`.
3. Get initialized with the current value of that key from persistent storage (Onyx does this by calling `setState()` or triggering the `callback` with the values currently on disk as part of the connection process)
- Subscribing to Onyx keys is done using a constant defined in `ONYXKEYS`. Each Onyx key represents either a collection of items or a specific entry in storage. For example, since all reports are stored as individual keys like `report_1234`, if code needs to know about all the reports (e.g. display a list of them in the nav menu), then it would subscribe to the key `ONYXKEYS.COLLECTION.REPORT`.
@@ -120,20 +120,20 @@ You should avoid arrays as much as possible. They do not work well with `merge()
## Subscribing to data changes
-To set up a basic subscription for a given key use the `Onyx.connect()` method.
+To set up a basic subscription for a given key outside of a React component use the `Onyx.connectWithoutView()` method. It returns a connection handle.
```javascript
let session;
-const connectionID = Onyx.connect({
+const connection = Onyx.connectWithoutView({
key: ONYXKEYS.SESSION,
callback: (val) => session = val || {},
});
```
-To teardown the subscription call `Onyx.disconnect()` with the `connectionID` returned from `Onyx.connect()`. It's recommended to clean up subscriptions anytime you are connecting from within a function to prevent memory leaks.
+To teardown the subscription call `Onyx.disconnect()` with the connection returned from `Onyx.connectWithoutView()`. It's recommended to clean up subscriptions anytime you are connecting from within a function to prevent memory leaks.
```javascript
-Onyx.disconnect(connectionID);
+Onyx.disconnect(connection);
```
We can also access values inside React function components via the `useOnyx()` [hook](https://react.dev/reference/react/hooks). When the data changes the component will re-render.
@@ -204,7 +204,7 @@ export default App;
* It is VERY important to NOT use empty string default values like `report.policyID || ''`. This results in the key returned to `useOnyx` as `policies_`, which subscribes to the ENTIRE POLICY COLLECTION and is most assuredly not what you were intending. You can use a default of `0` (as long as you are reasonably sure that there is never a policyID=0). This allows Onyx to return `undefined` as the value of the policy key, which is handled by `useOnyx` appropriately.
-It's also beneficial to use a [selector](https://github.com/Expensify/react-native-onyx/blob/main/API.md#connectmapping--number) with the mapping in case you need to grab a single item in a collection (like a single report action).
+It's also beneficial to use `useOnyx()`'s `selector` option in case you need to grab a single item in a collection (like a single report action).
## Collections
@@ -257,7 +257,7 @@ export default MyComponent;
This will add a prop to the component called `allReports` which is an object of collection member key/values. Changes to the individual member keys will modify the entire object and new props will be passed with each individual key update. The prop doesn't update on the initial rendering of the component until the entire collection has been read out of Onyx.
```js
-Onyx.connect({key: ONYXKEYS.COLLECTION.REPORT}, callback: (allReports, collectionKey) => {...});
+Onyx.connectWithoutView({key: ONYXKEYS.COLLECTION.REPORT, callback: (allReports, collectionKey) => {...}});
```
This will fire the callback once with the entire collection initially and later with an updated version of the collection when individual keys update.
@@ -289,7 +289,7 @@ function signOut() {
```
## Storage Providers
-`Onyx.get`, `Onyx.set`, and the rest of the API accesses the underlying storage
+`Onyx.set`, `Onyx.merge`, and the rest of the API accesses the underlying storage
differently depending on the platform
Under the hood storage access calls are delegated to a [`StorageProvider`](lib/storage/index.js)
diff --git a/lib/Onyx.ts b/lib/Onyx.ts
index 29a1d20ab..8d2a2c96f 100644
--- a/lib/Onyx.ts
+++ b/lib/Onyx.ts
@@ -4,8 +4,11 @@ import Storage from './storage';
import utils from './utils';
import DevTools, {initDevTools} from './DevTools';
import type {
+ CollectionConnectCallback,
CollectionKeyBase,
+ Connection,
ConnectOptions,
+ DefaultConnectCallback,
InitOptions,
KeyValueMapping,
MixedOperationsQueue,
@@ -28,8 +31,7 @@ import type {
import OnyxUtils from './OnyxUtils';
import OnyxKeys from './OnyxKeys';
import logMessages from './logMessages';
-import type {Connection} from './OnyxConnectionManager';
-import connectionManager from './OnyxConnectionManager';
+import onyxSubscriptionManager from './OnyxSubscriptionManager';
import OnyxMerge from './OnyxMerge';
/** Initialize the store with actions and listening for storage events */
@@ -71,7 +73,7 @@ function init({
const collectionKey = OnyxKeys.getCollectionKey(key);
const isCollectionMember = !!collectionKey && OnyxKeys.isCollectionMemberKey(collectionKey, key);
- // Capture the previous cached value BEFORE cache.set() so keysChanged() can diff old vs new per member.
+ // Capture the previous cached value before cache.set() so notifyCollection() can diff old vs new per member.
const previousValue = isCollectionMember ? cache.get(key) : undefined;
cache.set(key, value);
@@ -92,15 +94,15 @@ function init({
}
}
- // Non-collection keys: notify individually, matching keyChanged() semantics for exact keys.
+ // Non-collection keys: notify individually, matching notifyKey() semantics for exact keys.
for (const [key, value] of individual) {
- OnyxUtils.keyChanged(key, value);
+ OnyxUtils.notifyKey(key, value);
}
- // One keysChanged() per collection notifies the collection-root subscriber once and lets
- // keysChanged() decide which individual member subscribers actually changed.
+ // One notifyCollection() per collection notifies the collection-root subscriber once and lets
+ // notifyCollection() decide which individual member subscribers actually changed.
for (const [collectionKey, {partial, previous}] of collectionBatches) {
- OnyxUtils.keysChanged(collectionKey, partial, previous);
+ OnyxUtils.notifyCollection(collectionKey, partial, previous);
}
});
}
@@ -122,23 +124,95 @@ function init({
*
* @example
* ```ts
- * const connection = Onyx.connectWithoutView({
+ * const connection = Onyx.connect({
* key: ONYXKEYS.SESSION,
* callback: onSessionChange,
* });
* ```
*
+ * For a collection root key, the callback fires with the entire frozen collection
+ * object whenever any member changes; signature `(collection, collectionKey)`.
+ * For any other key, the callback fires with the value at that key; signature
+ * `(value, key)`. Initial fire is deferred via `scheduleInitialFire` so it reads
+ * cache after any same-tick writes have applied.
+ *
* @param connectOptions The options object that will define the behavior of the connection.
* @param connectOptions.key The Onyx key to subscribe to.
* @param connectOptions.callback A function that will be called when the Onyx data we are subscribed changes.
- * @param connectOptions.selector This will be used to subscribe to a subset of an Onyx key's data. **Only used inside `useOnyx()` hook.**
- * Using this setting on `useOnyx()` can have very positive performance benefits because the component will only re-render
- * when the subset of data changes. Otherwise, any change of data on any property would normally
- * cause the component to re-render (and that can be expensive from a performance standpoint).
- * @returns The connection object to use when calling `Onyx.disconnect()`.
+ * @returns The `Connection` handle to use when calling `Onyx.disconnect()`.
*/
function connect(connectOptions: ConnectOptions): Connection {
- return connectionManager.connect(connectOptions);
+ const {key, callback} = connectOptions;
+
+ let active = true;
+ let unsubscribeFn: (() => void) | null = null;
+
+ const wireUp = () => {
+ if (!active) {
+ return;
+ }
+
+ if (OnyxKeys.isCollectionKey(key)) {
+ // Collection-root mode: dedup skips identical collection refs. Initial fire delivers
+ // the current collection object: frozen `{}` for an empty-but-known collection,
+ // `undefined` only if the collection key has not been seen yet.
+ let lastDeliveredCollection: unknown = OnyxUtils.NOT_DELIVERED;
+ const deliverCollection = (rawCollection: OnyxValue | undefined, k: TKey) => {
+ if (Object.is(lastDeliveredCollection, rawCollection)) {
+ return;
+ }
+ lastDeliveredCollection = rawCollection;
+ (callback as CollectionConnectCallback | undefined)?.(rawCollection as NonNullable>, k);
+ };
+ unsubscribeFn = onyxSubscriptionManager.subscribe(key, (value, k) => {
+ deliverCollection(value as unknown as OnyxValue, k as TKey);
+ });
+ OnyxUtils.scheduleInitialFire(key, () => {
+ if (!active) {
+ return;
+ }
+ deliverCollection(onyxSubscriptionManager.getState(key) as unknown as OnyxValue, key as TKey);
+ });
+ return;
+ }
+
+ // Non-collection key (or a specific collection member): single-value subscription.
+ let lastDelivered: unknown = OnyxUtils.NOT_DELIVERED;
+ const deliverValue = (value: OnyxValue, k: TKey | undefined) => {
+ if (Object.is(lastDelivered, value)) {
+ return;
+ }
+ lastDelivered = value;
+ (callback as DefaultConnectCallback | undefined)?.(value, k as TKey);
+ };
+ unsubscribeFn = onyxSubscriptionManager.subscribe(key, (value, k) => {
+ deliverValue(value, k as TKey);
+ });
+ OnyxUtils.scheduleInitialFire(key, () => {
+ if (!active) {
+ return;
+ }
+ deliverValue(onyxSubscriptionManager.getState(key), key);
+ });
+ };
+
+ OnyxUtils.afterInit(() => {
+ wireUp();
+ return Promise.resolve();
+ });
+
+ return {
+ unsubscribe: () => {
+ if (!active) {
+ return;
+ }
+ active = false;
+ if (unsubscribeFn) {
+ unsubscribeFn();
+ unsubscribeFn = null;
+ }
+ },
+ };
}
/**
@@ -152,17 +226,19 @@ function connect(connectOptions: ConnectOptions): Co
* });
* ```
*
+ * For a collection root key, the callback fires with the entire frozen collection
+ * object whenever any member changes; signature `(collection, collectionKey)`.
+ * For any other key, the callback fires with the value at that key; signature
+ * `(value, key)`. Initial fire is deferred via `scheduleInitialFire` so it reads
+ * cache after any same-tick writes have applied.
+ *
* @param connectOptions The options object that will define the behavior of the connection.
* @param connectOptions.key The Onyx key to subscribe to.
* @param connectOptions.callback A function that will be called when the Onyx data we are subscribed changes.
- * @param connectOptions.selector This will be used to subscribe to a subset of an Onyx key's data. **Only used inside `useOnyx()` hook.**
- * Using this setting on `useOnyx()` can have very positive performance benefits because the component will only re-render
- * when the subset of data changes. Otherwise, any change of data on any property would normally
- * cause the component to re-render (and that can be expensive from a performance standpoint).
- * @returns The connection object to use when calling `Onyx.disconnect()`.
+ * @returns The `Connection` handle to use when calling `Onyx.disconnect()`.
*/
function connectWithoutView(connectOptions: ConnectOptions): Connection {
- return connectionManager.connect(connectOptions);
+ return connect(connectOptions);
}
/**
@@ -181,7 +257,10 @@ function connectWithoutView(connectOptions: ConnectOptions
* @param connection Connection object returned by calling `Onyx.connect()` or `Onyx.connectWithoutView()`.
*/
function disconnect(connection: Connection): void {
- connectionManager.disconnect(connection);
+ if (!connection) {
+ return;
+ }
+ connection.unsubscribe();
}
/**
@@ -192,7 +271,10 @@ function disconnect(connection: Connection): void {
* @param options optional configuration object
*/
function set(key: TKey, value: OnyxSetInput, options?: SetOptions): Promise {
- return OnyxUtils.afterInit(() => OnyxUtils.setWithRetry({key, value, options}));
+ return OnyxUtils.trackPendingWrite(
+ key,
+ OnyxUtils.afterInit(() => OnyxUtils.setWithRetry({key, value, options})),
+ );
}
/**
@@ -203,7 +285,10 @@ function set(key: TKey, value: OnyxSetInput, options
* @param data object keyed by ONYXKEYS and the values to set
*/
function multiSet(data: OnyxMultiSetInput): Promise {
- return OnyxUtils.afterInit(() => OnyxUtils.multiSetWithRetry(data));
+ return OnyxUtils.trackPendingWrite(
+ Object.keys(data),
+ OnyxUtils.afterInit(() => OnyxUtils.multiSetWithRetry(data)),
+ );
}
/**
@@ -223,91 +308,94 @@ function multiSet(data: OnyxMultiSetInput): Promise {
* Onyx.merge(ONYXKEYS.POLICY, {name: 'My Workspace'}); // -> {id: 1, name: 'My Workspace'}
*/
function merge(key: TKey, changes: OnyxMergeInput): Promise {
- return OnyxUtils.afterInit(() => {
- const skippableCollectionMemberIDs = OnyxUtils.getSkippableCollectionMemberIDs();
- if (skippableCollectionMemberIDs.size) {
- try {
- const [, collectionMemberID] = OnyxKeys.splitCollectionMemberKey(key);
- if (skippableCollectionMemberIDs.has(collectionMemberID)) {
- // The key is a skippable one, so we set the new changes to undefined.
- // eslint-disable-next-line no-param-reassign
- changes = undefined;
+ return OnyxUtils.trackPendingWrite(
+ key,
+ OnyxUtils.afterInit(() => {
+ const skippableCollectionMemberIDs = OnyxUtils.getSkippableCollectionMemberIDs();
+ if (skippableCollectionMemberIDs.size) {
+ try {
+ const [, collectionMemberID] = OnyxKeys.splitCollectionMemberKey(key);
+ if (skippableCollectionMemberIDs.has(collectionMemberID)) {
+ // The key is a skippable one, so we set the new changes to undefined.
+ // eslint-disable-next-line no-param-reassign
+ changes = undefined;
+ }
+ } catch (e) {
+ // The key is not a collection one or something went wrong during split, so we proceed with the function's logic.
}
- } catch (e) {
- // The key is not a collection one or something went wrong during split, so we proceed with the function's logic.
}
- }
- const mergeQueue = OnyxUtils.getMergeQueue();
- const mergeQueuePromise = OnyxUtils.getMergeQueuePromise();
+ const mergeQueue = OnyxUtils.getMergeQueue();
+ const mergeQueuePromise = OnyxUtils.getMergeQueuePromise();
- // Top-level undefined values are ignored
- // Therefore, we need to prevent adding them to the merge queue
- if (changes === undefined) {
- return mergeQueue[key] ? mergeQueuePromise[key] : Promise.resolve();
- }
-
- // Merge attempts are batched together. The delta should be applied after a single call to get() to prevent a race condition.
- // Using the initial value from storage in subsequent merge attempts will lead to an incorrect final merged value.
- if (mergeQueue[key]) {
- mergeQueue[key].push(changes);
- return mergeQueuePromise[key];
- }
- mergeQueue[key] = [changes];
-
- mergeQueuePromise[key] = OnyxUtils.get(key).then((valueFromGet) => {
- // Calls to Onyx.set after a merge will terminate the current merge process and clear the merge queue
- if (mergeQueue[key] == null) {
- return Promise.resolve();
+ // Top-level undefined values are ignored
+ // Therefore, we need to prevent adding them to the merge queue
+ if (changes === undefined) {
+ return mergeQueue[key] ? mergeQueuePromise[key] : Promise.resolve();
}
- // Other writers (notably Onyx.update's mergeCollection path, which doesn't participate in mergeQueue)
- // can land between get() resolving and this callback running. Applying the delta on top of the value
- // captured back then and broadcasting it would overwrite those writes wholesale, so re-read the cache.
- const existingValue = cache.hasCacheForKey(key) ? (cache.get(key) as OnyxInput | undefined) : valueFromGet;
-
- try {
- const validChanges = mergeQueue[key].filter((change) => {
- const {isCompatible, existingValueType, newValueType, isEmptyArrayCoercion} = utils.checkCompatibilityWithExistingValue(change, existingValue);
- if (isEmptyArrayCoercion) {
- // Merging an object into an empty array isn't semantically correct, but we allow it
- // in case we accidentally encoded an empty object as an empty array in PHP. If you're
- // looking at a bugbot from this message, we're probably missing that key in OnyxKeys::KEYS_REQUIRING_EMPTY_OBJECT
- Logger.logAlert(`[ENSURE_BUGBOT] Onyx merge called on key "${key}" whose existing value is an empty array. Will coerce to object.`);
- }
- if (!isCompatible) {
- Logger.logAlert(logMessages.incompatibleUpdateAlert(key, 'merge', existingValueType, newValueType));
- }
- return isCompatible;
- }) as Array>;
-
- // Clean up the write queue, so we don't apply these changes again.
- delete mergeQueue[key];
- delete mergeQueuePromise[key];
+ // Merge attempts are batched together. The delta should be applied after a single call to get() to prevent a race condition.
+ // Using the initial value from storage in subsequent merge attempts will lead to an incorrect final merged value.
+ if (mergeQueue[key]) {
+ mergeQueue[key].push(changes);
+ return mergeQueuePromise[key];
+ }
+ mergeQueue[key] = [changes];
- if (!validChanges.length) {
+ mergeQueuePromise[key] = OnyxUtils.get(key).then((valueFromGet) => {
+ // Calls to Onyx.set after a merge will terminate the current merge process and clear the merge queue
+ if (mergeQueue[key] == null) {
return Promise.resolve();
}
- // If the last change is null, we can just delete the key.
- // Therefore, we don't need to further broadcast and update the value so we can return early.
- if (validChanges.at(-1) === null) {
- OnyxUtils.remove(key);
- OnyxUtils.logKeyRemoved(OnyxUtils.METHOD.MERGE, key);
+ // Other writers (notably Onyx.update's mergeCollection path, which doesn't participate in mergeQueue)
+ // can land between get() resolving and this callback running. Applying the delta on top of the value
+ // captured back then and broadcasting it would overwrite those writes wholesale, so re-read the cache.
+ const existingValue = cache.hasCacheForKey(key) ? (cache.get(key) as OnyxInput | undefined) : valueFromGet;
+
+ try {
+ const validChanges = mergeQueue[key].filter((change) => {
+ const {isCompatible, existingValueType, newValueType, isEmptyArrayCoercion} = utils.checkCompatibilityWithExistingValue(change, existingValue);
+ if (isEmptyArrayCoercion) {
+ // Merging an object into an empty array isn't semantically correct, but we allow it
+ // in case we accidentally encoded an empty object as an empty array in PHP. If you're
+ // looking at a bugbot from this message, we're probably missing that key in OnyxKeys::KEYS_REQUIRING_EMPTY_OBJECT
+ Logger.logAlert(`[ENSURE_BUGBOT] Onyx merge called on key "${key}" whose existing value is an empty array. Will coerce to object.`);
+ }
+ if (!isCompatible) {
+ Logger.logAlert(logMessages.incompatibleUpdateAlert(key, 'merge', existingValueType, newValueType));
+ }
+ return isCompatible;
+ }) as Array>;
+
+ // Clean up the write queue, so we don't apply these changes again.
+ delete mergeQueue[key];
+ delete mergeQueuePromise[key];
+
+ if (!validChanges.length) {
+ return Promise.resolve();
+ }
+
+ // If the last change is null, we can just delete the key.
+ // Therefore, we don't need to further broadcast and update the value so we can return early.
+ if (validChanges.at(-1) === null) {
+ OnyxUtils.remove(key);
+ OnyxUtils.logKeyRemoved(OnyxUtils.METHOD.MERGE, key);
+ return Promise.resolve();
+ }
+
+ return OnyxMerge.applyMerge(key, existingValue, validChanges).then(({mergedValue}) => {
+ OnyxUtils.sendActionToDevTools(OnyxUtils.METHOD.MERGE, key, changes, mergedValue);
+ });
+ } catch (error) {
+ Logger.logAlert(`An error occurred while applying merge for key: ${key}, Error: ${error instanceof Error ? error.toString() : String(error)}`);
return Promise.resolve();
}
+ });
- return OnyxMerge.applyMerge(key, existingValue, validChanges).then(({mergedValue}) => {
- OnyxUtils.sendActionToDevTools(OnyxUtils.METHOD.MERGE, key, changes, mergedValue);
- });
- } catch (error) {
- Logger.logAlert(`An error occurred while applying merge for key: ${key}, Error: ${error instanceof Error ? error.toString() : String(error)}`);
- return Promise.resolve();
- }
- });
-
- return mergeQueuePromise[key];
- });
+ return mergeQueuePromise[key];
+ }),
+ );
}
/**
@@ -324,7 +412,10 @@ function merge(key: TKey, changes: OnyxMergeInput):
* @param collection Object collection keyed by individual collection member keys and values
*/
function mergeCollection(collectionKey: TKey, collection: OnyxMergeCollectionInput): Promise {
- return OnyxUtils.afterInit(() => OnyxUtils.mergeCollectionWithPatches({collectionKey, collection}));
+ return OnyxUtils.trackPendingWrite(
+ Object.keys(collection),
+ OnyxUtils.afterInit(() => OnyxUtils.mergeCollectionWithPatches({collectionKey, collection})),
+ );
}
/**
@@ -349,99 +440,100 @@ function mergeCollection(collectionKey: TKey, co
* @param keysToPreserve is a list of ONYXKEYS that should not be cleared with the rest of the data
*/
function clear(keysToPreserve: OnyxKey[] = []): Promise {
- return OnyxUtils.afterInit(() => {
- const defaultKeyStates = OnyxUtils.getDefaultKeyStates();
- const initialKeys = Object.keys(defaultKeyStates);
-
- const promise = OnyxUtils.getAllKeys()
- .then((cachedKeys) => {
- cache.clearNullishStorageKeys();
-
- const keysToBeClearedFromStorage: OnyxKey[] = [];
- const keyValuesToResetIndividually: KeyValueMapping = {};
- // We need to store old and new values for collection keys to properly notify subscribers when clearing Onyx
- // because the notification process needs the old values in cache but at that point they will be already removed from it.
- const keyValuesToResetAsCollection: Record<
- OnyxKey,
- {oldValues: Record; newValues: Record}
- > = {};
-
- const allKeys = new Set([...cachedKeys, ...initialKeys]);
-
- // The only keys that should not be cleared are:
- // 1. Anything specifically passed in keysToPreserve (because some keys like language preferences, offline
- // status, or activeClients need to remain in Onyx even when signed out)
- // 2. Any keys with a default state (because they need to remain in Onyx as their default, and setting them
- // to null would cause unknown behavior)
- // 2.1 However, if a default key was explicitly set to null, we need to reset it to the default value
- for (const key of allKeys) {
- const isKeyToPreserve = keysToPreserve.some((preserveKey) => OnyxKeys.isKeyMatch(preserveKey, key));
- const isDefaultKey = key in defaultKeyStates;
-
- // If the key is being removed or reset to default:
- // 1. Update it in the cache
- // 2. Figure out whether it is a collection key or not,
- // since collection key subscribers need to be updated differently
- if (!isKeyToPreserve) {
- const oldValue = cache.get(key);
- const newValue = defaultKeyStates[key] ?? null;
- if (newValue !== oldValue) {
- cache.set(key, newValue);
-
- const collectionKey = OnyxKeys.getCollectionKey(key);
-
- if (collectionKey) {
- if (!keyValuesToResetAsCollection[collectionKey]) {
- keyValuesToResetAsCollection[collectionKey] = {oldValues: {}, newValues: {}};
+ return OnyxUtils.trackPendingGlobalWrite(
+ OnyxUtils.afterInit(() => {
+ const defaultKeyStates = OnyxUtils.getDefaultKeyStates();
+ const initialKeys = Object.keys(defaultKeyStates);
+
+ const promise = OnyxUtils.getAllKeys()
+ .then((cachedKeys) => {
+ cache.clearNullishStorageKeys();
+
+ const keysToBeClearedFromStorage: OnyxKey[] = [];
+ const keyValuesToResetIndividually: KeyValueMapping = {};
+ // We need to store old and new values for collection keys to properly notify subscribers when clearing Onyx
+ // because the notification process needs the old values in cache but at that point they will be already removed from it.
+ const keyValuesToResetAsCollection: Record<
+ OnyxKey,
+ {oldValues: Record; newValues: Record}
+ > = {};
+
+ const allKeys = new Set([...cachedKeys, ...initialKeys]);
+
+ // The only keys that should not be cleared are:
+ // 1. Anything specifically passed in keysToPreserve (because some keys like language preferences, offline
+ // status, or activeClients need to remain in Onyx even when signed out)
+ // 2. Any keys with a default state (because they need to remain in Onyx as their default, and setting them
+ // to null would cause unknown behavior)
+ // 2.1 However, if a default key was explicitly set to null, we need to reset it to the default value
+ for (const key of allKeys) {
+ const isKeyToPreserve = keysToPreserve.some((preserveKey) => OnyxKeys.isKeyMatch(preserveKey, key));
+ const isDefaultKey = key in defaultKeyStates;
+
+ // If the key is being removed or reset to default:
+ // 1. Update it in the cache
+ // 2. Figure out whether it is a collection key or not,
+ // since collection key subscribers need to be updated differently
+ if (!isKeyToPreserve) {
+ const oldValue = cache.get(key);
+ const newValue = defaultKeyStates[key] ?? null;
+ if (newValue !== oldValue) {
+ cache.set(key, newValue);
+
+ const collectionKey = OnyxKeys.getCollectionKey(key);
+
+ if (collectionKey) {
+ if (!keyValuesToResetAsCollection[collectionKey]) {
+ keyValuesToResetAsCollection[collectionKey] = {oldValues: {}, newValues: {}};
+ }
+ keyValuesToResetAsCollection[collectionKey].oldValues[key] = oldValue;
+ keyValuesToResetAsCollection[collectionKey].newValues[key] = newValue ?? undefined;
+ } else {
+ keyValuesToResetIndividually[key] = newValue ?? undefined;
}
- keyValuesToResetAsCollection[collectionKey].oldValues[key] = oldValue;
- keyValuesToResetAsCollection[collectionKey].newValues[key] = newValue ?? undefined;
- } else {
- keyValuesToResetIndividually[key] = newValue ?? undefined;
}
}
- }
-
- if (isKeyToPreserve || isDefaultKey) {
- continue;
- }
- // If it isn't preserved and doesn't have a default, we'll remove it
- keysToBeClearedFromStorage.push(key);
- }
+ if (isKeyToPreserve || isDefaultKey) {
+ continue;
+ }
- // Exclude RAM-only keys to prevent them from being saved to storage
- const defaultKeyValuePairs = Object.entries(
- Object.keys(defaultKeyStates)
- .filter((key) => !keysToPreserve.some((preserveKey) => OnyxKeys.isKeyMatch(preserveKey, key)) && !OnyxKeys.isRamOnlyKey(key))
- .reduce((obj: KeyValueMapping, key) => {
- // eslint-disable-next-line no-param-reassign
- obj[key] = defaultKeyStates[key];
- return obj;
- }, {}),
- );
+ // If it isn't preserved and doesn't have a default, we'll remove it
+ keysToBeClearedFromStorage.push(key);
+ }
- // Remove only the items that we want cleared from storage, and reset others to default
- for (const key of keysToBeClearedFromStorage) cache.drop(key);
- return Storage.removeItems(keysToBeClearedFromStorage)
- .then(() => connectionManager.refreshSessionID())
- .then(() => Storage.multiSet(defaultKeyValuePairs))
- .then(() => {
- DevTools.clearState(keysToPreserve);
-
- // Notify the subscribers for each key/value group so they can receive the new values
- for (const [key, value] of Object.entries(keyValuesToResetIndividually)) {
- OnyxUtils.keyChanged(key, value);
- }
- for (const [key, value] of Object.entries(keyValuesToResetAsCollection)) {
- OnyxUtils.keysChanged(key, value.newValues, value.oldValues);
- }
- });
- })
- .then(() => undefined);
+ // Exclude RAM-only keys to prevent them from being saved to storage
+ const defaultKeyValuePairs = Object.entries(
+ Object.keys(defaultKeyStates)
+ .filter((key) => !keysToPreserve.some((preserveKey) => OnyxKeys.isKeyMatch(preserveKey, key)) && !OnyxKeys.isRamOnlyKey(key))
+ .reduce((obj: KeyValueMapping, key) => {
+ // eslint-disable-next-line no-param-reassign
+ obj[key] = defaultKeyStates[key];
+ return obj;
+ }, {}),
+ );
+
+ // Remove only the items that we want cleared from storage, and reset others to default
+ for (const key of keysToBeClearedFromStorage) cache.drop(key);
+ return Storage.removeItems(keysToBeClearedFromStorage)
+ .then(() => Storage.multiSet(defaultKeyValuePairs))
+ .then(() => {
+ DevTools.clearState(keysToPreserve);
+
+ // Notify the subscribers for each key/value group so they can receive the new values
+ for (const [key, value] of Object.entries(keyValuesToResetIndividually)) {
+ OnyxUtils.notifyKey(key, value);
+ }
+ for (const [key, value] of Object.entries(keyValuesToResetAsCollection)) {
+ OnyxUtils.notifyCollection(key, value.newValues, value.oldValues);
+ }
+ });
+ })
+ .then(() => undefined);
- return cache.captureTask(TASK.CLEAR, promise) as Promise;
- });
+ return cache.captureTask(TASK.CLEAR, promise) as Promise;
+ }),
+ );
}
/**
@@ -451,147 +543,150 @@ function clear(keysToPreserve: OnyxKey[] = []): Promise {
* @returns resolves when all operations are complete
*/
function update(data: Array>): Promise {
- return OnyxUtils.afterInit(() => {
- // The queue of operations within a single `update` call in the format of .
- // This allows us to batch the operations per item and merge them into one operation in the order they were requested.
- const updateQueue: Record>> = {};
- const enqueueSetOperation = (key: OnyxKey, value: OnyxValue) => {
- // If a `set` operation is enqueued, we should clear the whole queue.
- // Since the `set` operation replaces the value entirely, there's no need to perform any previous operations.
- // To do this, we first put `null` in the queue, which removes the existing value, and then merge the new value.
- updateQueue[key] = [null, value];
- };
- const enqueueMergeOperation = (key: OnyxKey, value: OnyxValue) => {
- if (value === null) {
- // If we merge `null`, the value is removed and all the previous operations are discarded.
- updateQueue[key] = [null];
- } else if (!updateQueue[key]) {
- updateQueue[key] = [value];
- } else {
- updateQueue[key].push(value);
- }
- };
+ return OnyxUtils.trackPendingWrite(
+ data.map((updateItem) => updateItem.key),
+ OnyxUtils.afterInit(() => {
+ // The queue of operations within a single `update` call in the format of .
+ // This allows us to batch the operations per item and merge them into one operation in the order they were requested.
+ const updateQueue: Record>> = {};
+ const enqueueSetOperation = (key: OnyxKey, value: OnyxValue) => {
+ // If a `set` operation is enqueued, we should clear the whole queue.
+ // Since the `set` operation replaces the value entirely, there's no need to perform any previous operations.
+ // To do this, we first put `null` in the queue, which removes the existing value, and then merge the new value.
+ updateQueue[key] = [null, value];
+ };
+ const enqueueMergeOperation = (key: OnyxKey, value: OnyxValue) => {
+ if (value === null) {
+ // If we merge `null`, the value is removed and all the previous operations are discarded.
+ updateQueue[key] = [null];
+ } else if (!updateQueue[key]) {
+ updateQueue[key] = [value];
+ } else {
+ updateQueue[key].push(value);
+ }
+ };
- const promises: Array<() => Promise> = [];
- let clearPromise: Promise = Promise.resolve();
+ const promises: Array<() => Promise> = [];
+ let clearPromise: Promise = Promise.resolve();
- const onyxMethods = Object.values(OnyxUtils.METHOD);
- for (const {onyxMethod, key, value} of data) {
- if (!onyxMethods.includes(onyxMethod)) {
- Logger.logInfo(`Invalid onyxMethod ${onyxMethod} in Onyx update. Skipping this operation.`);
- continue;
- }
- if (onyxMethod !== OnyxUtils.METHOD.CLEAR && onyxMethod !== OnyxUtils.METHOD.MULTI_SET && typeof key !== 'string') {
- Logger.logInfo(`Invalid ${typeof key} key provided in Onyx update. Key must be of type string. Skipping this operation.`);
- continue;
- }
-
- const handlers: Record void> = {
- [OnyxUtils.METHOD.SET]: enqueueSetOperation,
- [OnyxUtils.METHOD.MERGE]: enqueueMergeOperation,
- [OnyxUtils.METHOD.MERGE_COLLECTION]: () => {
- const collection = value as OnyxMergeCollectionInput;
- if (!OnyxUtils.isValidNonEmptyCollectionForMerge(collection)) {
- Logger.logInfo('Invalid or empty value provided in Onyx mergeCollection. Skipping this operation.');
- return;
- }
+ const onyxMethods = Object.values(OnyxUtils.METHOD);
+ for (const {onyxMethod, key, value} of data) {
+ if (!onyxMethods.includes(onyxMethod)) {
+ Logger.logInfo(`Invalid onyxMethod ${onyxMethod} in Onyx update. Skipping this operation.`);
+ continue;
+ }
+ if (onyxMethod !== OnyxUtils.METHOD.CLEAR && onyxMethod !== OnyxUtils.METHOD.MULTI_SET && typeof key !== 'string') {
+ Logger.logInfo(`Invalid ${typeof key} key provided in Onyx update. Key must be of type string. Skipping this operation.`);
+ continue;
+ }
- // Confirm all the collection keys belong to the same parent
- const collectionKeys = Object.keys(collection);
- if (OnyxUtils.doAllCollectionItemsBelongToSameParent(key, collectionKeys)) {
- const mergedCollection: OnyxInputKeyValueMapping = collection;
- for (const collectionKey of collectionKeys) enqueueMergeOperation(collectionKey, mergedCollection[collectionKey]);
- }
- },
- [OnyxUtils.METHOD.SET_COLLECTION]: (k, v) => promises.push(() => setCollection(k as TKey, v as OnyxSetCollectionInput)),
- [OnyxUtils.METHOD.MULTI_SET]: (k, v) => {
- if (typeof value !== 'object' || Array.isArray(value) || typeof value === 'function') {
- Logger.logInfo(`Invalid value provided in Onyx multiSet. Value must be of type object. Skipping this operation.`);
- return;
- }
+ const handlers: Record void> = {
+ [OnyxUtils.METHOD.SET]: enqueueSetOperation,
+ [OnyxUtils.METHOD.MERGE]: enqueueMergeOperation,
+ [OnyxUtils.METHOD.MERGE_COLLECTION]: () => {
+ const collection = value as OnyxMergeCollectionInput;
+ if (!OnyxUtils.isValidNonEmptyCollectionForMerge(collection)) {
+ Logger.logInfo('Invalid or empty value provided in Onyx mergeCollection. Skipping this operation.');
+ return;
+ }
- for (const [entryKey, entryValue] of Object.entries(v as Partial)) enqueueSetOperation(entryKey, entryValue);
- },
- [OnyxUtils.METHOD.CLEAR]: () => {
- clearPromise = clear();
- },
- };
+ // Confirm all the collection keys belong to the same parent
+ const collectionKeys = Object.keys(collection);
+ if (OnyxUtils.doAllCollectionItemsBelongToSameParent(key, collectionKeys)) {
+ const mergedCollection: OnyxInputKeyValueMapping = collection;
+ for (const collectionKey of collectionKeys) enqueueMergeOperation(collectionKey, mergedCollection[collectionKey]);
+ }
+ },
+ [OnyxUtils.METHOD.SET_COLLECTION]: (k, v) => promises.push(() => setCollection(k as TKey, v as OnyxSetCollectionInput)),
+ [OnyxUtils.METHOD.MULTI_SET]: (k, v) => {
+ if (typeof value !== 'object' || Array.isArray(value) || typeof value === 'function') {
+ Logger.logInfo(`Invalid value provided in Onyx multiSet. Value must be of type object. Skipping this operation.`);
+ return;
+ }
- handlers[onyxMethod](key, value);
- }
+ for (const [entryKey, entryValue] of Object.entries(v as Partial)) enqueueSetOperation(entryKey, entryValue);
+ },
+ [OnyxUtils.METHOD.CLEAR]: () => {
+ clearPromise = clear();
+ },
+ };
- // Group all the collection-related keys and update each collection in a single `mergeCollection` call.
- // This is needed to prevent multiple `mergeCollection` calls for the same collection and `merge` calls for the individual items of the said collection.
- // This way, we ensure there is no race condition in the queued updates of the same key.
- for (const collectionKey of OnyxKeys.getCollectionKeys()) {
- const collectionItemKeys = Object.keys(updateQueue).filter((key) => OnyxKeys.isKeyMatch(collectionKey, key));
- if (collectionItemKeys.length <= 1) {
- // If there are no items of this collection in the updateQueue, we should skip it.
- // If there is only one item, we should update it individually, therefore retain it in the updateQueue.
- continue;
+ handlers[onyxMethod](key, value);
}
- const batchedCollectionUpdates = collectionItemKeys.reduce(
- (queue: MixedOperationsQueue, key: string) => {
- const operations = updateQueue[key];
+ // Group all the collection-related keys and update each collection in a single `mergeCollection` call.
+ // This is needed to prevent multiple `mergeCollection` calls for the same collection and `merge` calls for the individual items of the said collection.
+ // This way, we ensure there is no race condition in the queued updates of the same key.
+ for (const collectionKey of OnyxKeys.getCollectionKeys()) {
+ const collectionItemKeys = Object.keys(updateQueue).filter((key) => OnyxKeys.isKeyMatch(collectionKey, key));
+ if (collectionItemKeys.length <= 1) {
+ // If there are no items of this collection in the updateQueue, we should skip it.
+ // If there is only one item, we should update it individually, therefore retain it in the updateQueue.
+ continue;
+ }
- // Remove the collection-related key from the updateQueue so that it won't be processed individually.
- delete updateQueue[key];
+ const batchedCollectionUpdates = collectionItemKeys.reduce(
+ (queue: MixedOperationsQueue, key: string) => {
+ const operations = updateQueue[key];
- const batchedChanges = OnyxUtils.mergeAndMarkChanges(operations);
- if (operations[0] === null) {
- // eslint-disable-next-line no-param-reassign
- queue.set[key] = batchedChanges.result;
- } else {
- // eslint-disable-next-line no-param-reassign
- queue.merge[key] = batchedChanges.result;
- if (batchedChanges.replaceNullPatches.length > 0) {
+ // Remove the collection-related key from the updateQueue so that it won't be processed individually.
+ delete updateQueue[key];
+
+ const batchedChanges = OnyxUtils.mergeAndMarkChanges(operations);
+ if (operations[0] === null) {
+ // eslint-disable-next-line no-param-reassign
+ queue.set[key] = batchedChanges.result;
+ } else {
// eslint-disable-next-line no-param-reassign
- queue.mergeReplaceNullPatches[key] = batchedChanges.replaceNullPatches;
+ queue.merge[key] = batchedChanges.result;
+ if (batchedChanges.replaceNullPatches.length > 0) {
+ // eslint-disable-next-line no-param-reassign
+ queue.mergeReplaceNullPatches[key] = batchedChanges.replaceNullPatches;
+ }
}
- }
- return queue;
- },
- {
- merge: {},
- mergeReplaceNullPatches: {},
- set: {},
- },
- );
-
- if (!utils.isEmptyObject(batchedCollectionUpdates.merge)) {
- promises.push(() =>
- OnyxUtils.mergeCollectionWithPatches({
- collectionKey,
- collection: batchedCollectionUpdates.merge as OnyxMergeCollectionInput,
- mergeReplaceNullPatches: batchedCollectionUpdates.mergeReplaceNullPatches,
- }),
+ return queue;
+ },
+ {
+ merge: {},
+ mergeReplaceNullPatches: {},
+ set: {},
+ },
);
- }
- if (!utils.isEmptyObject(batchedCollectionUpdates.set)) {
- promises.push(() => OnyxUtils.partialSetCollection({collectionKey, collection: batchedCollectionUpdates.set as OnyxSetCollectionInput}));
- }
- }
- for (const [key, operations] of Object.entries(updateQueue)) {
- if (operations[0] === null) {
- const batchedChanges = OnyxUtils.mergeChanges(operations).result;
- promises.push(() => set(key, batchedChanges));
- continue;
+ if (!utils.isEmptyObject(batchedCollectionUpdates.merge)) {
+ promises.push(() =>
+ OnyxUtils.mergeCollectionWithPatches({
+ collectionKey,
+ collection: batchedCollectionUpdates.merge as OnyxMergeCollectionInput,
+ mergeReplaceNullPatches: batchedCollectionUpdates.mergeReplaceNullPatches,
+ }),
+ );
+ }
+ if (!utils.isEmptyObject(batchedCollectionUpdates.set)) {
+ promises.push(() => OnyxUtils.partialSetCollection({collectionKey, collection: batchedCollectionUpdates.set as OnyxSetCollectionInput}));
+ }
}
- for (const operation of operations) {
- promises.push(() => merge(key, operation));
+ for (const [key, operations] of Object.entries(updateQueue)) {
+ if (operations[0] === null) {
+ const batchedChanges = OnyxUtils.mergeChanges(operations).result;
+ promises.push(() => set(key, batchedChanges));
+ continue;
+ }
+
+ for (const operation of operations) {
+ promises.push(() => merge(key, operation));
+ }
}
- }
- const snapshotPromises = OnyxUtils.updateSnapshots(data, merge);
+ const snapshotPromises = OnyxUtils.updateSnapshots(data, merge);
- // We need to run the snapshot updates before the other updates so the snapshot data can be updated before the loading state in the snapshot
- const finalPromises = snapshotPromises.concat(promises);
+ // We need to run the snapshot updates before the other updates so the snapshot data can be updated before the loading state in the snapshot
+ const finalPromises = snapshotPromises.concat(promises);
- return clearPromise.then(() => Promise.all(finalPromises.map((p) => p()))).then(() => undefined);
- });
+ return clearPromise.then(() => Promise.all(finalPromises.map((p) => p()))).then(() => undefined);
+ }),
+ );
}
/**
@@ -608,7 +703,10 @@ function update(data: Array>): Promise(collectionKey: TKey, collection: OnyxSetCollectionInput): Promise {
- return OnyxUtils.afterInit(() => OnyxUtils.setCollectionWithRetry({collectionKey, collection}));
+ return OnyxUtils.trackPendingWrite(
+ Object.keys(collection),
+ OnyxUtils.afterInit(() => OnyxUtils.setCollectionWithRetry({collectionKey, collection})),
+ );
}
const Onyx = {
@@ -628,4 +726,4 @@ const Onyx = {
};
export default Onyx;
-export type {OnyxUpdate, ConnectOptions, SetOptions};
+export type {OnyxUpdate, ConnectOptions, SetOptions, Connection};
diff --git a/lib/OnyxCache.ts b/lib/OnyxCache.ts
index baeabde9c..5cd0dc6a4 100644
--- a/lib/OnyxCache.ts
+++ b/lib/OnyxCache.ts
@@ -554,7 +554,7 @@ class OnyxCache {
* Returns a frozen snapshot with structural sharing — safe to return by reference.
* Lazily rebuilds the snapshot if the collection was modified since the last read.
*/
- getCollectionData(collectionKey: OnyxKey): Record> | undefined {
+ getCollectionData(collectionKey: OnyxKey): OnyxCollection | undefined {
if (this.dirtyCollections.has(collectionKey)) {
this.rebuildCollectionSnapshot(collectionKey);
this.dirtyCollections.delete(collectionKey);
@@ -562,20 +562,14 @@ class OnyxCache {
const snapshot = this.collectionSnapshots.get(collectionKey);
- // We never stored anything for this collection key.
+ // Returning `undefined` for this collection key means init hasn't seeded it yet (pre-load), so there's
+ // nothing to return. `setCollectionKeys()` (called inside `Onyx.init`) seeds every
+ // known collection with a frozen empty entry, so the presence of an entry is the reliable
+ // post-init "loaded" signal.
if (snapshot === undefined) {
return undefined;
}
- // The collection is empty (it holds our shared empty object). But "empty" is ambiguous
- // during startup: we can't tell an actually-empty collection apart from one whose data
- // hasn't loaded yet. Once any key exists, we know setAllKeys has run and loaded everything,
- // so an empty collection really is empty. Before that, return undefined so subscribers
- // don't briefly see a collection as empty when it just hasn't loaded.
- if (snapshot === FROZEN_EMPTY_COLLECTION) {
- return this.storageKeys.size > 0 ? FROZEN_EMPTY_COLLECTION : undefined;
- }
-
return snapshot;
}
}
diff --git a/lib/OnyxConnectionManager.ts b/lib/OnyxConnectionManager.ts
deleted file mode 100644
index ddf7ff00f..000000000
--- a/lib/OnyxConnectionManager.ts
+++ /dev/null
@@ -1,262 +0,0 @@
-import bindAll from 'lodash.bindall';
-import * as Logger from './Logger';
-import type {ConnectOptions} from './Onyx';
-import OnyxUtils from './OnyxUtils';
-import OnyxKeys from './OnyxKeys';
-import * as Str from './Str';
-import type {CollectionConnectCallback, DefaultConnectCallback, OnyxKey, OnyxValue} from './types';
-import onyxSnapshotCache from './OnyxSnapshotCache';
-
-type ConnectCallback = DefaultConnectCallback | CollectionConnectCallback;
-
-/**
- * Represents the connection's metadata that contains the necessary properties
- * to handle that connection.
- */
-type ConnectionMetadata = {
- /**
- * The subscription ID returned by `OnyxUtils.subscribeToKey()` that is associated to this connection.
- */
- subscriptionID: number;
-
- /**
- * The Onyx key associated to this connection.
- */
- onyxKey: OnyxKey;
-
- /**
- * Whether the first connection's callback was fired or not.
- */
- isConnectionMade: boolean;
-
- /**
- * A map of the subscriber's callbacks associated to this connection.
- */
- callbacks: Map;
-
- /**
- * The last callback value returned by `OnyxUtils.subscribeToKey()`'s callback.
- */
- cachedCallbackValue?: OnyxValue;
-
- /**
- * The last callback key returned by `OnyxUtils.subscribeToKey()`'s callback.
- */
- cachedCallbackKey?: OnyxKey;
-};
-
-/**
- * Represents the connection object returned by `Onyx.connect()`.
- */
-type Connection = {
- /**
- * The ID used to identify this particular connection.
- */
- id: string;
-
- /**
- * The ID of the subscriber's callback that is associated to this connection.
- */
- callbackID: string;
-};
-
-/**
- * Manages Onyx connections of `Onyx.connect()` and `useOnyx()` subscribers.
- */
-class OnyxConnectionManager {
- /**
- * A map where the key is the connection ID generated inside `connect()` and the value is the metadata of that connection.
- */
- private connectionsMap: Map;
-
- /**
- * Stores the last generated callback ID which will be incremented when making a new connection.
- */
- private lastCallbackID: number;
-
- /**
- * Stores the last generated session ID for the connection manager. The current session ID
- * is appended to the connection IDs and it's used to create new different connections for the same key
- * when `refreshSessionID()` is called.
- *
- * When calling `Onyx.clear()` after a logout operation some connections might remain active as they
- * aren't tied to the React's lifecycle e.g. `Onyx.connect()` usage, causing infinite loading state issues to new `useOnyx()` subscribers
- * that are connecting to the same key as we didn't populate the cache again because we are still reusing such connections.
- *
- * To elimitate this problem, the session ID must be refreshed during the `Onyx.clear()` call (by using `refreshSessionID()`)
- * in order to create fresh connections when new subscribers connect to the same keys again, allowing them
- * to use the cache system correctly and avoid the mentioned issues in `useOnyx()`.
- */
- private sessionID: string;
-
- constructor() {
- this.connectionsMap = new Map();
- this.lastCallbackID = 0;
- this.sessionID = Str.guid();
-
- // Binds all public methods to prevent problems with `this`.
- bindAll(this, 'generateConnectionID', 'fireCallbacks', 'connect', 'disconnect', 'disconnectAll', 'refreshSessionID');
- }
-
- /**
- * Generates a connection ID based on the `connectOptions` object passed to the function.
- *
- * The properties used to generate the ID are handpicked for performance reasons and
- * according to their purpose and effect they produce in the Onyx connection.
- */
- private generateConnectionID(connectOptions: ConnectOptions): string {
- const {key, reuseConnection} = connectOptions;
-
- // The current session ID is appended to the connection ID so we can have different connections
- // after an `Onyx.clear()` operation.
- let suffix = `,sessionID=${this.sessionID}`;
-
- // We will generate a unique ID when `reuseConnection` is `false`, which means the subscriber
- // explicitly wants the connection to not be reused. Collection-root subscriptions are now always
- // snapshot mode, so they can be reused like any other connection.
- if (reuseConnection === false) {
- suffix += `,uniqueID=${Str.guid()}`;
- }
-
- return `onyxKey=${key}${suffix}`;
- }
-
- /**
- * Fires all the subscribers callbacks associated with that connection ID.
- */
- private fireCallbacks(connectionID: string): void {
- const connection = this.connectionsMap.get(connectionID);
- if (!connection) {
- return;
- }
-
- for (const callback of connection.callbacks.values()) {
- try {
- if (OnyxKeys.isCollectionKey(connection.onyxKey)) {
- (callback as CollectionConnectCallback)(connection.cachedCallbackValue as Record, connection.cachedCallbackKey as OnyxKey);
- } else {
- (callback as DefaultConnectCallback)(connection.cachedCallbackValue, connection.cachedCallbackKey as OnyxKey);
- }
- } catch (error) {
- Logger.logAlert(`[ConnectionManager] Subscriber callback threw an error for key '${connection.onyxKey}': ${String(error)}`);
- }
- }
- }
-
- /**
- * Connects to an Onyx key given the options passed and listens to its changes.
- *
- * @param connectOptions The options object that will define the behavior of the connection.
- * @returns The connection object to use when calling `disconnect()`.
- */
- connect(connectOptions: ConnectOptions): Connection {
- const connectionID = this.generateConnectionID(connectOptions);
- let connectionMetadata = this.connectionsMap.get(connectionID);
- let subscriptionID: number | undefined;
-
- const callbackID = String(this.lastCallbackID++);
-
- // If there is no connection yet for that connection ID, we create a new one.
- if (!connectionMetadata) {
- const callback: ConnectCallback = (value: OnyxValue, key: OnyxKey) => {
- const createdConnection = this.connectionsMap.get(connectionID);
- if (createdConnection) {
- // We signal that the first connection was made and now any new subscribers
- // can fire their callbacks immediately with the cached value when connecting.
- createdConnection.isConnectionMade = true;
- createdConnection.cachedCallbackValue = value;
- createdConnection.cachedCallbackKey = key;
- this.fireCallbacks(connectionID);
- }
- };
-
- subscriptionID = OnyxUtils.subscribeToKey({
- ...connectOptions,
- callback,
- } as ConnectOptions);
-
- connectionMetadata = {
- subscriptionID,
- onyxKey: connectOptions.key,
- isConnectionMade: false,
- callbacks: new Map(),
- };
-
- this.connectionsMap.set(connectionID, connectionMetadata);
- }
-
- // We add the subscriber's callback to the list of callbacks associated with this connection.
- if (connectOptions.callback) {
- connectionMetadata.callbacks.set(callbackID, connectOptions.callback as ConnectCallback);
- }
-
- // If the first connection is already made we want any new subscribers to receive the cached callback value immediately.
- if (connectionMetadata.isConnectionMade) {
- // Defer the callback execution to the next tick of the event loop.
- // This ensures that the current execution flow completes and the result connection object is available when the callback fires.
- Promise.resolve().then(() => {
- (connectOptions.callback as DefaultConnectCallback | undefined)?.(connectionMetadata.cachedCallbackValue, connectionMetadata.cachedCallbackKey as OnyxKey);
- });
- }
-
- return {id: connectionID, callbackID};
- }
-
- /**
- * Disconnects and removes the listener from the Onyx key.
- *
- * @param connection Connection object returned by calling `connect()`.
- */
- disconnect(connection: Connection): void {
- if (!connection) {
- Logger.logInfo(`[ConnectionManager] Attempted to disconnect passing an undefined connection object.`);
- return;
- }
-
- const connectionMetadata = this.connectionsMap.get(connection.id);
- if (!connectionMetadata) {
- Logger.logInfo(`[ConnectionManager] Attempted to disconnect but no connection was found.`);
- return;
- }
-
- // Removes the callback from the connection's callbacks map.
- connectionMetadata.callbacks.delete(connection.callbackID);
-
- // If the connection's callbacks map is empty we can safely unsubscribe from the Onyx key.
- if (connectionMetadata.callbacks.size === 0) {
- OnyxUtils.unsubscribeFromKey(connectionMetadata.subscriptionID);
-
- this.connectionsMap.delete(connection.id);
- }
- }
-
- /**
- * Disconnect all subscribers from Onyx.
- */
- disconnectAll(): void {
- for (const connectionMetadata of this.connectionsMap.values()) {
- OnyxUtils.unsubscribeFromKey(connectionMetadata.subscriptionID);
- }
-
- this.connectionsMap.clear();
-
- // Clear snapshot cache when all connections are disconnected
- onyxSnapshotCache.clear();
- }
-
- /**
- * Refreshes the connection manager's session ID.
- */
- refreshSessionID(): void {
- this.sessionID = Str.guid();
-
- // Clear snapshot cache when session refreshes to avoid stale cache issues
- onyxSnapshotCache.clear();
- }
-}
-
-const connectionManager = new OnyxConnectionManager();
-
-export default connectionManager;
-
-export type {Connection};
diff --git a/lib/OnyxSnapshotCache.ts b/lib/OnyxSnapshotCache.ts
deleted file mode 100644
index 70af8021e..000000000
--- a/lib/OnyxSnapshotCache.ts
+++ /dev/null
@@ -1,158 +0,0 @@
-import OnyxKeys from './OnyxKeys';
-import type {OnyxKey, OnyxValue} from './types';
-import type {UseOnyxOptions, UseOnyxResult, UseOnyxSelector} from './useOnyx';
-
-/**
- * Manages snapshot caching for useOnyx hook performance optimization.
- * Handles selector function tracking and memoized getSnapshot results.
- */
-class OnyxSnapshotCache {
- /**
- * Snapshot cache is a two-level map. The top-level keys are Onyx keys. The top-level values maps.
- * The second-level keys are a custom composite string defined by this.registerConsumer. These represent a unique useOnyx config, which is not fully represented by the Onyx key alone.
- * The reason we have two levels is for performance: not to make cache access faster, but to make cache invalidation faster.
- * We can invalidate the snapshot cache for a given Onyx key with one map.delete operation on the top-level map, rather than having to loop through a large single-level map and delete any matching keys.
- */
- private snapshotCache: Map>>>;
-
- /**
- * Maps selector functions to unique IDs for cache key generation
- */
- private selectorIDMap: WeakMap, number>;
-
- /**
- * Counter for generating incremental selector IDs
- */
- private selectorIDCounter: number;
-
- /**
- * Reference counting for cache keys to enable automatic cleanup.
- * Maps cache key (string) to number of consumers using it.
- */
- private cacheKeyRefCounts: Map;
-
- constructor() {
- this.snapshotCache = new Map();
- this.selectorIDMap = new WeakMap();
- this.selectorIDCounter = 0;
- this.cacheKeyRefCounts = new Map();
- }
-
- /**
- * Generate unique ID for selector functions using incrementing numbers
- */
- getSelectorID(selector: UseOnyxSelector): number {
- const typedSelector = selector as unknown as UseOnyxSelector;
- const existingID = this.selectorIDMap.get(typedSelector);
- if (existingID !== undefined) {
- return existingID;
- }
- const id = this.selectorIDCounter++;
- this.selectorIDMap.set(typedSelector, id);
- return id;
- }
-
- /**
- * Register a consumer for a cache key and return the cache key.
- * Generates cache key and increments reference counter.
- *
- * The properties used to generate the cache key are handpicked for performance reasons and
- * according to their purpose and effect they produce in the useOnyx hook behavior:
- *
- * - `selector`: Different selectors produce different results, so each selector needs its own cache entry
- *
- * Other options like `reuseConnection` don't affect the data transformation
- * or timing behavior of getSnapshot, so they're excluded from the cache key for better cache hit rates.
- */
- registerConsumer(key: TKey, options: Pick, 'selector'>): string {
- const selectorID = options?.selector ? this.getSelectorID(options.selector) : 'no_selector';
- const cacheKey = `${key}_${selectorID}`;
-
- // Increment reference count for this cache key
- const currentCount = this.cacheKeyRefCounts.get(cacheKey) ?? 0;
- this.cacheKeyRefCounts.set(cacheKey, currentCount + 1);
-
- return cacheKey;
- }
-
- /**
- * Deregister a consumer for a cache key.
- * Decrements reference counter and removes cache entry if no consumers remain.
- */
- deregisterConsumer(key: OnyxKey, cacheKey: string): void {
- const currentCount = this.cacheKeyRefCounts.get(cacheKey) ?? 0;
-
- if (currentCount <= 1) {
- // Last consumer - remove from reference counter and cache
- this.cacheKeyRefCounts.delete(cacheKey);
-
- // Remove from snapshot cache
- const keyCache = this.snapshotCache.get(key);
- if (keyCache) {
- keyCache.delete(cacheKey);
- // If this was the last cache entry for this Onyx key, remove the key entirely
- if (keyCache.size === 0) {
- this.snapshotCache.delete(key);
- }
- }
- } else {
- // Still has other consumers - just decrement count
- this.cacheKeyRefCounts.set(cacheKey, currentCount - 1);
- }
- }
-
- /**
- * Get cached snapshot result for a key and cache key combination
- */
- getCachedResult>>(key: OnyxKey, cacheKey: string): TResult | undefined {
- const keyCache = this.snapshotCache.get(key);
- return keyCache?.get(cacheKey) as TResult | undefined;
- }
-
- /**
- * Set cached snapshot result for a key and cache key combination
- */
- setCachedResult>>(key: OnyxKey, cacheKey: string, result: TResult): void {
- let keyCache = this.snapshotCache.get(key);
- if (!keyCache) {
- keyCache = new Map();
- this.snapshotCache.set(key, keyCache);
- }
- keyCache.set(cacheKey, result);
- }
-
- /**
- * Selective cache invalidation to prevent data unavailability
- * Collection members invalidate upward, collections don't cascade downward
- */
- invalidateForKey(keyToInvalidate: OnyxKey): void {
- // Always invalidate the exact key
- this.snapshotCache.delete(keyToInvalidate);
-
- // Check if the key is a collection member and invalidate the collection base key
- const collectionBaseKey = OnyxKeys.getCollectionKey(keyToInvalidate);
- if (collectionBaseKey) {
- this.snapshotCache.delete(collectionBaseKey);
- }
- }
-
- /**
- * Clear all snapshot cache
- */
- clear(): void {
- this.snapshotCache.clear();
- }
-
- /**
- * Clear selector ID mappings (useful for testing)
- */
- clearSelectorIds(): void {
- this.selectorIDCounter = 0;
- }
-}
-
-// Create and export a singleton instance
-const onyxSnapshotCache = new OnyxSnapshotCache();
-
-export default onyxSnapshotCache;
-export {OnyxSnapshotCache};
diff --git a/lib/OnyxUtils.ts b/lib/OnyxUtils.ts
index 519a1d8dc..53c3f7462 100644
--- a/lib/OnyxUtils.ts
+++ b/lib/OnyxUtils.ts
@@ -1,4 +1,3 @@
-import {shallowEqual} from 'fast-equals';
import type {ValueOf} from 'type-fest';
import _ from 'underscore';
import DevTools from './DevTools';
@@ -7,15 +6,13 @@ import type Onyx from './Onyx';
import cache, {TASK} from './OnyxCache';
import OnyxKeys from './OnyxKeys';
import StorageCircuitBreaker from './StorageCircuitBreaker';
+import onyxSubscriptionManager from './OnyxSubscriptionManager';
import Storage from './storage';
import {StorageErrorClass} from './storage/errors';
import type {
CollectionKeyBase,
- ConnectOptions,
DeepRecord,
- DefaultConnectCallback,
KeyValueMapping,
- CallbackToStateMapping,
MultiMergeReplaceNullPatches,
OnyxCollection,
OnyxEntry,
@@ -62,10 +59,6 @@ function resetDiskPressureLogThrottle(): void {
lastDiskPressureLogTime = 0;
}
-function formatCaughtError(error: unknown): string {
- return error instanceof Error ? error.toString() : String(error);
-}
-
type OnyxMethod = ValueOf;
/** Result of `prepareKeyValuePairsForStorage`: pairs to write and keys whose `null` value marks them for removal. */
@@ -78,26 +71,109 @@ type PreparedKeyValuePairs = {
let mergeQueue: Record>> = {};
let mergeQueuePromise: Record> = {};
-// Holds a mapping of all the React components that want their state subscribed to a store key
-let callbackToStateMapping: Record> = {};
+// In-flight writes tracked per affected key, so a subscriber's initial fire waits only for writes
+// that can change its own value, never for unrelated (or slow) writes elsewhere.
+const pendingWritesByKey = new Map>>();
-// Holds a mapping of the connected key to the subscriptionID for faster lookups
-let onyxKeyToSubscriptionIDs = new Map();
+// In-flight writes that affect every key (Onyx.clear). Any initial fire waits for these.
+const pendingGlobalWrites = new Set>();
// Optional user-provided key value states set when Onyx initializes or clears
let defaultKeyStates: Record> = {};
-// Used for comparison with a new update to avoid invoking the Onyx.connect callback with the same data.
-let lastConnectionCallbackData = new Map; matchedKey: OnyxKey | undefined}>();
-
let snapshotKey: OnyxKey | null = null;
-// Keeps track of the last subscriptionID that was used so we can keep incrementing it
-let lastSubscriptionID = 0;
-
// Connections can be made before `Onyx.init`. They would wait for this task before resolving
const deferredInitTask = createDeferredTask();
+/**
+ * Sentinel for "nothing delivered yet" in `connect()`'s per-subscription dedup. A Symbol
+ * can't collide with any real Onyx value, so the first `Object.is` check never matches and
+ * the initial fire runs even when a key's genuine first value is `undefined`. It only needs
+ * to be distinct from real values, not unique per subscription, so one module-level instance
+ * is reused by every connection.
+ */
+// eslint-disable-next-line rulesdir/no-negated-variables
+const NOT_DELIVERED = Symbol('NOT_DELIVERED');
+
+/**
+ * Registers an in-flight write under each key it can change, so `scheduleInitialFire` waits only for
+ * the writes relevant to a connecting key. Returns the same promise so callers can wrap a write's
+ * return value inline. The write is deregistered once it settles (success or failure).
+ */
+function trackPendingWrite(keys: OnyxKey | OnyxKey[], promise: Promise): Promise {
+ const keyList = Array.isArray(keys) ? keys : [keys];
+ for (const key of keyList) {
+ let set = pendingWritesByKey.get(key);
+ if (!set) {
+ set = new Set();
+ pendingWritesByKey.set(key, set);
+ }
+ set.add(promise);
+ }
+ const deregister = () => {
+ for (const key of keyList) {
+ const set = pendingWritesByKey.get(key);
+ if (!set) {
+ continue;
+ }
+ set.delete(promise);
+ if (set.size === 0) {
+ pendingWritesByKey.delete(key);
+ }
+ }
+ };
+ promise.then(deregister, deregister);
+ return promise;
+}
+
+/**
+ * Registers an in-flight write that affects every key (Onyx.clear). Deregistered once it settles.
+ */
+function trackPendingGlobalWrite(promise: Promise): Promise {
+ pendingGlobalWrites.add(promise);
+ const deregister = () => pendingGlobalWrites.delete(promise);
+ promise.then(deregister, deregister);
+ return promise;
+}
+
+/**
+ * In-flight writes that can change the value delivered to a subscriber of `key`: writes to the key
+ * itself, writes to any member when `key` is a collection root, and global writes (clear).
+ */
+function pendingWritesForKey(key: OnyxKey): Array> {
+ const promises = [...pendingGlobalWrites];
+ const own = pendingWritesByKey.get(key);
+ if (own) {
+ promises.push(...own);
+ }
+ if (OnyxKeys.isCollectionKey(key)) {
+ for (const [writeKey, set] of pendingWritesByKey) {
+ if (writeKey !== key && OnyxKeys.isCollectionMemberKey(key, writeKey)) {
+ promises.push(...set);
+ }
+ }
+ }
+ return promises;
+}
+
+/**
+ * Defer a `Onyx.connect` callback's initial fire until the writes relevant to `key` that are in
+ * flight this tick have applied, so it reads post-write cache and dedups against their notifications.
+ * The wait is scoped to `key` and snapshotted after one microtask, so an unrelated or slow write
+ * elsewhere cannot block or postpone this delivery, and writes issued after it do not either.
+ */
+function scheduleInitialFire(key: OnyxKey, fn: () => void): void {
+ Promise.resolve().then(() => {
+ const relevant = pendingWritesForKey(key);
+ if (relevant.length === 0) {
+ fn();
+ return;
+ }
+ Promise.all(relevant.map((promise) => promise.catch(() => undefined))).then(fn);
+ });
+}
+
// Collection member IDs that Onyx should silently ignore across all operations — reads, writes, cache, and subscriber
// notifications. This is used to filter out keys formed from invalid/default IDs (e.g. "-1", "0",
// "undefined", "null", "NaN") that can appear when an ID variable is accidentally coerced to string.
@@ -430,35 +506,6 @@ function tupleGet(keys: Keys): Promise<{[Index
}>;
}
-/**
- * Stores a subscription ID associated with a given key.
- *
- * @param subscriptionID - A subscription ID of the subscriber.
- * @param key - A key that the subscriber is subscribed to.
- */
-function storeKeyBySubscriptions(key: OnyxKey, subscriptionID: number) {
- if (!onyxKeyToSubscriptionIDs.has(key)) {
- onyxKeyToSubscriptionIDs.set(key, []);
- }
- onyxKeyToSubscriptionIDs.get(key).push(subscriptionID);
-}
-
-/**
- * Deletes a subscription ID associated with its corresponding key.
- *
- * @param subscriptionID - The subscription ID to be deleted.
- */
-function deleteKeyBySubscriptions(subscriptionID: number) {
- const subscriber = callbackToStateMapping[subscriptionID];
-
- if (subscriber && onyxKeyToSubscriptionIDs.has(subscriber.key)) {
- const updatedSubscriptionsIDs = onyxKeyToSubscriptionIDs.get(subscriber.key).filter((id: number) => id !== subscriptionID);
- onyxKeyToSubscriptionIDs.set(subscriber.key, updatedSubscriptionsIDs);
- }
-
- lastConnectionCallbackData.delete(subscriptionID);
-}
-
/** Returns current key names stored in persisted storage */
function getAllKeys(): Promise> {
// When we've already read stored keys, resolve right away
@@ -486,30 +533,6 @@ function getAllKeys(): Promise> {
return cache.captureTask(TASK.GET_ALL_KEYS, promise) as Promise>;
}
-/**
- * Tries to get a value from the cache. If the value is not present in cache it will return the default value or undefined.
- * If the requested key is a collection, it will return an object with all the collection members.
- */
-function tryGetCachedValue(key: TKey): OnyxValue {
- let val = cache.get(key);
-
- if (OnyxKeys.isCollectionKey(key)) {
- const collectionData = cache.getCollectionData(key);
- if (collectionData !== undefined) {
- val = collectionData;
- } else {
- // If we haven't loaded all keys yet, we can't determine if the collection exists
- if (cache.getAllKeys().size === 0) {
- return;
- }
- // Set an empty collection object for collections that exist but have no data
- val = {};
- }
- }
-
- return val;
-}
-
function getCachedCollection(collectionKey: TKey, collectionMemberKeys?: string[]): NonNullable> {
// Use optimized collection data retrieval when cache is populated
const collectionData = cache.getCollectionData(collectionKey);
@@ -557,207 +580,46 @@ function getCachedCollection(collectionKey: TKey
}
/**
- * When a collection of keys change, search for any callbacks matching the collection key and trigger those callbacks
- */
-function keysChanged(
- collectionKey: TKey,
- partialCollection: OnyxCollection,
- partialPreviousCollection: OnyxCollection | undefined,
-): void {
- const cachedCollection = getCachedCollection(collectionKey);
- const previousCollection = partialPreviousCollection ?? {};
- const changedMemberKeys = Object.keys(partialCollection ?? {});
-
- // Add or remove the keys from the recentlyAccessedKeys list
- for (const memberKey of changedMemberKeys) {
- const value = partialCollection?.[memberKey];
- if (value !== null && value !== undefined) {
- cache.addLastAccessedKey(memberKey, false);
- } else {
- cache.removeLastAccessedKey(memberKey);
- }
- }
-
- // Use indexed lookup instead of scanning all subscribers.
- // We need subscribers for: (1) the collection key itself, and (2) individual changed member keys.
- const collectionSubscriberIDs = onyxKeyToSubscriptionIDs.get(collectionKey) ?? [];
- const memberSubscriberIDs: number[] = [];
- for (const memberKey of changedMemberKeys) {
- const ids = onyxKeyToSubscriptionIDs.get(memberKey);
- if (ids) {
- for (const id of ids) {
- memberSubscriberIDs.push(id);
- }
- }
- }
-
- // Notify collection-level subscribers
- for (const subID of collectionSubscriberIDs) {
- const subscriber = callbackToStateMapping[subID];
- if (!subscriber || typeof subscriber.callback !== 'function') {
- continue;
- }
-
- try {
- lastConnectionCallbackData.set(subscriber.subscriptionID, {value: cachedCollection, matchedKey: subscriber.key});
- subscriber.callback(cachedCollection, subscriber.key);
- } catch (error) {
- Logger.logAlert(`[OnyxUtils.keysChanged] Subscriber callback threw an error for key '${collectionKey}': ${formatCaughtError(error)}`);
- }
- }
-
- // Notify member-level subscribers (e.g. subscribed to `report_123`)
- for (const subID of memberSubscriberIDs) {
- const subscriber = callbackToStateMapping[subID];
- if (!subscriber || typeof subscriber.callback !== 'function') {
- continue;
- }
-
- if (cachedCollection[subscriber.key] === previousCollection[subscriber.key]) {
- continue;
- }
-
- try {
- const subscriberCallback = subscriber.callback as DefaultConnectCallback;
- subscriberCallback(cachedCollection[subscriber.key], subscriber.key as TKey);
- lastConnectionCallbackData.set(subscriber.subscriptionID, {
- value: cachedCollection[subscriber.key],
- matchedKey: subscriber.key,
- });
- } catch (error) {
- Logger.logAlert(`[OnyxUtils.keysChanged] Subscriber callback threw an error for key '${collectionKey}': ${formatCaughtError(error)}`);
- }
- }
-}
-
-/**
- * When a key change happens, search for any callbacks matching the key or collection key and trigger those callbacks
+ * Notify subscribers of a single-key write. Wrapper over `onyxSubscriptionManager.notifyKey()`
+ * that also performs LRU bookkeeping for eviction.
*/
-function keyChanged(key: TKey, value: OnyxValue, canUpdateSubscriber: (subscriber?: CallbackToStateMapping) => boolean = () => true): void {
- // Add or remove this key from the recentlyAccessedKeys list
+function notifyKey(key: TKey, value: OnyxValue): void {
if (value !== null && value !== undefined) {
cache.addLastAccessedKey(key, OnyxKeys.isCollectionKey(key));
} else {
cache.removeLastAccessedKey(key);
}
-
- // We get the subscribers interested in the key that has just changed. If the subscriber's key is a collection key then we will
- // notify them if the key that changed is a collection member. Or if it is a regular key notify them when there is an exact match.
- // Given the amount of times this function is called we need to make sure we are not iterating over all subscribers every time. On the other hand, we don't need to
- // do the same in keysChanged, because we only call that function when a collection key changes, and it doesn't happen that often.
- // For performance reason, we look for the given key and later if don't find it we look for the collection key, instead of checking if it is a collection key first.
- let stateMappingKeys = onyxKeyToSubscriptionIDs.get(key) ?? [];
- const collectionKey = OnyxKeys.getCollectionKey(key);
-
- if (collectionKey) {
- // Getting the collection key from the specific key because only collection keys were stored in the mapping.
- stateMappingKeys = [...stateMappingKeys, ...(onyxKeyToSubscriptionIDs.get(collectionKey) ?? [])];
- if (stateMappingKeys.length === 0) {
- return;
- }
- }
-
- // Cache the collection snapshot per dispatch so all subscribers to the same collection
- // see a consistent view, even if an earlier subscriber's callback synchronously writes
- // to the same collection.
- const cachedCollections: Record> = {};
-
- for (const stateMappingKey of stateMappingKeys) {
- const subscriber = callbackToStateMapping[stateMappingKey];
- if (!subscriber || !OnyxKeys.isKeyMatch(subscriber.key, key) || !canUpdateSubscriber(subscriber)) {
- continue;
- }
-
- // Subscriber is a regular call to connect() and provided a callback
- if (typeof subscriber.callback === 'function') {
- try {
- const lastData = lastConnectionCallbackData.get(subscriber.subscriptionID);
- if (lastData && lastData.matchedKey === key && lastData.value === value) {
- continue;
- }
-
- if (OnyxKeys.isCollectionKey(subscriber.key)) {
- // Cache once per dispatch to ensure all subscribers see a consistent snapshot
- // even if a previous callback synchronously wrote to the same collection.
- let cachedCollection = cachedCollections[subscriber.key];
- if (!cachedCollection) {
- cachedCollection = getCachedCollection(subscriber.key);
- cachedCollections[subscriber.key] = cachedCollection;
- }
- lastConnectionCallbackData.set(subscriber.subscriptionID, {value: cachedCollection, matchedKey: subscriber.key});
- subscriber.callback(cachedCollection, subscriber.key);
- continue;
- }
-
- const subscriberCallback = subscriber.callback as DefaultConnectCallback;
- subscriberCallback(value, key);
-
- lastConnectionCallbackData.set(subscriber.subscriptionID, {
- value,
- matchedKey: key,
- });
- continue;
- } catch (error) {
- Logger.logAlert(`[OnyxUtils.keyChanged] Subscriber callback threw an error for key '${key}': ${formatCaughtError(error)}`);
- }
-
- continue;
- }
-
- console.error('Warning: Found a matching subscriber to a key that changed, but no callback could be found.');
- }
+ onyxSubscriptionManager.notifyKey(key, value);
}
/**
- * Sends the data obtained from the keys to the connection.
+ * Notify subscribers of a batch collection update. Wrapper over
+ * `onyxSubscriptionManager.notifyCollection()` that also performs LRU bookkeeping per
+ * changed member.
*/
-function sendDataToConnection(mapping: CallbackToStateMapping, matchedKey: TKey | undefined): void {
- // If the mapping no longer exists then we should not send any data.
- // This means our subscriber was disconnected.
- if (!callbackToStateMapping[mapping.subscriptionID]) {
- return;
- }
-
- // Always read the latest value from cache to avoid stale or duplicate data.
- // For collection-root subscribers, read the full collection.
- // For individual key subscribers, read just that key's value.
- let value: OnyxValue | undefined;
- if (OnyxKeys.isCollectionKey(mapping.key)) {
- const collection = getCachedCollection(mapping.key);
- value = Object.keys(collection).length > 0 ? (collection as OnyxValue) : undefined;
- } else {
- value = cache.get(matchedKey ?? mapping.key) as OnyxValue;
- }
-
- // For regular callbacks, we never want to pass null values, but always just undefined if a value is not set in cache or storage.
- value = value === null ? undefined : value;
- const lastData = lastConnectionCallbackData.get(mapping.subscriptionID);
-
- // If the value has not changed for the same key we do not need to trigger the callback.
- // We compare matchedKey to avoid suppressing callbacks for different collection members
- // that happen to have shallow-equal values (e.g. during hydration racing with set()).
- if (lastData && lastData.matchedKey === matchedKey && shallowEqual(lastData.value, value)) {
- return;
+function notifyCollection(
+ collectionKey: TKey,
+ partialCollection: OnyxCollection,
+ partialPreviousCollection?: OnyxCollection,
+): void {
+ const changedKeys = Object.keys(partialCollection ?? {});
+ for (const memberKey of changedKeys) {
+ const value = partialCollection?.[memberKey];
+ if (value !== null && value !== undefined) {
+ cache.addLastAccessedKey(memberKey, false);
+ } else {
+ cache.removeLastAccessedKey(memberKey);
+ }
}
-
- (mapping.callback as DefaultConnectCallback | undefined)?.(value, matchedKey as TKey);
+ onyxSubscriptionManager.notifyCollection(collectionKey, partialCollection, partialPreviousCollection);
}
/**
- * Gets the data for a given an array of matching keys, combines them into an object, and sends the result back to the subscriber.
- */
-function getCollectionDataAndSendAsObject(matchingKeys: CollectionKeyBase[], mapping: CallbackToStateMapping): void {
- multiGet(matchingKeys).then(() => {
- sendDataToConnection(mapping, mapping.key);
- });
-}
-
-/**
- * Remove a key from Onyx and update the subscribers
+ * Remove a key from Onyx and update the subscribers.
*/
function remove(key: TKey): Promise {
cache.drop(key);
- keyChanged(key, undefined as OnyxValue);
+ notifyKey(key, undefined as OnyxValue);
if (OnyxKeys.isRamOnlyKey(key)) {
return Promise.resolve();
@@ -909,7 +771,7 @@ function broadcastUpdate(key: TKey, value: OnyxValue
}
cache.set(key, value);
- keyChanged(key, value);
+ notifyKey(key, value);
}
function hasPendingMergeForKey(key: OnyxKey): boolean {
@@ -1069,7 +931,7 @@ function initializeWithDefaultKeyStates(): Promise {
// Notify subscribers about default key states so that any subscriber that connected
// before init (e.g. during module load) receives the merged default values immediately
for (const [key, value] of Object.entries(merged ?? {})) {
- keyChanged(key, value);
+ notifyKey(key, value);
}
})
.catch((error) => {
@@ -1087,7 +949,7 @@ function initializeWithDefaultKeyStates(): Promise {
// Notify subscribers about default key states so that any subscriber that connected
// before init (e.g. during module load) receives the merged default values immediately
for (const [key, value] of Object.entries(defaultKeyStates)) {
- keyChanged(key, value);
+ notifyKey(key, value);
}
});
}
@@ -1120,108 +982,6 @@ function doAllCollectionItemsBelongToSameParent(
return !hasCollectionKeyCheckFailed;
}
-/**
- * Subscribes to an Onyx key and listens to its changes.
- *
- * @param connectOptions The options object that will define the behavior of the connection.
- * @returns The subscription ID to use when calling `OnyxUtils.unsubscribeFromKey()`.
- */
-function subscribeToKey(connectOptions: ConnectOptions): number {
- const mapping = connectOptions as CallbackToStateMapping;
- const subscriptionID = lastSubscriptionID++;
- callbackToStateMapping[subscriptionID] = mapping as CallbackToStateMapping;
- callbackToStateMapping[subscriptionID].subscriptionID = subscriptionID;
-
- // When keyChanged is called, a key is passed and the method looks through all the Subscribers in callbackToStateMapping for the matching key to get the subscriptionID
- // to avoid having to loop through all the Subscribers all the time (even when just one connection belongs to one key),
- // We create a mapping from key to lists of subscriptionIDs to access the specific list of subscriptionIDs.
- storeKeyBySubscriptions(mapping.key, callbackToStateMapping[subscriptionID].subscriptionID);
-
- // Commit connection only after init passes
- deferredInitTask.promise
- // This first .then() adds a microtask tick for compatibility reasons and
- // to ensure subscribers don't receive an extra initial callback before Onyx.update() data arrives.
- .then(() => undefined)
- .then(() => {
- // Performance improvement
- // If the mapping is connected to an onyx key that is not a collection
- // we can skip the call to getAllKeys() and return an array with a single item
- if (!!mapping.key && typeof mapping.key === 'string' && !OnyxKeys.isCollectionKey(mapping.key) && cache.getAllKeys().has(mapping.key)) {
- return new Set([mapping.key]);
- }
- return getAllKeys();
- })
- .then((keys) => {
- // We search all the keys in storage to see if any are a "match" for the subscriber we are connecting so that we
- // can send data back to the subscriber. Note that multiple keys can match as a subscriber could either be
- // subscribed to a "collection key" or a single key.
- const matchingKeys: string[] = [];
-
- // Performance optimization: For single key subscriptions, avoid O(n) iteration
- if (!OnyxKeys.isCollectionKey(mapping.key)) {
- if (keys.has(mapping.key)) {
- matchingKeys.push(mapping.key);
- }
- } else {
- // Collection case - need to iterate through all keys to find matches (O(n))
- for (const key of keys) {
- if (!OnyxKeys.isKeyMatch(mapping.key, key)) {
- continue;
- }
- matchingKeys.push(key);
- }
- }
- // If the key being connected to does not exist we initialize the value with null. For subscribers that connected
- // directly via connect() they will simply get a null value sent to them without any information about which key matched
- // since there are none matched.
- if (matchingKeys.length === 0) {
- if (mapping.key) {
- cache.addNullishStorageKey(mapping.key);
- }
-
- const matchedKey = OnyxKeys.isCollectionKey(mapping.key) ? mapping.key : undefined;
-
- // Here we cannot use batching because the nullish value is expected to be set immediately for default props
- // or they will be undefined.
- sendDataToConnection(mapping, matchedKey);
- return;
- }
-
- // When using a callback subscriber, a subscription to a collection key combines all matching
- // member values into a single object and makes one call with the whole collection object.
- if (typeof mapping.callback === 'function') {
- if (OnyxKeys.isCollectionKey(mapping.key)) {
- getCollectionDataAndSendAsObject(matchingKeys, mapping);
- return;
- }
-
- // If we are not subscribed to a collection key then there's only a single key to send an update for.
- get(mapping.key).then(() => sendDataToConnection(mapping, mapping.key));
- return;
- }
-
- console.error('Warning: Onyx.connect() was found without a callback');
- });
-
- // The subscriptionID is returned back to the caller so that it can be used to clean up the connection when it's no longer needed
- // by calling OnyxUtils.unsubscribeFromKey(subscriptionID).
- return subscriptionID;
-}
-
-/**
- * Disconnects and removes the listener from the Onyx key.
- *
- * @param subscriptionID Subscription ID returned by calling `OnyxUtils.subscribeToKey()`.
- */
-function unsubscribeFromKey(subscriptionID: number): void {
- if (!callbackToStateMapping[subscriptionID]) {
- return;
- }
-
- deleteKeyBySubscriptions(subscriptionID);
- delete callbackToStateMapping[subscriptionID];
-}
-
function updateSnapshots(data: Array>, mergeFn: typeof Onyx.merge): Array<() => Promise> {
const snapshotCollectionKey = getSnapshotKey();
if (!snapshotCollectionKey) return [];
@@ -1435,9 +1195,9 @@ function multiSetWithRetry(data: OnyxMultiSetInput, retryAttempt?: number): Prom
const keysToRemove = removalCandidates.filter((key) => cache.get(key) !== undefined || persistedKeys.size === 0 || persistedKeys.has(key));
// Group collection members by their parent collection key so each collection can be notified
- // via a single batched keysChanged() call instead of one keyChanged() per member. For each
+ // via a single batched notifyCollection() call instead of one notifyKey() per member. For each
// collection, `partial` holds the new values being set and `previous` holds the cached values
- // from before the set, which keysChanged() uses to skip subscribers whose value didn't change.
+ // from before the set, which notifyCollection() uses to skip subscribers whose value didn't change.
const collectionBatches = new Map<
string,
{
@@ -1455,7 +1215,7 @@ function multiSetWithRetry(data: OnyxMultiSetInput, retryAttempt?: number): Prom
const collectionKey = OnyxKeys.getCollectionKey(key);
if (collectionKey && OnyxKeys.isCollectionMemberKey(collectionKey, key)) {
- // Capture the previous cached value BEFORE calling cache.set() so keysChanged()
+ // Capture the previous cached value before calling cache.set() so notifyCollection()
// can diff old vs new per-member.
const previousValue = cache.get(key);
cache.set(key, value);
@@ -1468,14 +1228,13 @@ function multiSetWithRetry(data: OnyxMultiSetInput, retryAttempt?: number): Prom
batch.partial[key] = value;
batch.previous[key] = previousValue;
} else {
- // Non-collection keys are notified inline (cache.set + keyChanged in iteration order)
+ // Non-collection keys are notified inline (cache.set + notifyKey in iteration order)
// so re-entrant callbacks (e.g. Onyx.set inside a callback) see consistent cache
// and subscriber state, matching the original per-key notification semantics.
cache.set(key, value);
// Skip subscriber notification on retry — already notified on attempt 0.
- // Collection-root subscribers re-fire on every keyChanged by contract.
if (!retryAttempt) {
- keyChanged(key, value);
+ notifyKey(key, value);
}
}
}
@@ -1497,16 +1256,16 @@ function multiSetWithRetry(data: OnyxMultiSetInput, retryAttempt?: number): Prom
batch.previous[key] = previousValue;
} else if (!retryAttempt) {
// Skip subscriber notification on retry — already notified on attempt 0.
- keyChanged(key, undefined);
+ notifyKey(key, undefined);
}
}
- // One keysChanged() per collection — fires each collection-level subscriber once and lets
- // keysChanged() internally decide which individual member subscribers need notification.
+ // One notifyCollection() per collection: fires each collection-level subscriber once and lets
+ // notifyCollection() internally decide which individual member subscribers need notification.
// Skip on retry — already notified on attempt 0 (see same-reason comment above).
if (!retryAttempt) {
for (const [collectionKey, batch] of collectionBatches) {
- keysChanged(collectionKey as CollectionKeyBase, batch.partial, batch.previous);
+ notifyCollection(collectionKey as CollectionKeyBase, batch.partial, batch.previous);
}
}
@@ -1589,18 +1348,17 @@ function setCollectionWithRetry({collectionKey,
const {pairs: keyValuePairs, keysToRemove: removalCandidates} = OnyxUtils.prepareKeyValuePairsForStorage(mutableCollection, true);
// Removals of keys that are neither cached nor persisted are no-ops and skipped.
const keysToRemove = removalCandidates.filter((key) => cache.get(key) !== undefined || persistedKeys.has(key));
- // Snapshot before cache mutations so keysChanged() can diff removed members.
+ // Capture the previous collection before cache mutations so notifyCollection() can diff removed members.
const previousCollection = OnyxUtils.getCachedCollection(collectionKey);
for (const [key, value] of keyValuePairs) cache.set(key, value);
for (const key of keysToRemove) cache.drop(key);
// Skip subscriber notification on retry — already notified on attempt 0.
- // Collection-root subscribers re-fire on every keysChanged by contract.
if (!retryAttempt) {
// Removed members are notified as undefined, matching mergeCollection/multiSet.
const partialForNotify = Object.fromEntries(Object.entries(mutableCollection).map(([key, value]) => [key, value ?? undefined]));
- keysChanged(collectionKey, partialForNotify, previousCollection);
+ notifyCollection(collectionKey, partialForNotify, previousCollection);
}
// RAM-only keys are not supposed to be saved to storage
@@ -1758,13 +1516,13 @@ function mergeCollectionWithPatches(
// promise-chain depth; slow path batches the misses into one Storage.multiGet.
const hasColdExistingKey = existingKeys.some((key) => !cache.hasCacheForKey(key));
// Swallow pre-warm read failures so a transient Storage.multiGet rejection doesn't
- // skip the cache.merge() + keysChanged() below. Subscribers still see the merge even
+ // skip the cache.merge() + notifyCollection() below. Subscribers still see the merge even
// when storage reads fail.
const prewarmPromise = hasColdExistingKey
? multiGet(existingKeys).catch((err) => Logger.logInfo(`mergeCollectionWithPatches pre-warm failed; proceeding with cache-only merge. Error: ${err}`))
: Promise.resolve();
return prewarmPromise.then(() => {
- // Snapshot previous values from the (now-warm) cache for keysChanged's diff, then update
+ // Capture previous values from the (now-warm) cache for the subscriber diff, then update
// cache and notify subscribers synchronously BEFORE issuing storage writes. This matches
// the cache-first / storage-second invariant followed by every other Onyx write method
// (setWithRetry, applyMerge, setCollectionWithRetry, partialSetCollection, clear),
@@ -1774,12 +1532,11 @@ function mergeCollectionWithPatches(
cache.merge(finalMergedCollection);
// Skip subscriber notification on retry — already notified on attempt 0.
- // Collection-root subscribers re-fire on every keysChanged by contract.
if (!retryAttempt) {
const partialForNotify = keysToRemove.length > 0 ? {...finalMergedCollection, ...Object.fromEntries(keysToRemove.map((key) => [key, undefined]))} : finalMergedCollection;
const previousForNotify = keysToRemove.length > 0 ? {...previousCollection, ...removedPreviousValues} : previousCollection;
if (Object.keys(partialForNotify).length > 0) {
- keysChanged(collectionKey, partialForNotify, previousForNotify);
+ notifyCollection(collectionKey, partialForNotify, previousForNotify);
}
}
@@ -1871,18 +1628,17 @@ function partialSetCollection({collectionKey, co
const {pairs: keyValuePairs, keysToRemove: removalCandidates} = prepareKeyValuePairsForStorage(mutableCollection, true);
// Removals of keys that are neither cached nor persisted are no-ops and skipped.
const keysToRemove = removalCandidates.filter((key) => cache.get(key) !== undefined || persistedKeys.has(key));
- // Snapshot before cache mutations so keysChanged() can diff removed members.
+ // Capture the previous collection before cache mutations so notifyCollection() can diff removed members.
const previousCollection = getCachedCollection(collectionKey, existingKeys);
for (const [key, value] of keyValuePairs) cache.set(key, value);
for (const key of keysToRemove) cache.drop(key);
// Skip subscriber notification on retry — already notified on attempt 0.
- // Collection-root subscribers re-fire on every keysChanged by contract.
if (!retryAttempt) {
// Removed members are notified as undefined, matching mergeCollection/multiSet.
const partialForNotify = Object.fromEntries(Object.entries(mutableCollection).map(([key, value]) => [key, value ?? undefined]));
- keysChanged(collectionKey, partialForNotify, previousCollection);
+ notifyCollection(collectionKey, partialForNotify, previousCollection);
}
if (OnyxKeys.isRamOnlyKey(collectionKey)) {
@@ -1922,13 +1678,16 @@ function logKeyRemoved(onyxMethod: Extract, key: On
function clearOnyxUtilsInternals() {
mergeQueue = {};
mergeQueuePromise = {};
- callbackToStateMapping = {};
- onyxKeyToSubscriptionIDs = new Map();
- lastConnectionCallbackData = new Map();
+ pendingWritesByKey.clear();
+ pendingGlobalWrites.clear();
}
const OnyxUtils = {
METHOD,
+ NOT_DELIVERED,
+ scheduleInitialFire,
+ trackPendingWrite,
+ trackPendingGlobalWrite,
getMergeQueue,
getMergeQueuePromise,
getDefaultKeyStates,
@@ -1938,12 +1697,9 @@ const OnyxUtils = {
sendActionToDevTools,
get,
getAllKeys,
- tryGetCachedValue,
getCachedCollection,
- keysChanged,
- keyChanged,
- sendDataToConnection,
- getCollectionDataAndSendAsObject,
+ notifyKey,
+ notifyCollection,
remove,
reportStorageQuota,
resetDiskPressureLogThrottle,
@@ -1959,14 +1715,10 @@ const OnyxUtils = {
tupleGet,
isValidNonEmptyCollectionForMerge,
doAllCollectionItemsBelongToSameParent,
- subscribeToKey,
- unsubscribeFromKey,
getSkippableCollectionMemberIDs,
setSkippableCollectionMemberIDs,
getSnapshotMergeKeys,
setSnapshotMergeKeys,
- storeKeyBySubscriptions,
- deleteKeyBySubscriptions,
reduceCollectionWithSelector,
updateSnapshots,
mergeCollectionWithPatches,
diff --git a/lib/createMemoizedSelector.ts b/lib/createMemoizedSelector.ts
deleted file mode 100644
index 3dbdafbff..000000000
--- a/lib/createMemoizedSelector.ts
+++ /dev/null
@@ -1,38 +0,0 @@
-import {deepEqual} from 'fast-equals';
-
-/**
- * Wraps a selector function so that:
- * - Calling the wrapper with the same input reference twice short-circuits to the cached output
- * (cheap `===` check, no recompute).
- * - Calling with a different input that produces a deep-equal output returns the *previous*
- * output reference, so downstream `===` comparisons treat it as unchanged.
- *
- * This is the minimum needed for `useSyncExternalStore` to not loop when consumers pass
- * inline selectors that allocate fresh objects on every call (e.g. `(e) => ({id: e?.id})`):
- * without the deep-equal fallback, every `getSnapshot` would return a new reference and React
- * would re-render (or throw "getSnapshot should be cached") indefinitely.
- *
- * Stateful by design — each call to `createMemoizedSelector` produces an independent wrapper
- * with its own `lastInput`/`lastOutput` cache, so a wrapper must not be shared across
- * subscriptions that can see different inputs.
- */
-function createMemoizedSelector(selector: (input: TInput) => TOutput): (input: TInput) => TOutput {
- let lastInput: TInput;
- let lastOutput: TOutput;
- let hasComputed = false;
-
- return (input) => {
- if (hasComputed && lastInput === input) {
- return lastOutput;
- }
- const next = selector(input);
- lastInput = input;
- if (!hasComputed || !deepEqual(lastOutput, next)) {
- lastOutput = next;
- hasComputed = true;
- }
- return lastOutput;
- };
-}
-
-export default createMemoizedSelector;
diff --git a/lib/index.ts b/lib/index.ts
index bb6df0e0c..671b28a70 100644
--- a/lib/index.ts
+++ b/lib/index.ts
@@ -1,4 +1,4 @@
-import type {ConnectOptions, OnyxUpdate} from './Onyx';
+import type {Connection, ConnectOptions, OnyxUpdate} from './Onyx';
import Onyx from './Onyx';
import type {
CustomTypeOptions,
@@ -19,7 +19,6 @@ import type {
OnyxSetCollectionInput,
} from './types';
import type {FetchStatus, ResultMetadata, UseOnyxResult, UseOnyxOptions} from './useOnyx';
-import type {Connection} from './OnyxConnectionManager';
import useOnyx from './useOnyx';
import type {OnyxSQLiteKeyValuePair} from './storage/providers/SQLiteProvider';
diff --git a/lib/memoizedShallowEqual.ts b/lib/memoizedShallowEqual.ts
deleted file mode 100644
index 4a015f3d9..000000000
--- a/lib/memoizedShallowEqual.ts
+++ /dev/null
@@ -1,41 +0,0 @@
-import {shallowEqual} from 'fast-equals';
-
-/**
- * Memoizes shallowEqual verdicts by the identity of the compared objects. Onyx values are
- * treated as immutable (merge/set replace objects, never mutate), so a (prev, next) reference
- * pair always yields the same verdict. In the hot case — N no-selector hooks on the same big
- * key — every hook compares the exact same two cache-owned objects, so the first hook pays for
- * the O(keys) walk and the rest resolve in O(1). WeakMap keys make stale entries impossible to
- * read (lookup requires holding both exact objects) and let GC reclaim them.
- */
-const shallowEqualVerdicts = new WeakMap