From adb9a3220f28cf8d19513f3468c8a2585b04ae77 Mon Sep 17 00:00:00 2001 From: Nikki Wines Date: Fri, 22 Dec 2023 19:37:12 -0500 Subject: [PATCH 1/6] update reequest and onyxupdates to support finallyData --- src/libs/API.ts | 1 + src/libs/Middleware/SaveResponseInOnyx.ts | 2 +- src/libs/actions/OnyxUpdates.ts | 11 +++++++---- src/types/onyx/Request.ts | 1 + 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/libs/API.ts b/src/libs/API.ts index 91cf6a7db877..1cb5da2259ed 100644 --- a/src/libs/API.ts +++ b/src/libs/API.ts @@ -34,6 +34,7 @@ type OnyxData = { optimisticData?: OnyxUpdate[]; successData?: OnyxUpdate[]; failureData?: OnyxUpdate[]; + finallyData?: OnyxUpdate[]; }; type ApiRequestType = ValueOf; diff --git a/src/libs/Middleware/SaveResponseInOnyx.ts b/src/libs/Middleware/SaveResponseInOnyx.ts index d73a10d98663..041d6e089be5 100644 --- a/src/libs/Middleware/SaveResponseInOnyx.ts +++ b/src/libs/Middleware/SaveResponseInOnyx.ts @@ -14,7 +14,7 @@ const SaveResponseInOnyx: Middleware = (requestResponse, request) => // Sometimes we call requests that are successfull but they don't have any response or any success/failure data to set. Let's return early since // we don't need to store anything here. - if (!onyxUpdates && !request.successData && !request.failureData) { + if (!onyxUpdates && (!request.successData && !request.failureData || !request.finallyData)) { return Promise.resolve(response); } diff --git a/src/libs/actions/OnyxUpdates.ts b/src/libs/actions/OnyxUpdates.ts index bc407625dc6a..2bd4d262c9ba 100644 --- a/src/libs/actions/OnyxUpdates.ts +++ b/src/libs/actions/OnyxUpdates.ts @@ -34,11 +34,14 @@ function applyHTTPSOnyxUpdates(request: Request, response: Response) { return onyxDataUpdatePromise .then(() => { // Handle the request's success/failure data (client-side data) - if (response.jsonCode === 200 && request.successData) { - return updateHandler(request.successData); + const successData = request.successData?.length ? request.successData : request.finallyData; + if (response.jsonCode === 200 && successData) { + return updateHandler(successData); } - if (response.jsonCode !== 200 && request.failureData) { - return updateHandler(request.failureData); + + const failureData = request.failureData?.length ? request.failureData : request.finallyData; + if (response.jsonCode !== 200 && failureData) { + return updateHandler(failureData); } return Promise.resolve(); }) diff --git a/src/types/onyx/Request.ts b/src/types/onyx/Request.ts index 746e7f75b3d5..79de2b8ff967 100644 --- a/src/types/onyx/Request.ts +++ b/src/types/onyx/Request.ts @@ -4,6 +4,7 @@ import Response from './Response'; type OnyxData = { successData?: OnyxUpdate[]; failureData?: OnyxUpdate[]; + finallyData?: OnyxUpdate[]; optimisticData?: OnyxUpdate[]; }; From d70989557eae3852ced5574679c610d21fdc90fe Mon Sep 17 00:00:00 2001 From: Nikki Wines Date: Fri, 22 Dec 2023 19:37:38 -0500 Subject: [PATCH 2/6] update RequestNewValidateCode to use finallyData --- src/libs/actions/Session/index.ts | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index ca38e0dd5902..50edda0d9230 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -195,16 +195,7 @@ function resendValidateCode(login = credentials.login) { }, }, ]; - const successData: OnyxUpdate[] = [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.ACCOUNT, - value: { - loadingForm: null, - }, - }, - ]; - const failureData: OnyxUpdate[] = [ + const finallyData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.ACCOUNT, @@ -220,7 +211,7 @@ function resendValidateCode(login = credentials.login) { const params: RequestNewValidateCodeParams = {email: login}; - API.write('RequestNewValidateCode', params, {optimisticData, successData, failureData}); + API.write('RequestNewValidateCode', params, {optimisticData, finallyData}); } type OnyxData = { From 7617e93405914e63a6488a63e82aebee64abcdb0 Mon Sep 17 00:00:00 2001 From: Nikki Wines Date: Fri, 22 Dec 2023 21:29:30 -0500 Subject: [PATCH 3/6] update getOnyxDataForOpenOrReconnect to use finallyData --- src/libs/API.ts | 7 +++++-- src/libs/Middleware/SaveResponseInOnyx.ts | 2 +- src/libs/actions/App.ts | 23 ++++------------------- 3 files changed, 10 insertions(+), 22 deletions(-) diff --git a/src/libs/API.ts b/src/libs/API.ts index 1cb5da2259ed..26fefc790b3f 100644 --- a/src/libs/API.ts +++ b/src/libs/API.ts @@ -26,7 +26,7 @@ Request.use(Middleware.Reauthentication); // If an optimistic ID is not used by the server, this will update the remaining serialized requests using that optimistic ID to use the correct ID instead. Request.use(Middleware.HandleUnusedOptimisticID); -// SaveResponseInOnyx - Merges either the successData or failureData into Onyx depending on if the call was successful or not. This needs to be the LAST middleware we use, don't add any +// SaveResponseInOnyx - Merges either the successData or failureData (or finallyData, if included in place of the former two values) into Onyx depending on if the call was successful or not. This needs to be the LAST middleware we use, don't add any // middlewares after this, because the SequentialQueue depends on the result of this middleware to pause the queue (if needed) to bring the app to an up-to-date state. Request.use(Middleware.SaveResponseInOnyx); @@ -40,7 +40,7 @@ type OnyxData = { type ApiRequestType = ValueOf; /** - * All calls to API.write() will be persisted to disk as JSON with the params, successData, and failureData. + * All calls to API.write() will be persisted to disk as JSON with the params, successData, and failureData (or finallyData, if included in place of the former two values). * This is so that if the network is unavailable or the app is closed, we can send the WRITE request later. * * @param command - Name of API command to call. @@ -51,6 +51,7 @@ type ApiRequestType = ValueOf; * @param [onyxData.optimisticData] - Onyx instructions that will be passed to Onyx.update() before the request is made. * @param [onyxData.successData] - Onyx instructions that will be passed to Onyx.update() when the response has jsonCode === 200. * @param [onyxData.failureData] - Onyx instructions that will be passed to Onyx.update() when the response has jsonCode !== 200. + * @param [onyxData.finallyData] - Onyx instructions that will be passed to Onyx.update() when the response has jsonCode === 200 or jsonCode !== 200. */ function write(command: string, apiCommandParameters: Record = {}, onyxData: OnyxData = {}) { Log.info('Called API write', false, {command, ...apiCommandParameters}); @@ -105,6 +106,7 @@ function write(command: string, apiCommandParameters: Record = * @param [onyxData.optimisticData] - Onyx instructions that will be passed to Onyx.update() before the request is made. * @param [onyxData.successData] - Onyx instructions that will be passed to Onyx.update() when the response has jsonCode === 200. * @param [onyxData.failureData] - Onyx instructions that will be passed to Onyx.update() when the response has jsonCode !== 200. + * @param [onyxData.finallyData] - Onyx instructions that will be passed to Onyx.update() when the response has jsonCode === 200 or jsonCode !== 200. * @param [apiRequestType] - Can be either 'read', 'write', or 'makeRequestWithSideEffects'. We use this to either return the chained * response back to the caller or to trigger reconnection callbacks when re-authentication is required. * @returns @@ -152,6 +154,7 @@ function makeRequestWithSideEffects( * @param [onyxData.optimisticData] - Onyx instructions that will be passed to Onyx.update() before the request is made. * @param [onyxData.successData] - Onyx instructions that will be passed to Onyx.update() when the response has jsonCode === 200. * @param [onyxData.failureData] - Onyx instructions that will be passed to Onyx.update() when the response has jsonCode !== 200. + * @param [onyxData.finallyData] - Onyx instructions that will be passed to Onyx.update() when the response has jsonCode === 200 or jsonCode !== 200. */ function read(command: string, apiCommandParameters: Record, onyxData: OnyxData = {}) { // Ensure all write requests on the sequential queue have finished responding before running read requests. diff --git a/src/libs/Middleware/SaveResponseInOnyx.ts b/src/libs/Middleware/SaveResponseInOnyx.ts index 041d6e089be5..9f8a3a56bcd1 100644 --- a/src/libs/Middleware/SaveResponseInOnyx.ts +++ b/src/libs/Middleware/SaveResponseInOnyx.ts @@ -14,7 +14,7 @@ const SaveResponseInOnyx: Middleware = (requestResponse, request) => // Sometimes we call requests that are successfull but they don't have any response or any success/failure data to set. Let's return early since // we don't need to store anything here. - if (!onyxUpdates && (!request.successData && !request.failureData || !request.finallyData)) { + if (!onyxUpdates && ((!request.successData && !request.failureData) || !request.finallyData)) { return Promise.resolve(response); } diff --git a/src/libs/actions/App.ts b/src/libs/actions/App.ts index ec43d4358134..daccb40d61ed 100644 --- a/src/libs/actions/App.ts +++ b/src/libs/actions/App.ts @@ -155,7 +155,7 @@ function getPolicyParamsForOpenOrReconnect(): Promise = { + const defaultData = { optimisticData: [ { onyxMethod: Onyx.METHOD.MERGE, @@ -163,14 +163,7 @@ function getOnyxDataForOpenOrReconnect(isOpenApp = false): OnyxData { value: true, }, ], - successData: [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.IS_LOADING_REPORT_DATA, - value: false, - }, - ], - failureData: [ + finallyData: [ { onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.IS_LOADING_REPORT_DATA, @@ -190,16 +183,8 @@ function getOnyxDataForOpenOrReconnect(isOpenApp = false): OnyxData { value: true, }, ], - successData: [ - ...defaultData.successData, - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.IS_LOADING_APP, - value: false, - }, - ], - failureData: [ - ...defaultData.failureData, + finallyData: [ + ...defaultData.finallyData, { onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.IS_LOADING_APP, From 68951152e65459364c7a4ac1ad030618f1736d5d Mon Sep 17 00:00:00 2001 From: Nikki Wines Date: Wed, 3 Jan 2024 18:38:50 -0500 Subject: [PATCH 4/6] update early return condition --- src/libs/Middleware/SaveResponseInOnyx.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/Middleware/SaveResponseInOnyx.ts b/src/libs/Middleware/SaveResponseInOnyx.ts index 9f8a3a56bcd1..76e05a28f7bf 100644 --- a/src/libs/Middleware/SaveResponseInOnyx.ts +++ b/src/libs/Middleware/SaveResponseInOnyx.ts @@ -12,9 +12,9 @@ const SaveResponseInOnyx: Middleware = (requestResponse, request) => requestResponse.then((response = {}) => { const onyxUpdates = response?.onyxData ?? []; - // Sometimes we call requests that are successfull but they don't have any response or any success/failure data to set. Let's return early since + // Sometimes we call requests that are successfull but they don't have any response or any success/failure/finally data to set. Let's return early since // we don't need to store anything here. - if (!onyxUpdates && ((!request.successData && !request.failureData) || !request.finallyData)) { + if (!onyxUpdates && !request.successData && !request.failureData && !request.finallyData) { return Promise.resolve(response); } From eccc1f04d6b888e9689566182d5c73fb44d7b179 Mon Sep 17 00:00:00 2001 From: Nikki Wines Date: Thu, 4 Jan 2024 13:25:35 -0500 Subject: [PATCH 5/6] add finallyData to request data --- src/types/onyx/Request.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/types/onyx/Request.ts b/src/types/onyx/Request.ts index 79de2b8ff967..c3543466ea5d 100644 --- a/src/types/onyx/Request.ts +++ b/src/types/onyx/Request.ts @@ -18,6 +18,7 @@ type RequestData = { shouldUseSecure?: boolean; successData?: OnyxUpdate[]; failureData?: OnyxUpdate[]; + finallyData?: OnyxUpdate[]; idempotencyKey?: string; resolve?: (value: Response) => void; From f73460660c4b9ad071c749d1677bb562e97abfe0 Mon Sep 17 00:00:00 2001 From: Nikki Wines Date: Thu, 4 Jan 2024 20:14:37 -0500 Subject: [PATCH 6/6] handle finally data separately for more versatility --- src/libs/actions/OnyxUpdates.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/libs/actions/OnyxUpdates.ts b/src/libs/actions/OnyxUpdates.ts index 2bd4d262c9ba..e4ecc751cd24 100644 --- a/src/libs/actions/OnyxUpdates.ts +++ b/src/libs/actions/OnyxUpdates.ts @@ -34,14 +34,17 @@ function applyHTTPSOnyxUpdates(request: Request, response: Response) { return onyxDataUpdatePromise .then(() => { // Handle the request's success/failure data (client-side data) - const successData = request.successData?.length ? request.successData : request.finallyData; - if (response.jsonCode === 200 && successData) { - return updateHandler(successData); + if (response.jsonCode === 200 && request.successData) { + return updateHandler(request.successData); } - - const failureData = request.failureData?.length ? request.failureData : request.finallyData; - if (response.jsonCode !== 200 && failureData) { - return updateHandler(failureData); + if (response.jsonCode !== 200 && request.failureData) { + return updateHandler(request.failureData); + } + return Promise.resolve(); + }) + .then(() => { + if (request.finallyData) { + return updateHandler(request.finallyData); } return Promise.resolve(); })