diff --git a/lib/Onyx.ts b/lib/Onyx.ts index 7d46d5a8..4f78a634 100644 --- a/lib/Onyx.ts +++ b/lib/Onyx.ts @@ -254,9 +254,8 @@ function merge(key: TKey, changes: OnyxMergeInput): return Promise.resolve(); } - return OnyxMerge.applyMerge(key, existingValue, validChanges).then(({mergedValue, updatePromise}) => { + return OnyxMerge.applyMerge(key, existingValue, validChanges).then(({mergedValue}) => { OnyxUtils.sendActionToDevTools(OnyxUtils.METHOD.MERGE, key, changes, mergedValue); - return updatePromise; }); } catch (error) { Logger.logAlert(`An error occurred while applying merge for key: ${key}, Error: ${error}`); @@ -368,16 +367,6 @@ function clear(keysToPreserve: OnyxKey[] = []): Promise { keysToBeClearedFromStorage.push(key); } - const updatePromises: Array> = []; - - // Notify the subscribers for each key/value group so they can receive the new values - for (const [key, value] of Object.entries(keyValuesToResetIndividually)) { - updatePromises.push(OnyxUtils.scheduleSubscriberUpdate(key, value)); - } - for (const [key, value] of Object.entries(keyValuesToResetAsCollection)) { - updatePromises.push(OnyxUtils.scheduleNotifyCollectionSubscribers(key, value.newValues, value.oldValues)); - } - // Exclude RAM-only keys to prevent them from being saved to storage const defaultKeyValuePairs = Object.entries( Object.keys(defaultKeyStates) @@ -396,7 +385,14 @@ function clear(keysToPreserve: OnyxKey[] = []): Promise { .then(() => Storage.multiSet(defaultKeyValuePairs)) .then(() => { DevTools.clearState(keysToPreserve); - return Promise.all(updatePromises); + + // 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); diff --git a/lib/OnyxMerge/index.native.ts b/lib/OnyxMerge/index.native.ts index ec8c242e..5e56bf49 100644 --- a/lib/OnyxMerge/index.native.ts +++ b/lib/OnyxMerge/index.native.ts @@ -26,21 +26,20 @@ const applyMerge: ApplyMerge = , hasChanged); + OnyxUtils.broadcastUpdate(key, mergedValue as OnyxValue, hasChanged); const shouldSkipStorageOperations = !hasChanged || OnyxUtils.isRamOnlyKey(key); // If the value has not changed, calling Storage.setItem() would be redundant and a waste of performance, so return early instead. // If the key is marked as RAM-only, it should not be saved nor updated in the storage. if (shouldSkipStorageOperations) { - return Promise.resolve({mergedValue, updatePromise}); + return Promise.resolve({mergedValue}); } // For native platforms we use `mergeItem` that will take advantage of JSON_PATCH and JSON_REPLACE SQL operations to // merge the object in a performant way. return Storage.mergeItem(key, batchedChanges as OnyxValue, replaceNullPatches).then(() => ({ mergedValue, - updatePromise, })); }; diff --git a/lib/OnyxMerge/index.ts b/lib/OnyxMerge/index.ts index 7eac789c..ef92293d 100644 --- a/lib/OnyxMerge/index.ts +++ b/lib/OnyxMerge/index.ts @@ -18,20 +18,19 @@ const applyMerge: ApplyMerge = , hasChanged); + OnyxUtils.broadcastUpdate(key, mergedValue as OnyxValue, hasChanged); const shouldSkipStorageOperations = !hasChanged || OnyxUtils.isRamOnlyKey(key); // If the value has not changed, calling Storage.setItem() would be redundant and a waste of performance, so return early instead. // If the key is marked as RAM-only, it should not be saved nor updated in the storage. if (shouldSkipStorageOperations) { - return Promise.resolve({mergedValue, updatePromise}); + return Promise.resolve({mergedValue}); } // For web platforms we use `setItem` since the object was already merged with its changes before. return Storage.setItem(key, mergedValue as OnyxValue).then(() => ({ mergedValue, - updatePromise, })); }; diff --git a/lib/OnyxMerge/types.ts b/lib/OnyxMerge/types.ts index c59b7892..e53d8ff3 100644 --- a/lib/OnyxMerge/types.ts +++ b/lib/OnyxMerge/types.ts @@ -2,7 +2,6 @@ import type {OnyxInput, OnyxKey} from '../types'; type ApplyMergeResult = { mergedValue: TValue; - updatePromise: Promise; }; type ApplyMerge = | undefined, TChange extends OnyxInput | null>( diff --git a/lib/OnyxUtils.ts b/lib/OnyxUtils.ts index eca0cc90..dd08e6be 100644 --- a/lib/OnyxUtils.ts +++ b/lib/OnyxUtils.ts @@ -74,9 +74,6 @@ type OnyxMethod = ValueOf; let mergeQueue: Record>> = {}; let mergeQueuePromise: Record> = {}; -// Used to schedule subscriber update to the macro task queue -let nextMacrotaskPromise: Promise | null = null; - // Holds a mapping of all the React components that want their state subscribed to a store key let callbackToStateMapping: Record> = {}; @@ -690,6 +687,7 @@ function keysChanged( // send the whole cached collection. if (isSubscribedToCollectionKey) { if (subscriber.waitForCollectionCallback) { + lastConnectionCallbackData.set(subscriber.subscriptionID, cachedCollection); subscriber.callback(cachedCollection, subscriber.key, partialCollection); continue; } @@ -704,6 +702,7 @@ function keysChanged( subscriber.callback(cachedCollection[dataKey], dataKey); } + lastConnectionCallbackData.set(subscriber.subscriptionID, cachedCollection); continue; } @@ -788,6 +787,7 @@ function keyChanged( } cachedCollection[key] = value; + lastConnectionCallbackData.set(subscriber.subscriptionID, cachedCollection); subscriber.callback(cachedCollection, subscriber.key, {[key]: value}); continue; } @@ -815,11 +815,10 @@ function sendDataToConnection(mapping: CallbackToStateMapp // For regular callbacks, we never want to pass null values, but always just undefined if a value is not set in cache or storage. const valueToPass = value === null ? undefined : value; - const lastValue = lastConnectionCallbackData.get(mapping.subscriptionID); - lastConnectionCallbackData.get(mapping.subscriptionID); - // If the value has not changed we do not need to trigger the callback - if (lastConnectionCallbackData.has(mapping.subscriptionID) && valueToPass === lastValue) { + // If the subscriber was already notified (e.g. by a synchronous keyChanged call), + // skip the initial data delivery to prevent duplicate callbacks. + if (lastConnectionCallbackData.has(mapping.subscriptionID)) { return; } @@ -852,57 +851,12 @@ function getCollectionDataAndSendAsObject(matchingKeys: Co }); } -/** - * Delays promise resolution until the next macrotask to prevent race condition if the key subscription is in progress. - * - * @param callback The keyChanged/keysChanged callback - * */ -function prepareSubscriberUpdate(callback: () => void): Promise { - if (!nextMacrotaskPromise) { - nextMacrotaskPromise = new Promise((resolve) => { - setTimeout(() => { - nextMacrotaskPromise = null; - resolve(); - }, 0); - }); - } - return Promise.all([nextMacrotaskPromise, Promise.resolve().then(callback)]).then(); -} - -/** - * Schedules an update that will be appended to the macro task queue (so it doesn't update the subscribers immediately). - * - * @example - * scheduleSubscriberUpdate(key, value, subscriber => subscriber.initWithStoredValues === false) - */ -function scheduleSubscriberUpdate( - key: TKey, - value: OnyxValue, - canUpdateSubscriber: (subscriber?: CallbackToStateMapping) => boolean = () => true, - isProcessingCollectionUpdate = false, -): Promise { - return prepareSubscriberUpdate(() => keyChanged(key, value, canUpdateSubscriber, isProcessingCollectionUpdate)); -} - -/** - * This method is similar to scheduleSubscriberUpdate but it is built for working specifically with collections - * so that keysChanged() is triggered for the collection and not keyChanged(). If this was not done, then the - * subscriber callbacks receive the data in a different format than they normally expect and it breaks code. - */ -function scheduleNotifyCollectionSubscribers( - key: TKey, - value: OnyxCollection, - previousValue?: OnyxCollection, -): Promise { - return prepareSubscriberUpdate(() => keysChanged(key, value, previousValue)); -} - /** * Remove a key from Onyx and update the subscribers */ function remove(key: TKey, isProcessingCollectionUpdate?: boolean): Promise { cache.drop(key); - scheduleSubscriberUpdate(key, undefined as OnyxValue, undefined, isProcessingCollectionUpdate); + keyChanged(key, undefined as OnyxValue, undefined, isProcessingCollectionUpdate); if (isRamOnlyKey(key)) { return Promise.resolve(); @@ -973,7 +927,7 @@ function retryOperation(error: Error, on /** * Notifies subscribers and writes current value to cache */ -function broadcastUpdate(key: TKey, value: OnyxValue, hasChanged?: boolean): Promise { +function broadcastUpdate(key: TKey, value: OnyxValue, hasChanged?: boolean): void { // Update subscribers if the cached value has changed, or when the subscriber specifically requires // all updates regardless of value changes (indicated by initWithStoredValues set to false). if (hasChanged) { @@ -982,7 +936,7 @@ function broadcastUpdate(key: TKey, value: OnyxValue cache.addToAccessedKeys(key); } - return scheduleSubscriberUpdate(key, value, (subscriber) => hasChanged || subscriber?.initWithStoredValues === false).then(() => undefined); + keyChanged(key, value, (subscriber) => hasChanged || subscriber?.initWithStoredValues === false); } function hasPendingMergeForKey(key: OnyxKey): boolean { @@ -1376,24 +1330,23 @@ function setWithRetry({key, value, options}: SetParams OnyxUtils.retryOperation(error, setWithRetry, {key, value: valueWithoutNestedNullValues, options}, retryAttempt)) .then(() => { OnyxUtils.sendActionToDevTools(OnyxUtils.METHOD.SET, key, valueWithoutNestedNullValues); - return updatePromise; }); } @@ -1427,7 +1380,7 @@ function multiSetWithRetry(data: OnyxMultiSetInput, retryAttempt?: number): Prom const keyValuePairsToSet = OnyxUtils.prepareKeyValuePairsForStorage(newData, true); - const updatePromises = keyValuePairsToSet.map(([key, value]) => { + for (const [key, value] of keyValuePairsToSet) { // When we use multiSet to set a key we want to clear the current delta changes from Onyx.merge that were queued // before the value was set. If Onyx.merge is currently reading the old value from storage, it will then not apply the changes. if (OnyxUtils.hasPendingMergeForKey(key)) { @@ -1436,8 +1389,8 @@ function multiSetWithRetry(data: OnyxMultiSetInput, retryAttempt?: number): Prom // Update cache and optimistically inform subscribers on the next tick cache.set(key, value); - return OnyxUtils.scheduleSubscriberUpdate(key, value); - }); + keyChanged(key, value); + } const keyValuePairsToStore = keyValuePairsToSet.filter((keyValuePair) => { const [key] = keyValuePair; @@ -1449,9 +1402,7 @@ function multiSetWithRetry(data: OnyxMultiSetInput, retryAttempt?: number): Prom .catch((error) => OnyxUtils.retryOperation(error, multiSetWithRetry, newData, retryAttempt)) .then(() => { OnyxUtils.sendActionToDevTools(OnyxUtils.METHOD.MULTI_SET, undefined, newData); - return Promise.all(updatePromises); - }) - .then(() => undefined); + }); } /** @@ -1512,19 +1463,18 @@ function setCollectionWithRetry({collectionKey, for (const [key, value] of keyValuePairs) cache.set(key, value); - const updatePromise = OnyxUtils.scheduleNotifyCollectionSubscribers(collectionKey, mutableCollection, previousCollection); + keysChanged(collectionKey, mutableCollection, previousCollection); // RAM-only keys are not supposed to be saved to storage if (isRamOnlyKey(collectionKey)) { OnyxUtils.sendActionToDevTools(OnyxUtils.METHOD.SET_COLLECTION, undefined, mutableCollection); - return updatePromise; + return; } return Storage.multiSet(keyValuePairs) .catch((error) => OnyxUtils.retryOperation(error, setCollectionWithRetry, {collectionKey, collection}, retryAttempt)) .then(() => { OnyxUtils.sendActionToDevTools(OnyxUtils.METHOD.SET_COLLECTION, undefined, mutableCollection); - return updatePromise; }); }); } @@ -1647,7 +1597,7 @@ function mergeCollectionWithPatches( // and update all subscribers const promiseUpdate = previousCollectionPromise.then((previousCollection) => { cache.merge(finalMergedCollection); - return scheduleNotifyCollectionSubscribers(collectionKey, finalMergedCollection, previousCollection); + keysChanged(collectionKey, finalMergedCollection, previousCollection); }); return Promise.all(promises) @@ -1713,18 +1663,17 @@ function partialSetCollection({collectionKey, co for (const [key, value] of keyValuePairs) cache.set(key, value); - const updatePromise = scheduleNotifyCollectionSubscribers(collectionKey, mutableCollection, previousCollection); + keysChanged(collectionKey, mutableCollection, previousCollection); if (isRamOnlyKey(collectionKey)) { sendActionToDevTools(METHOD.SET_COLLECTION, undefined, mutableCollection); - return updatePromise; + return; } return Storage.multiSet(keyValuePairs) .catch((error) => retryOperation(error, partialSetCollection, {collectionKey, collection}, retryAttempt)) .then(() => { sendActionToDevTools(METHOD.SET_COLLECTION, undefined, mutableCollection); - return updatePromise; }); }); } @@ -1772,8 +1721,6 @@ const OnyxUtils = { sendDataToConnection, getCollectionKey, getCollectionDataAndSendAsObject, - scheduleSubscriberUpdate, - scheduleNotifyCollectionSubscribers, remove, reportStorageQuota, retryOperation, @@ -1830,10 +1777,6 @@ GlobalSettings.addGlobalSettingsChangeListener(({enablePerformanceMetrics}) => { // @ts-expect-error Reassign sendDataToConnection = decorateWithMetrics(sendDataToConnection, 'OnyxUtils.sendDataToConnection'); // @ts-expect-error Reassign - scheduleSubscriberUpdate = decorateWithMetrics(scheduleSubscriberUpdate, 'OnyxUtils.scheduleSubscriberUpdate'); - // @ts-expect-error Reassign - scheduleNotifyCollectionSubscribers = decorateWithMetrics(scheduleNotifyCollectionSubscribers, 'OnyxUtils.scheduleNotifyCollectionSubscribers'); - // @ts-expect-error Reassign remove = decorateWithMetrics(remove, 'OnyxUtils.remove'); // @ts-expect-error Reassign reportStorageQuota = decorateWithMetrics(reportStorageQuota, 'OnyxUtils.reportStorageQuota'); diff --git a/tests/perf-test/OnyxUtils.perf-test.ts b/tests/perf-test/OnyxUtils.perf-test.ts index 5a00d910..fdc1d107 100644 --- a/tests/perf-test/OnyxUtils.perf-test.ts +++ b/tests/perf-test/OnyxUtils.perf-test.ts @@ -431,66 +431,6 @@ describe('OnyxUtils', () => { }); }); - describe('scheduleSubscriberUpdate', () => { - test('10k calls scheduling updates', async () => { - const subscriptionMap = new Map(); - - const changedReportActions = Object.fromEntries( - Object.entries(mockedReportActionsMap).map(([k, v]) => [k, createRandomReportAction(Number(v.reportActionID))] as const), - ) as GenericCollection; - - await measureAsyncFunction(() => Promise.all(Object.entries(changedReportActions).map(([key, value]) => OnyxUtils.scheduleSubscriberUpdate(key, value))), { - beforeEach: async () => { - await Onyx.multiSet(mockedReportActionsMap); - for (const key of mockedReportActionsKeys) { - const id = OnyxUtils.subscribeToKey({key, callback: jest.fn(), initWithStoredValues: false}); - subscriptionMap.set(key, id); - } - }, - afterEach: async () => { - for (const key of mockedReportActionsKeys) { - const id = subscriptionMap.get(key); - if (id) { - OnyxUtils.unsubscribeFromKey(id); - } - } - subscriptionMap.clear(); - await clearOnyxAfterEachMeasure(); - }, - }); - }); - }); - - describe('scheduleNotifyCollectionSubscribers', () => { - test('one call with 10k heavy objects to update 10k subscribers', async () => { - const subscriptionMap = new Map(); - - const changedReportActions = Object.fromEntries( - Object.entries(mockedReportActionsMap).map(([k, v]) => [k, createRandomReportAction(Number(v.reportActionID))] as const), - ) as GenericCollection; - - await measureAsyncFunction(() => OnyxUtils.scheduleNotifyCollectionSubscribers(collectionKey, changedReportActions, mockedReportActionsMap), { - beforeEach: async () => { - await Onyx.multiSet(mockedReportActionsMap); - for (const key of mockedReportActionsKeys) { - const id = OnyxUtils.subscribeToKey({key, callback: jest.fn(), initWithStoredValues: false}); - subscriptionMap.set(key, id); - } - }, - afterEach: async () => { - for (const key of mockedReportActionsKeys) { - const id = subscriptionMap.get(key); - if (id) { - OnyxUtils.unsubscribeFromKey(id); - } - } - subscriptionMap.clear(); - await clearOnyxAfterEachMeasure(); - }, - }); - }); - }); - describe('remove', () => { test('10k calls', async () => { await measureAsyncFunction(() => Promise.all(mockedReportActionsKeys.map((key) => OnyxUtils.remove(key))), { @@ -534,7 +474,7 @@ describe('OnyxUtils', () => { const reportAction = mockedReportActionsMap[`${collectionKey}0`]; const changedReportAction = createRandomReportAction(Number(reportAction.reportActionID)); - await measureAsyncFunction(() => OnyxUtils.broadcastUpdate(key, changedReportAction, true), { + await measureFunction(() => OnyxUtils.broadcastUpdate(key, changedReportAction, true), { beforeEach: async () => { await Onyx.set(key, reportAction); }, diff --git a/tests/unit/onyxTest.ts b/tests/unit/onyxTest.ts index f66b0dc7..64caec57 100644 --- a/tests/unit/onyxTest.ts +++ b/tests/unit/onyxTest.ts @@ -1482,10 +1482,9 @@ describe('Onyx', () => { return waitForPromisesToResolve(); }) .then(() => { - expect(collectionCallback).toHaveBeenCalledTimes(3); + expect(collectionCallback).toHaveBeenCalledTimes(2); expect(collectionCallback).toHaveBeenNthCalledWith(1, {[cat]: initialValue}, ONYX_KEYS.COLLECTION.ANIMALS, {[cat]: initialValue}); - expect(collectionCallback).toHaveBeenNthCalledWith(2, {[cat]: initialValue}, ONYX_KEYS.COLLECTION.ANIMALS, undefined); - expect(collectionCallback).toHaveBeenNthCalledWith(3, collectionDiff, ONYX_KEYS.COLLECTION.ANIMALS, {[cat]: initialValue, [dog]: {name: 'Rex'}}); + expect(collectionCallback).toHaveBeenNthCalledWith(2, collectionDiff, ONYX_KEYS.COLLECTION.ANIMALS, {[cat]: initialValue, [dog]: {name: 'Rex'}}); // Cat hasn't changed from its original value, expect only the initial connect callback expect(catCallback).toHaveBeenCalledTimes(1);