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

feat: Speed up startup #2143

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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