Skip to content

Commit

Permalink
Applying comments + unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gedu committed Aug 29, 2024
1 parent 6f278c0 commit 615a8ba
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
6 changes: 3 additions & 3 deletions src/libs/Network/SequentialQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 3 additions & 5 deletions src/libs/actions/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,21 +275,19 @@ function reconnectApp(updateIDFrom: OnyxEntry<number> = 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,
},
};
Expand Down
2 changes: 0 additions & 2 deletions src/libs/actions/PersistedRequests.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
29 changes: 17 additions & 12 deletions src/types/onyx/Request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,14 @@ 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.
*/
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.
Expand All @@ -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;
};

/**
Expand All @@ -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.
*/
Expand Down

0 comments on commit 615a8ba

Please sign in to comment.