Skip to content

Commit

Permalink
feat: Speed up startup (#2143)
Browse files Browse the repository at this point in the history
By auto loading prevoius definitions at startup. Then checking after if there are new versions.
  • Loading branch information
NigelBreslaw authored Jul 24, 2024
1 parent c853db3 commit 4ea5270
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 18 deletions.
30 changes: 23 additions & 7 deletions native/app/Root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ async function getBungieDefinitions() {
}

async function init() {
const startupTime = performance.now();
useGGStore.getState().setAppStartupTime(startupTime);
useGGStore.getState().initAuthentication();
getCustomItemDefinition();
getBungieDefinitions();
}
init();

Expand Down Expand Up @@ -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<NavigationContainerRef<ReactNavigation.RootParamList>>(null);
const { width } = useWindowDimensions();
Expand All @@ -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 (
<GestureHandlerRootView>
Expand Down
4 changes: 0 additions & 4 deletions native/app/index.js
Original file line number Diff line number Diff line change
@@ -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,
});
Expand Down
5 changes: 4 additions & 1 deletion native/app/inventory/pages/InventoryPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}
Expand Down
1 change: 0 additions & 1 deletion native/app/store/Authentication/AuthenticationLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ async function getTokenInternal(
errorMessage: string,
): Promise<AuthToken> {
try {
console.log("getTokenInternal!!!");
const updatedToken = await getUpdatedAccessToken(authToken);
return updatedToken;
} catch (e) {
Expand Down
1 change: 1 addition & 0 deletions native/app/store/Authentication/AuthenticationSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export const createAuthenticationSlice: StateCreator<IStore, [], [], Authenticat
bungieMembershipProfiles: [],
currentInventoryPage: InventoryPageEnums.Weapons,
currentListIndex: 0,
previousDefinitionsSuccessfullyLoaded: false,
});
removeAsyncStorageItem("CACHED_PROFILE");
const membershipId = get().bungieUser.profile.membershipId;
Expand Down
28 changes: 24 additions & 4 deletions native/app/store/DefinitionsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export interface DefinitionsSlice {
itemDefinitionVersion: string;
bungieDefinitionsReady: boolean;
bungieDefinitionVersions: string;
allDefinitionsSuccessfullyLoaded: boolean;
previousDefinitionsSuccessfullyLoaded: boolean;

snackBarVisible: boolean;
snackBarMessage: string;
Expand All @@ -77,6 +77,7 @@ export interface DefinitionsSlice {
setBungieDefinitionsReady: () => void;
loadCustomDefinitions: (uniqueKey: string) => Promise<void>;
loadBungieDefinitions: (bungieManifest: BungieManifest) => Promise<void>;
fastLoadDefinitions: () => Promise<void>;
showSnackBar: (message: string) => void;
setInventorySectionWidth: (inventorySectionWidth: number) => void;
clearCache: () => void;
Expand All @@ -87,25 +88,34 @@ export const createDefinitionsSlice: StateCreator<IStore, [], [], DefinitionsSli
itemDefinitionVersion: "",
bungieDefinitionsReady: false,
bungieDefinitionVersions: "",
allDefinitionsSuccessfullyLoaded: false,
previousDefinitionsSuccessfullyLoaded: false,

snackBarVisible: false,
snackBarMessage: "",
inventorySectionWidth: 0,
setItemsDefinitionReady: () => {
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);
Expand All @@ -119,6 +129,12 @@ export const createDefinitionsSlice: StateCreator<IStore, [], [], DefinitionsSli
const storedVersion = get().bungieDefinitionVersions;
const versionKey = bungieManifest?.Response.version;

// Don't attempt to get an already loaded definition
if (storedVersion === versionKey && get().itemsDefinitionReady) {
console.info("No new bungie definitions needed", storedVersion, versionKey, get().itemsDefinitionReady);
return;
}

if (storedVersion === versionKey) {
// use the already downloaded version
await loadLocalBungieDefinitions(get, set);
Expand All @@ -128,6 +144,10 @@ export const createDefinitionsSlice: StateCreator<IStore, [], [], DefinitionsSli
await downloadAndStoreBungieDefinitions(get, set, bungieManifest);
}
},
fastLoadDefinitions: async () => {
loadLocalItemDefinitionVersion(get, set);
loadLocalBungieDefinitions(get, set);
},
showSnackBar: (message) => {
Toast.show({
type: "success",
Expand Down
2 changes: 1 addition & 1 deletion native/app/store/GGStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const useGGStore = create<IStore>()(

itemDefinitionVersion: state.itemDefinitionVersion,
bungieDefinitionVersions: state.bungieDefinitionVersions,
allDefinitionsSuccessfullyLoaded: state.allDefinitionsSuccessfullyLoaded,
previousDefinitionsSuccessfullyLoaded: state.previousDefinitionsSuccessfullyLoaded,
}),
onRehydrateStorage: () => {
return (state, error) => {
Expand Down

0 comments on commit 4ea5270

Please sign in to comment.