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

Fix: deleted messages are shown until revisit page #38609

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/libs/Network/SequentialQueue.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Onyx from 'react-native-onyx';
import * as ActiveClientManager from '@libs/ActiveClientManager';
import {WRITE_COMMANDS} from '@libs/API/types';
import * as Request from '@libs/Request';
import * as RequestThrottle from '@libs/RequestThrottle';
import * as PersistedRequests from '@userActions/PersistedRequests';
Expand Down Expand Up @@ -45,6 +46,47 @@ function flushOnyxUpdatesQueue() {
QueuedOnyxUpdates.flushQueue();
}

/**
* Identifies and removes conflicting requests from the queue
*/
function reconcileRequests(persistedRequests: OnyxRequest[], commands: string[]) {
const requestsByActionID: Record<string, OnyxRequest[]> = {};

// Group requests by reportActionID
persistedRequests.forEach((request) => {
const {data} = request;
const reportActionID = data?.reportActionID as string;
if (reportActionID) {
if (!requestsByActionID[reportActionID]) {
requestsByActionID[reportActionID] = [];
}
requestsByActionID[reportActionID].push(request);
}
});

// Process requests with conflicting actions
Object.values(requestsByActionID).forEach((requests) => {
const conflictingRequests: OnyxRequest[] = [];
commands.forEach((command) => {
const conflictingRequest = requests.find((request) => request.command === command);
if (conflictingRequest) {
conflictingRequests.push(conflictingRequest);
}
});

if (conflictingRequests.length > 1) {
// Remove all requests as they cancel each other out
conflictingRequests.forEach((request) => {
// Perform action: Remove request from persisted requests
const index = persistedRequests.findIndex((req) => req === request);
if (index !== -1) {
persistedRequests.splice(index, 1);
}
});
}
});
}

/**
* Process any persisted requests, when online, one at a time until the queue is empty.
*
Expand All @@ -63,6 +105,11 @@ function process(): Promise<void> {
if (persistedRequests.length === 0 || NetworkStore.isOffline()) {
return Promise.resolve();
}

// Remove conflicting requests from the queue to avoid processing them
const commands = [WRITE_COMMANDS.ADD_COMMENT, WRITE_COMMANDS.DELETE_COMMENT];
reconcileRequests(persistedRequests, commands);

const requestToProcess = persistedRequests[0];

// Set the current request to a promise awaiting its processing so that getCurrentRequest can be used to take some action after the current request has processed.
Expand Down
Loading