From 615a8babb3b6f8eb42620aa7a81baded8c55a72f Mon Sep 17 00:00:00 2001 From: Eduardo Date: Thu, 29 Aug 2024 19:02:57 +0200 Subject: [PATCH] Applying comments + unit tests --- src/libs/Network/SequentialQueue.ts | 6 +++--- src/libs/actions/App.ts | 8 +++----- src/libs/actions/PersistedRequests.ts | 2 -- src/types/onyx/Request.ts | 29 ++++++++++++++++----------- 4 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/libs/Network/SequentialQueue.ts b/src/libs/Network/SequentialQueue.ts index 9fd65602eca2..57bfdc176fe3 100644 --- a/src/libs/Network/SequentialQueue.ts +++ b/src/libs/Network/SequentialQueue.ts @@ -199,11 +199,11 @@ function push(newRequest: OnyxRequest) { const {checkAndFixConflictingRequest} = newRequest; if (checkAndFixConflictingRequest) { - const {conflictAction} = checkAndFixConflictingRequest(requests, newRequest); + const {conflictAction} = checkAndFixConflictingRequest(requests); - if (conflictAction.type === 'save') { + if (conflictAction.type === 'push') { PersistedRequests.save(newRequest); - } else { + } else if (conflictAction.type === 'replace') { PersistedRequests.update(conflictAction.index, newRequest); } } else { diff --git a/src/libs/actions/App.ts b/src/libs/actions/App.ts index 7f7fc95ae5d4..331c39c35137 100644 --- a/src/libs/actions/App.ts +++ b/src/libs/actions/App.ts @@ -275,21 +275,19 @@ function reconnectApp(updateIDFrom: OnyxEntry = 0) { } API.write(WRITE_COMMANDS.RECONNECT_APP, params, getOnyxDataForOpenOrReconnect(), { - checkAndFixConflictingRequest: (persistedRequests, newRequest) => { + checkAndFixConflictingRequest: (persistedRequests) => { const index = persistedRequests.findIndex((request) => request.command === WRITE_COMMANDS.RECONNECT_APP); if (index === -1) { return { - request: newRequest, conflictAction: { - type: 'save', + type: 'push', }, }; } return { - request: newRequest, conflictAction: { - type: 'update', + type: 'replace', index, }, }; diff --git a/src/libs/actions/PersistedRequests.ts b/src/libs/actions/PersistedRequests.ts index 18d66ee9ccb7..50a273e07a97 100644 --- a/src/libs/actions/PersistedRequests.ts +++ b/src/libs/actions/PersistedRequests.ts @@ -1,12 +1,10 @@ import isEqual from 'lodash/isEqual'; import Onyx from 'react-native-onyx'; -import {WRITE_COMMANDS} from '@libs/API/types'; import Log from '@libs/Log'; import ONYXKEYS from '@src/ONYXKEYS'; import type {Request} from '@src/types/onyx'; let persistedRequests: Request[] = []; -const keepLastInstance: string[] = [WRITE_COMMANDS.RECONNECT_APP]; Onyx.connect({ key: ONYXKEYS.PERSISTED_REQUESTS, diff --git a/src/types/onyx/Request.ts b/src/types/onyx/Request.ts index 0b583422f738..7f17ae6b9d72 100644 --- a/src/types/onyx/Request.ts +++ b/src/types/onyx/Request.ts @@ -55,9 +55,6 @@ type RequestData = { shouldSkipWebProxy?: boolean; }; -/** Model of requests sent to the API */ -type Request = RequestData & OnyxData & RequestConflictResolver; - /** * Model of a conflict request that has to be updated, in the request queue. */ @@ -65,7 +62,7 @@ type ConflictRequestUpdate = { /** * The action to take in case of a conflict. */ - type: 'update'; + type: 'replace'; /** * The index of the request in the queue to update. @@ -74,28 +71,33 @@ type ConflictRequestUpdate = { }; /** - * Model of a conflict request that has to be saved, in the request queue. + * Model of a conflict request that has to be saved at the end the request queue. */ type ConflictRequestSave = { /** * The action to take in case of a conflict. */ - type: 'save'; + type: 'push'; }; /** - * An object that has the request and the action to take in case of a conflict. + * Model of a conflict request that no need to be updated or saved, in the request queue. */ -type ConflictActionData = { +type ConflictRequestNoAction = { /** - * The request that is conflicting with the new request. + * The action to take in case of a conflict. */ - request: Request; + type: 'noAction'; +}; +/** + * An object that has the request and the action to take in case of a conflict. + */ +type ConflictActionData = { /** * The action to take in case of a conflict. */ - conflictAction: ConflictRequestUpdate | ConflictRequestSave; + conflictAction: ConflictRequestUpdate | ConflictRequestSave | ConflictRequestNoAction; }; /** @@ -106,9 +108,12 @@ type RequestConflictResolver = { /** * A function that checks if a new request conflicts with any existing requests in the queue. */ - checkAndFixConflictingRequest?: (persistedRequest: Request[], request: Request) => ConflictActionData; + checkAndFixConflictingRequest?: (persistedRequest: Request[]) => ConflictActionData; }; +/** Model of requests sent to the API */ +type Request = RequestData & OnyxData & RequestConflictResolver; + /** * An object used to describe how a request can be paginated. */