Skip to content

Commit

Permalink
Merge pull request #33530 from Expensify/nikki-finallyData
Browse files Browse the repository at this point in the history
Add finallyData as alternative to successData and failureData
  • Loading branch information
NikkiWines authored Jan 10, 2024
2 parents 6d683f7 + f734606 commit 50865ef
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 34 deletions.
8 changes: 6 additions & 2 deletions src/libs/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,21 @@ 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);

type OnyxData = {
optimisticData?: OnyxUpdate[];
successData?: OnyxUpdate[];
failureData?: OnyxUpdate[];
finallyData?: OnyxUpdate[];
};

type ApiRequestType = ValueOf<typeof CONST.API_REQUEST_TYPE>;

/**
* 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.
Expand All @@ -51,6 +52,7 @@ type ApiRequestType = ValueOf<typeof CONST.API_REQUEST_TYPE>;
* @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<string, unknown> = {}, onyxData: OnyxData = {}) {
Log.info('Called API write', false, {command, ...apiCommandParameters});
Expand Down Expand Up @@ -105,6 +107,7 @@ function write(command: string, apiCommandParameters: Record<string, unknown> =
* @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
Expand Down Expand Up @@ -152,6 +155,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<string, unknown>, onyxData: OnyxData = {}) {
// Ensure all write requests on the sequential queue have finished responding before running read requests.
Expand Down
4 changes: 2 additions & 2 deletions src/libs/Middleware/SaveResponseInOnyx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
if (!onyxUpdates && !request.successData && !request.failureData && !request.finallyData) {
return Promise.resolve(response);
}

Expand Down
23 changes: 4 additions & 19 deletions src/libs/actions/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,22 +159,15 @@ function getPolicyParamsForOpenOrReconnect(): Promise<PolicyParamsForOpenOrRecon
* Returns the Onyx data that is used for both the OpenApp and ReconnectApp API commands.
*/
function getOnyxDataForOpenOrReconnect(isOpenApp = false): OnyxData {
const defaultData: Required<OnyxData> = {
const defaultData = {
optimisticData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.IS_LOADING_REPORT_DATA,
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,
Expand All @@ -194,16 +187,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,
Expand Down
6 changes: 6 additions & 0 deletions src/libs/actions/OnyxUpdates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ function applyHTTPSOnyxUpdates(request: Request, response: Response) {
}
return Promise.resolve();
})
.then(() => {
if (request.finallyData) {
return updateHandler(request.finallyData);
}
return Promise.resolve();
})
.then(() => {
console.debug('[OnyxUpdateManager] Done applying HTTPS update');
return Promise.resolve(response);
Expand Down
13 changes: 2 additions & 11 deletions src/libs/actions/Session/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,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,
Expand All @@ -225,7 +216,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 = {
Expand Down
2 changes: 2 additions & 0 deletions src/types/onyx/Request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type Response from './Response';
type OnyxData = {
successData?: OnyxUpdate[];
failureData?: OnyxUpdate[];
finallyData?: OnyxUpdate[];
optimisticData?: OnyxUpdate[];
};

Expand All @@ -17,6 +18,7 @@ type RequestData = {
shouldUseSecure?: boolean;
successData?: OnyxUpdate[];
failureData?: OnyxUpdate[];
finallyData?: OnyxUpdate[];
idempotencyKey?: string;

resolve?: (value: Response) => void;
Expand Down

0 comments on commit 50865ef

Please sign in to comment.