From 7672dd109ded059b9c9d2089d98d0bfc4c375c48 Mon Sep 17 00:00:00 2001 From: Arnaud Vergnet Date: Wed, 7 Oct 2020 10:42:08 +0200 Subject: [PATCH] load all initial data at once --- App.tsx | 22 +++++++++---------- src/managers/AsyncStorageManager.ts | 34 ++++++++++++++++------------- src/managers/ConnectionManager.ts | 34 +++++++++++++---------------- 3 files changed, 45 insertions(+), 45 deletions(-) diff --git a/App.tsx b/App.tsx index 5e10caa..b8e5f0d 100644 --- a/App.tsx +++ b/App.tsx @@ -80,9 +80,7 @@ export default class App extends React.Component<{}, StateType> { this.urlHandler = new URLHandler(this.onInitialURLParsed, this.onDetectURL); this.urlHandler.listen(); setSafeBounceHeight(Platform.OS === 'ios' ? 100 : 20); - this.loadAssetsAsync().finally(() => { - this.onLoadFinished(); - }); + this.loadAssetsAsync(); } /** @@ -149,7 +147,7 @@ export default class App extends React.Component<{}, StateType> { /** * Async loading is done, finish processing startup data */ - onLoadFinished() { + onLoadFinished = () => { // Only show intro if this is the first time starting the app ThemeManager.getInstance().setUpdateThemeCallback(this.onUpdateTheme); // Status bar goes dark if set too fast on ios @@ -176,19 +174,21 @@ export default class App extends React.Component<{}, StateType> { ), }); SplashScreen.hide(); - } + }; /** * Loads every async data * * @returns {Promise} */ - loadAssetsAsync = async () => { - await AsyncStorageManager.getInstance().loadPreferences(); - await ConnectionManager.getInstance() - .recoverLogin() - .catch(() => {}); - }; + loadAssetsAsync() { + Promise.all([ + AsyncStorageManager.getInstance().loadPreferences(), + ConnectionManager.getInstance().recoverLogin(), + ]) + .then(this.onLoadFinished) + .catch(this.onLoadFinished); + } /** * Renders the app based on loading state diff --git a/src/managers/AsyncStorageManager.ts b/src/managers/AsyncStorageManager.ts index f125936..ac0155c 100644 --- a/src/managers/AsyncStorageManager.ts +++ b/src/managers/AsyncStorageManager.ts @@ -209,21 +209,25 @@ export default class AsyncStorageManager { * @return {Promise} */ async loadPreferences() { - const prefKeys: Array = []; - // Get all available keys - Object.keys(AsyncStorageManager.PREFERENCES).forEach((key: string) => { - prefKeys.push(key); - }); - // Get corresponding values - const resultArray = await AsyncStorage.multiGet(prefKeys); - // Save those values for later use - resultArray.forEach((item: [string, string | null]) => { - const key = item[0]; - let val = item[1]; - if (val === null) { - val = AsyncStorageManager.PREFERENCES[key].default; - } - this.currentPreferences[key] = val; + return new Promise((resolve: () => void) => { + const prefKeys: Array = []; + // Get all available keys + Object.keys(AsyncStorageManager.PREFERENCES).forEach((key: string) => { + prefKeys.push(key); + }); + // Get corresponding values + AsyncStorage.multiGet(prefKeys).then((resultArray) => { + // Save those values for later use + resultArray.forEach((item: [string, string | null]) => { + const key = item[0]; + let val = item[1]; + if (val === null) { + val = AsyncStorageManager.PREFERENCES[key].default; + } + this.currentPreferences[key] = val; + }); + resolve(); + }); }); } diff --git a/src/managers/ConnectionManager.ts b/src/managers/ConnectionManager.ts index 51c160f..5f344d3 100644 --- a/src/managers/ConnectionManager.ts +++ b/src/managers/ConnectionManager.ts @@ -73,25 +73,21 @@ export default class ConnectionManager { * @returns Promise */ async recoverLogin(): Promise { - return new Promise( - (resolve: (token: string) => void, reject: () => void) => { - const token = this.getToken(); - if (token != null) { - resolve(token); - } else { - Keychain.getInternetCredentials(SERVER_NAME) - .then((data: Keychain.UserCredentials | false) => { - if (data && data.password != null) { - this.token = data.password; - resolve(this.token); - } else { - reject(); - } - }) - .catch((): void => reject()); - } - }, - ); + return new Promise((resolve: () => void) => { + const token = this.getToken(); + if (token != null) { + resolve(); + } else { + Keychain.getInternetCredentials(SERVER_NAME) + .then((data: Keychain.UserCredentials | false) => { + if (data && data.password != null) { + this.token = data.password; + } + resolve(); + }) + .catch(resolve); + } + }); } /**