Skip to content

Commit

Permalink
Merge pull request #31743 from Expensify/revert-30425-refactor-persis…
Browse files Browse the repository at this point in the history
…ted-requests

Revert "Refactor save method in PersistedRequests"
  • Loading branch information
justinpersaud authored Nov 22, 2023
2 parents 2e88d28 + a6c6b7f commit 2fe4269
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 88 deletions.
2 changes: 1 addition & 1 deletion src/libs/Network/SequentialQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ NetworkStore.onReconnection(flush);

function push(request: OnyxRequest) {
// Add request to Persisted Requests so that it can be retried if it fails
PersistedRequests.save(request);
PersistedRequests.save([request]);

// If we are offline we don't need to trigger the queue to empty as it will happen when we come back online
if (NetworkStore.isOffline()) {
Expand Down
3 changes: 0 additions & 3 deletions src/libs/Network/enhanceParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,5 @@ export default function enhanceParameters(command: string, parameters: Record<st
// Include current user's email in every request and the server logs
finalParameters.email = parameters.email ?? NetworkStore.getCurrentUserEmail();

// idempotencyKey declared in JS is front-end-only. We delete it here so it doesn't interfere with idempotency in other layers.
delete finalParameters.idempotencyKey;

return finalParameters;
}
16 changes: 7 additions & 9 deletions src/libs/actions/PersistedRequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@ function clear() {
return Onyx.set(ONYXKEYS.PERSISTED_REQUESTS, []);
}

function save(requestToPersist: Request) {
// Check for a request w/ matching idempotencyKey in the queue
const existingRequestIndex = persistedRequests.findIndex((request) => request.data?.idempotencyKey && request.data?.idempotencyKey === requestToPersist.data?.idempotencyKey);
if (existingRequestIndex > -1) {
// Merge the new request into the existing one, keeping its place in the queue
persistedRequests.splice(existingRequestIndex, 1, requestToPersist);
function save(requestsToPersist: Request[]) {
let requests: Request[] = [];
if (persistedRequests.length) {
requests = persistedRequests.concat(requestsToPersist);
} else {
// If not, push the new request to the end of the queue
persistedRequests.push(requestToPersist);
requests = requestsToPersist;
}
Onyx.set(ONYXKEYS.PERSISTED_REQUESTS, persistedRequests);
persistedRequests = requests;
Onyx.set(ONYXKEYS.PERSISTED_REQUESTS, requests);
}

function remove(requestToRemove: Request) {
Expand Down
9 changes: 2 additions & 7 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,9 +470,6 @@ function openReport(reportID, participantLoginList = [], newReportObject = {}, p
if (!reportID) {
return;
}

const commandName = 'OpenReport';

const optimisticReportData = [
{
onyxMethod: Onyx.METHOD.MERGE,
Expand Down Expand Up @@ -538,7 +535,6 @@ function openReport(reportID, participantLoginList = [], newReportObject = {}, p
emailList: participantLoginList ? participantLoginList.join(',') : '',
accountIDList: participantAccountIDList ? participantAccountIDList.join(',') : '',
parentReportActionID,
idempotencyKey: `${commandName}_${reportID}`,
};

if (isFromDeepLink) {
Expand Down Expand Up @@ -616,7 +612,6 @@ function openReport(reportID, participantLoginList = [], newReportObject = {}, p

// Add the createdReportActionID parameter to the API call
params.createdReportActionID = optimisticCreatedAction.reportActionID;
params.idempotencyKey = `${params.idempotencyKey}_NewReport_${optimisticCreatedAction.reportActionID}`;

// If we are creating a thread, ensure the report action has childReportID property added
if (newReportObject.parentReportID && parentReportActionID) {
Expand All @@ -637,12 +632,12 @@ function openReport(reportID, participantLoginList = [], newReportObject = {}, p

if (isFromDeepLink) {
// eslint-disable-next-line rulesdir/no-api-side-effects-method
API.makeRequestWithSideEffects(commandName, params, onyxData).finally(() => {
API.makeRequestWithSideEffects('OpenReport', params, onyxData).finally(() => {
Onyx.set(ONYXKEYS.IS_CHECKING_PUBLIC_ROOM, false);
});
} else {
// eslint-disable-next-line rulesdir/no-multiple-api-calls
API.write(commandName, params, onyxData);
API.write('OpenReport', params, onyxData);
}
}

Expand Down
1 change: 0 additions & 1 deletion src/types/onyx/Request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ type RequestData = {
shouldUseSecure?: boolean;
successData?: OnyxUpdate[];
failureData?: OnyxUpdate[];
idempotencyKey?: string;

resolve?: (value: Response) => void;
reject?: (value?: unknown) => void;
Expand Down
67 changes: 0 additions & 67 deletions tests/unit/PersistedRequestsTest.ts

This file was deleted.

0 comments on commit 2fe4269

Please sign in to comment.