From 3dafeb2e587cd901daa5d1c38410a3456ca15f03 Mon Sep 17 00:00:00 2001 From: Nigel Breslaw Date: Wed, 24 Jul 2024 16:58:01 +0300 Subject: [PATCH] feat: Speed up startup By auto loading prevoius definitions at startup. Then checking after if there are new versions. --- native/app/Root.tsx | 30 ++++++++++++++----- native/app/index.js | 4 --- native/app/inventory/pages/InventoryPage.tsx | 5 +++- .../Authentication/AuthenticationLogic.ts | 1 - .../Authentication/AuthenticationSlice.ts | 1 + native/app/store/DefinitionsSlice.ts | 28 ++++++++++++++--- native/app/store/GGStore.ts | 2 +- 7 files changed, 53 insertions(+), 18 deletions(-) diff --git a/native/app/Root.tsx b/native/app/Root.tsx index 94c9b3c05..7b5597fce 100644 --- a/native/app/Root.tsx +++ b/native/app/Root.tsx @@ -65,9 +65,9 @@ async function getBungieDefinitions() { } async function init() { + const startupTime = performance.now(); + useGGStore.getState().setAppStartupTime(startupTime); useGGStore.getState().initAuthentication(); - getCustomItemDefinition(); - getBungieDefinitions(); } init(); @@ -126,8 +126,6 @@ function Root() { allowFontScaling: false, }; - const itemsDefinitionReady = useGGStore((state) => state.itemsDefinitionReady); - const bungieDefinitionsReady = useGGStore((state) => state.bungieDefinitionsReady); const authenticated = useGGStore((state) => state.authenticated); const navigationRef = useRef>(null); const { width } = useWindowDimensions(); @@ -149,15 +147,33 @@ function Root() { } else { console.error("No navigationRef"); } - } else if (authenticated === "AUTHENTICATED" && itemsDefinitionReady && bungieDefinitionsReady) { + } else if (authenticated === "AUTHENTICATED" && appReady) { useGGStore.getState().loadCachedProfile(); - getFullProfile(); useGGStore.getState().setLastRefreshTime(); + getFullProfile(); const intervalId = setInterval(refreshIfNeeded, 2000); return () => clearInterval(intervalId); } - }, [authenticated, itemsDefinitionReady, bungieDefinitionsReady, stateHydrated]); + }, [authenticated, appReady, stateHydrated]); + + useEffect(() => { + async function initDefinitions() { + if (useGGStore.getState().previousDefinitionsSuccessfullyLoaded) { + useGGStore.getState().fastLoadDefinitions(); + // pause for 200ms to allow the definitions to load + await new Promise((resolve) => setTimeout(resolve, 1000)); + getCustomItemDefinition(); + getBungieDefinitions(); + } else { + getCustomItemDefinition(); + getBungieDefinitions(); + } + } + if (stateHydrated) { + initDefinitions(); + } + }, [stateHydrated]); return ( diff --git a/native/app/index.js b/native/app/index.js index b82f7c8eb..6ac1ce9c2 100644 --- a/native/app/index.js +++ b/native/app/index.js @@ -1,13 +1,9 @@ import "react-native-gesture-handler"; // Avoid crash in production https://reactnavigation.org/docs/stack-navigator/#installation import * as Sentry from "@sentry/react-native"; import { registerRootComponent } from "expo"; -import { useGGStore } from "@/app/store/GGStore.ts"; import Root from "./Root.tsx"; -const startupTime = performance.now(); -useGGStore.getState().setAppStartupTime(startupTime); - const routingInstrumentation = new Sentry.ReactNavigationInstrumentation({ enableTimeToInitialDisplay: true, }); diff --git a/native/app/inventory/pages/InventoryPage.tsx b/native/app/inventory/pages/InventoryPage.tsx index c5256c79e..2f688cc6e 100644 --- a/native/app/inventory/pages/InventoryPage.tsx +++ b/native/app/inventory/pages/InventoryPage.tsx @@ -145,10 +145,13 @@ export default function InventoryPage({ inventoryPageEnum, pageEstimatedFlashLis } onLoad={() => { if (index === pageData.length - 1) { + const p1 = useGGStore.getState().appStartupTime; + const p2 = performance.now(); + console.log("load time", `${(p2 - p1).toFixed(4)} ms`); setPageReady(true); jumpToCharacterRef.current(); opacity.value = withSpring(1, { - duration: 1000, + duration: 750, reduceMotion: ReduceMotion.System, }); } diff --git a/native/app/store/Authentication/AuthenticationLogic.ts b/native/app/store/Authentication/AuthenticationLogic.ts index 0149930ad..7d529a8bd 100644 --- a/native/app/store/Authentication/AuthenticationLogic.ts +++ b/native/app/store/Authentication/AuthenticationLogic.ts @@ -122,7 +122,6 @@ async function getTokenInternal( errorMessage: string, ): Promise { try { - console.log("getTokenInternal!!!"); const updatedToken = await getUpdatedAccessToken(authToken); return updatedToken; } catch (e) { diff --git a/native/app/store/Authentication/AuthenticationSlice.ts b/native/app/store/Authentication/AuthenticationSlice.ts index d5d8df0de..e47a1f6ee 100644 --- a/native/app/store/Authentication/AuthenticationSlice.ts +++ b/native/app/store/Authentication/AuthenticationSlice.ts @@ -115,6 +115,7 @@ export const createAuthenticationSlice: StateCreator void; loadCustomDefinitions: (uniqueKey: string) => Promise; loadBungieDefinitions: (bungieManifest: BungieManifest) => Promise; + fastLoadDefinitions: () => Promise; showSnackBar: (message: string) => void; setInventorySectionWidth: (inventorySectionWidth: number) => void; clearCache: () => void; @@ -87,7 +88,7 @@ export const createDefinitionsSlice: StateCreator { set({ itemsDefinitionReady: true }); if (get().bungieDefinitionsReady && get().stateHydrated) { - set({ appReady: true }); + set({ appReady: true, previousDefinitionsSuccessfullyLoaded: true }); } }, setBungieDefinitionsReady: () => { set({ bungieDefinitionsReady: true }); if (get().itemsDefinitionReady && get().stateHydrated) { - set({ appReady: true }); + const p1 = get().appStartupTime; + const p2 = performance.now(); + console.log("setItemsDefinitionReady", `${(p2 - p1).toFixed(4)} ms`); + set({ appReady: true, previousDefinitionsSuccessfullyLoaded: true }); } }, loadCustomDefinitions: async (uniqueKey) => { const storedVersion = get().itemDefinitionVersion; + // Don't attempt to get an already loaded definition + if (storedVersion === uniqueKey && get().itemsDefinitionReady) { + console.info("No new custom definitions needed"); + return; + } + if (storedVersion === uniqueKey) { // use the already downloaded version await loadLocalItemDefinitionVersion(get, set); @@ -119,6 +129,12 @@ export const createDefinitionsSlice: StateCreator { + loadLocalItemDefinitionVersion(get, set); + loadLocalBungieDefinitions(get, set); + }, showSnackBar: (message) => { Toast.show({ type: "success", diff --git a/native/app/store/GGStore.ts b/native/app/store/GGStore.ts index ce7c82914..e4cab085a 100644 --- a/native/app/store/GGStore.ts +++ b/native/app/store/GGStore.ts @@ -35,7 +35,7 @@ export const useGGStore = create()( itemDefinitionVersion: state.itemDefinitionVersion, bungieDefinitionVersions: state.bungieDefinitionVersions, - allDefinitionsSuccessfullyLoaded: state.allDefinitionsSuccessfullyLoaded, + previousDefinitionsSuccessfullyLoaded: state.previousDefinitionsSuccessfullyLoaded, }), onRehydrateStorage: () => { return (state, error) => {