Skip to content

Commit

Permalink
fix race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
jasperhuangg committed Apr 28, 2023
1 parent 57de036 commit ae5de2e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 32 deletions.
4 changes: 2 additions & 2 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ PODS:
- react-native-airship (15.2.3):
- AirshipFrameworkProxy (= 2.0.5)
- React-Core
- react-native-blob-util (0.17.3):
- react-native-blob-util (0.16.2):
- React-Core
- react-native-cameraroll (5.3.1):
- React-Core
Expand Down Expand Up @@ -1072,7 +1072,7 @@ SPEC CHECKSUMS:
React-jsinspector: 31517b1de3fadf93ad8558840a8974c7a7160bd3
React-logger: b90aa6ed0dbc30717dc72d843af3cf4550297b22
react-native-airship: 25045092934bf6eabf483e803af0a6e31826b8b9
react-native-blob-util: 99f4d79189252f597fe0d810c57a3733b1b1dea6
react-native-blob-util: c3b0faecd2919db568e9d552084396f3e50b57c7
react-native-cameraroll: f3050460fe1708378698c16686bfaa5f34099be2
react-native-config: 7cd105e71d903104e8919261480858940a6b9c0e
react-native-document-picker: f68191637788994baed5f57d12994aa32cf8bf88
Expand Down
5 changes: 5 additions & 0 deletions src/libs/Navigation/AppNavigator/MainDrawerNavigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import SCREENS from '../../../SCREENS';
import Permissions from '../../Permissions';
import Timing from '../../actions/Timing';
import CONST from '../../../CONST';
import * as App from '../../actions/App';

// Screens
import ReportScreen from '../../../pages/home/ReportScreen';
Expand Down Expand Up @@ -97,6 +98,10 @@ class MainDrawerNavigator extends Component {
lodashGet(nextProps, 'route.params.openOnAdminRoom', false),
);
if (this.initialParams.reportID === initialNextParams.reportID) {
// We need to wait to open the app until this check is made, since there's a race condition that can happen
// where OpenApp will get called beforehand, setting isFirstTimeNewExpensifyUser to false and causing us
// to miss the deep-linked report in ReportUtils.findLastAccessedReport
App.confirmReadyToOpenApp();
return false;
}

Expand Down
74 changes: 44 additions & 30 deletions src/libs/actions/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ Onyx.connect({
callback: val => preferredLocale = val,
});

let resolveIsReadyPromise;
const isReadyToOpenApp = new Promise((resolve) => {
resolveIsReadyPromise = resolve;
});

console.log(">>>> initialized");

function confirmReadyToOpenApp() {
resolveIsReadyPromise();
}

/**
* @param {Array} policies
* @return {Array<String>} array of policy ids
Expand Down Expand Up @@ -128,36 +139,38 @@ AppState.addEventListener('change', (nextAppState) => {
* Fetches data needed for app initialization
*/
function openApp() {
// We need a fresh connection/callback here to make sure that the list of policyIDs that is sent to OpenApp is the most updated list from Onyx
const connectionID = Onyx.connect({
key: ONYXKEYS.COLLECTION.POLICY,
waitForCollectionCallback: true,
callback: (policies) => {
Onyx.disconnect(connectionID);
API.read('OpenApp', {policyIDList: getNonOptimisticPolicyIDs(policies)}, {
optimisticData: [
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.IS_LOADING_REPORT_DATA,
value: true,
},
],
successData: [
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.IS_LOADING_REPORT_DATA,
value: false,
},
],
failureData: [
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.IS_LOADING_REPORT_DATA,
value: false,
},
],
});
},
isReadyToOpenApp.then(() => {
// We need a fresh connection/callback here to make sure that the list of policyIDs that is sent to OpenApp is the most updated list from Onyx
const connectionID = Onyx.connect({
key: ONYXKEYS.COLLECTION.POLICY,
waitForCollectionCallback: true,
callback: (policies) => {
Onyx.disconnect(connectionID);
API.read('OpenApp', {policyIDList: getNonOptimisticPolicyIDs(policies)}, {
optimisticData: [
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.IS_LOADING_REPORT_DATA,
value: true,
},
],
successData: [
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.IS_LOADING_REPORT_DATA,
value: false,
},
],
failureData: [
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.IS_LOADING_REPORT_DATA,
value: false,
},
],
});
},
});
});
}

Expand Down Expand Up @@ -294,4 +307,5 @@ export {
openProfile,
openApp,
reconnectApp,
confirmReadyToOpenApp,
};

0 comments on commit ae5de2e

Please sign in to comment.