Skip to content

Commit d73a3bf

Browse files
authored
Merge pull request #49588 from callstack-internal/fix/persist-network-queue-on-clear
fix: persist pending actions when clearing Onyx state
2 parents f978bf9 + 4ca9cd6 commit d73a3bf

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

Diff for: src/libs/API/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,10 @@ function processRequest(request: OnyxRequest, type: ApiRequestType): Promise<voi
116116
* @param [onyxData.failureData] - Onyx instructions that will be passed to Onyx.update() when the response has jsonCode !== 200.
117117
* @param [onyxData.finallyData] - Onyx instructions that will be passed to Onyx.update() when the response has jsonCode === 200 or jsonCode !== 200.
118118
*/
119-
function write<TCommand extends WriteCommand>(command: TCommand, apiCommandParameters: ApiRequestCommandParameters[TCommand], onyxData: OnyxData = {}): void {
119+
function write<TCommand extends WriteCommand>(command: TCommand, apiCommandParameters: ApiRequestCommandParameters[TCommand], onyxData: OnyxData = {}): Promise<void | Response> {
120120
Log.info('[API] Called API write', false, {command, ...apiCommandParameters});
121121
const request = prepareRequest(command, CONST.API_REQUEST_TYPE.WRITE, apiCommandParameters, onyxData);
122-
processRequest(request, CONST.API_REQUEST_TYPE.WRITE);
122+
return processRequest(request, CONST.API_REQUEST_TYPE.WRITE);
123123
}
124124

125125
/**

Diff for: src/libs/actions/App.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,9 @@ function getOnyxDataForOpenOrReconnect(isOpenApp = false): OnyxData {
243243
* Fetches data needed for app initialization
244244
*/
245245
function openApp() {
246-
getPolicyParamsForOpenOrReconnect().then((policyParams: PolicyParamsForOpenOrReconnect) => {
246+
return getPolicyParamsForOpenOrReconnect().then((policyParams: PolicyParamsForOpenOrReconnect) => {
247247
const params: OpenAppParams = {enablePriorityModeFilter: true, ...policyParams};
248-
249-
API.write(WRITE_COMMANDS.OPEN_APP, params, getOnyxDataForOpenOrReconnect(true));
248+
return API.write(WRITE_COMMANDS.OPEN_APP, params, getOnyxDataForOpenOrReconnect(true));
250249
});
251250
}
252251

Diff for: src/pages/settings/Troubleshoot/TroubleshootPage.tsx

+14-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import useResponsiveLayout from '@hooks/useResponsiveLayout';
2323
import useThemeStyles from '@hooks/useThemeStyles';
2424
import useWaitForNavigation from '@hooks/useWaitForNavigation';
2525
import {setShouldMaskOnyxState} from '@libs/actions/MaskOnyx';
26+
import * as PersistedRequests from '@libs/actions/PersistedRequests';
2627
import ExportOnyxState from '@libs/ExportOnyxState';
2728
import Navigation from '@libs/Navigation/Navigation';
2829
import * as App from '@userActions/App';
@@ -153,8 +154,20 @@ function TroubleshootPage() {
153154
isVisible={isConfirmationModalVisible}
154155
onConfirm={() => {
155156
setIsConfirmationModalVisible(false);
157+
// Requests in a sequential queue should be called even if the Onyx state is reset, so we do not lose any pending data.
158+
// However, the OpenApp request must be called before any other request in a queue to ensure data consistency.
159+
// To do that, sequential queue is cleared together with other keys, and then it's restored once the OpenApp request is resolved.
160+
const sequentialQueue = PersistedRequests.getAll();
156161
Onyx.clear(App.KEYS_TO_PRESERVE).then(() => {
157-
App.openApp();
162+
App.openApp().then(() => {
163+
if (!sequentialQueue) {
164+
return;
165+
}
166+
167+
sequentialQueue.forEach((request) => {
168+
PersistedRequests.save(request);
169+
});
170+
});
158171
});
159172
}}
160173
onCancel={() => setIsConfirmationModalVisible(false)}

0 commit comments

Comments
 (0)