Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add finallyData as alternative to successData and failureData #33530

Merged
merged 7 commits into from
Jan 10, 2024
Merged
8 changes: 6 additions & 2 deletions src/libs/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,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 @@ -50,6 +51,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 @@ -104,6 +106,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 @@ -151,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<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 @@ -155,22 +155,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 @@ -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,
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 @@ -42,6 +42,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 @@ -199,16 +199,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 @@ -224,7 +215,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 Response from './Response';
type OnyxData = {
successData?: OnyxUpdate[];
failureData?: OnyxUpdate[];
finallyData?: OnyxUpdate[];
optimisticData?: OnyxUpdate[];
};

NikkiWines marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -17,6 +18,7 @@ type RequestData = {
shouldUseSecure?: boolean;
successData?: OnyxUpdate[];
failureData?: OnyxUpdate[];
finallyData?: OnyxUpdate[];
idempotencyKey?: string;

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