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

Improve import onyx performance #56046

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
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();
kubabutkiewicz marked this conversation as resolved.
Show resolved Hide resolved

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
Loading