Skip to content

Commit

Permalink
fix reseting to original state
Browse files Browse the repository at this point in the history
  • Loading branch information
kubabutkiewicz committed Jan 31, 2025
1 parent 81d9e88 commit 407dbfd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 35 deletions.
3 changes: 3 additions & 0 deletions src/components/ImportOnyxState/index.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {useOnyx} from 'react-native-onyx';
import type {FileObject} from '@components/AttachmentModal';
import {setIsUsingImportedState, setPreservedUserSession} from '@libs/actions/App';
import {setShouldForceOffline} from '@libs/actions/Network';
import {rollbackOngoingRequest} from '@libs/actions/PersistedRequests';
import Navigation from '@libs/Navigation/Navigation';
import type {OnyxValues} from '@src/ONYXKEYS';
import ONYXKEYS from '@src/ONYXKEYS';
Expand Down Expand Up @@ -32,6 +33,8 @@ export default function ImportOnyxState({setIsLoading}: ImportOnyxStateProps) {
return;
}

rollbackOngoingRequest();

setIsLoading(true);
readOnyxFile(file.uri)
.then((fileContent: string) => {
Expand Down
58 changes: 29 additions & 29 deletions src/libs/actions/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import ROUTES from '@src/ROUTES';
import type * as OnyxTypes from '@src/types/onyx';
import type {OnyxData} from '@src/types/onyx/Request';
import {setShouldForceOffline} from './Network';
import {getAll, getOngoingRequest, save} from './PersistedRequests';
import {getAll, save} from './PersistedRequests';
import {createDraftInitialWorkspace, createWorkspace, generatePolicyID} from './Policy/Policy';
import {resolveDuplicationConflictAction} from './RequestConflictUtils';
import {isAnonymousUser} from './Session';
Expand Down Expand Up @@ -570,41 +570,41 @@ function clearOnyxAndResetApp(shouldNavigateToHomepage?: boolean) {
const isStateImported = isUsingImportedState;
const shouldUseStagingServer = preservedShouldUseStagingServer;
const sequentialQueue = getAll();
const ongoingRequests = getOngoingRequest();
console.log('sequentialQueue', sequentialQueue);
console.log('ongoingRequests', ongoingRequests);
Onyx.clear(KEYS_TO_PRESERVE).then(() => {
// Network key is preserved, so when using imported state, we should stop forcing offline mode so that the app can re-fetch the network
if (isStateImported) {
setShouldForceOffline(false);
}

if (shouldNavigateToHomepage) {
Navigation.navigate(ROUTES.HOME);
}

if (preservedUserSession) {
Onyx.set(ONYXKEYS.SESSION, preservedUserSession);
Onyx.set(ONYXKEYS.PRESERVED_USER_SESSION, null);
}
Onyx.clear(KEYS_TO_PRESERVE)
.then(() => {
// Network key is preserved, so when using imported state, we should stop forcing offline mode so that the app can re-fetch the network
if (isStateImported) {
setShouldForceOffline(false);
}

if (shouldUseStagingServer) {
Onyx.set(ONYXKEYS.USER, {shouldUseStagingServer});
}
if (shouldNavigateToHomepage) {
Navigation.navigate(ROUTES.HOME);
}

// Requests in a sequential queue should be called even if the Onyx state is reset, so we do not lose any pending data.
// However, the OpenApp request must be called before any other request in a queue to ensure data consistency.
// To do that, sequential queue is cleared together with other keys, and then it's restored once the OpenApp request is resolved.
openApp().then(() => {
if (!sequentialQueue.length && isStateImported) {
return;
if (preservedUserSession) {
Onyx.set(ONYXKEYS.SESSION, preservedUserSession);
Onyx.set(ONYXKEYS.PRESERVED_USER_SESSION, null);
}

sequentialQueue.forEach((request) => {
save(request);
if (shouldUseStagingServer) {
Onyx.set(ONYXKEYS.USER, {shouldUseStagingServer});
}
})
.then(() => {
// Requests in a sequential queue should be called even if the Onyx state is reset, so we do not lose any pending data.
// However, the OpenApp request must be called before any other request in a queue to ensure data consistency.
// To do that, sequential queue is cleared together with other keys, and then it's restored once the OpenApp request is resolved.
openApp().then(() => {
if (!sequentialQueue || isStateImported) {
return;
}

sequentialQueue.forEach((request) => {
save(request);
});
});
});
});
clearSoundAssetsCache();
}

Expand Down
6 changes: 0 additions & 6 deletions src/libs/actions/PersistedRequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,14 @@ let ongoingRequest: Request | null = null;
Onyx.connect({
key: ONYXKEYS.PERSISTED_REQUESTS,
callback: (val) => {
console.log('val', val);
Log.info('[PersistedRequests] hit Onyx connect callback', false, {isValNullish: val == null});
persistedRequests = val ?? [];

console.log('Onyx connect persistedRequests', persistedRequests);
console.log('ongoingRequest', ongoingRequest);
console.log('ongoingRequest && persistedRequests.length > 0', ongoingRequest && persistedRequests.length > 0);
if (ongoingRequest && persistedRequests.length > 0) {
const nextRequestToProcess = persistedRequests.at(0);
console.log('nextRequestToProcess', nextRequestToProcess);
// We try to remove the next request from the persistedRequests if it is the same as ongoingRequest
// so we don't process it twice.
if (isEqual(nextRequestToProcess, ongoingRequest)) {
console.log('Removing the next request from the persistedRequests');
persistedRequests = persistedRequests.slice(1);
}
}
Expand Down

0 comments on commit 407dbfd

Please sign in to comment.