Skip to content

Commit

Permalink
feat: Cache and reload getProfile (#2131)
Browse files Browse the repository at this point in the history
  • Loading branch information
NigelBreslaw authored Jul 23, 2024
1 parent 3c7085a commit e629f73
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
1 change: 1 addition & 0 deletions native/app/Root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ function Root() {
console.error("No navigationRef");
}
} else if (authenticated === "AUTHENTICATED" && itemsDefinitionReady && bungieDefinitionsReady) {
useGGStore.getState().loadCachedProfile();
getFullProfile();
useGGStore.getState().setLastRefreshTime();
const intervalId = setInterval(refreshIfNeeded, 2000);
Expand Down
44 changes: 37 additions & 7 deletions native/app/store/Account/AccountSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,21 @@ import {
updateAllPages,
} from "@/app/store/InventoryLogic.ts";
import { bitmaskContains } from "@/app/utilities/Helpers.ts";
import type {
BucketHash,
CharacterId,
DestinyItemBase,
GuardianData,
ItemHash,
ProfileData,
import {
getSimpleProfileSchema,
type BucketHash,
type CharacterId,
type DestinyItemBase,
type GuardianData,
type ItemHash,
type ProfileData,
} from "@/app/core/GetProfile.ts";
import { InventoryPageEnums, lightLevelBuckets, type UISections } from "@/app/inventory/logic/Helpers.ts";
import { iconUrl, screenshotUrl } from "@/app/core/ApiResponse.ts";
import { DamageType, DestinyClass, ItemSubType, ItemType, SectionBuckets, TierType } from "@/app/bungie/Enums.ts";
import { ArmorSort, WeaponsSort } from "@/app/store/Types.ts";
import { getAsyncStorage, setAsyncStorage } from "@/app/store/DefinitionsSlice.ts";
import { safeParse } from "valibot";

export type AccountSliceSetter = Parameters<StateCreator<IStore, [], [], AccountSlice>>[0];
export type AccountSliceGetter = Parameters<StateCreator<IStore, [], [], AccountSlice>>[1];
Expand Down Expand Up @@ -126,6 +129,7 @@ export interface AccountSlice {
setSecondarySpecial: (characterId: CharacterId, itemHash: ItemHash) => void;
setLastRefreshTime: () => void;
setDemoMode: () => Promise<void>;
loadCachedProfile: () => Promise<void>;
getCharacterIndex: (characterId: CharacterId) => number;
updateLightLevel: () => void;
showInventoryMenu: (show: boolean) => void;
Expand Down Expand Up @@ -229,8 +233,16 @@ export const createAccountSlice: StateCreator<IStore, [], [], AccountSlice> = (s
},

updateProfile: (profile) => {
const p1 = performance.now();
updateProfile(get, set, profile);
const p2 = performance.now();
console.log("update entire Profile", `${(p2 - p1).toFixed(4)} ms`);
get().updateLightLevel();
try {
setAsyncStorage("CACHED_PROFILE", JSON.stringify(profile));
} catch (e) {
console.error("Failed to save cached profile", e);
}
},

setQuantityToTransfer: (quantityToTransfer) => {
Expand Down Expand Up @@ -290,6 +302,24 @@ export const createAccountSlice: StateCreator<IStore, [], [], AccountSlice> = (s
updateProfile(get, set, await demoData.json());
get().updateLightLevel();
},
loadCachedProfile: async () => {
try {
const p1 = performance.now();
const cachedProfile = await getAsyncStorage("CACHED_PROFILE");
if (cachedProfile) {
const profileData = JSON.parse(cachedProfile);
const p2 = performance.now();
console.log("load cached profile", `${(p2 - p1).toFixed(4)} ms`);
const validatedProfile = safeParse(getSimpleProfileSchema, profileData);
if (validatedProfile.success) {
updateProfile(get, set, validatedProfile.output as ProfileData);
get().updateLightLevel();
}
}
} catch (e) {
console.error("Failed to load cached profile", e);
}
},
getCharacterIndex: (characterId: CharacterId) => {
const characterIndex = get().ggCharacters.findIndex((character) => character.characterId === characterId);
return characterIndex;
Expand Down
2 changes: 2 additions & 0 deletions native/app/store/Authentication/AuthenticationSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { BungieProfileSchema, type BungieMembershipProfiles, type LinkedProfiles
import { getBungieUser } from "@/app/bungie/Account.ts";
import { safeParse } from "valibot";
import { InventoryPageEnums } from "@/app/inventory/logic/Helpers.ts";
import { setAsyncStorage } from "@/app/store/DefinitionsSlice.ts";

const initialBungieUser = {
supplementalDisplayName: "",
Expand Down Expand Up @@ -115,6 +116,7 @@ export const createAuthenticationSlice: StateCreator<IStore, [], [], Authenticat
currentInventoryPage: InventoryPageEnums.Weapons,
currentListIndex: 0,
});
setAsyncStorage("CACHED_PROFILE", "");
const membershipId = get().bungieUser.profile.membershipId;
deleteUserData(membershipId);
},
Expand Down
1 change: 1 addition & 0 deletions native/app/store/DefinitionsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ export const createDefinitionsSlice: StateCreator<IStore, [], [], DefinitionsSli
},
setInventorySectionWidth: (inventorySectionWidth) => set({ inventorySectionWidth }),
clearCache: () => {
setAsyncStorage("CACHED_PROFILE", "");
set({ itemDefinitionVersion: "", bungieDefinitionVersions: "", itemsDefinitionReady: false });
},
});
Expand Down
2 changes: 1 addition & 1 deletion native/app/store/Types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { DefinitionKey } from "@/app/core/BungieDefinitions.ts";

export type StorageKey = "ITEM_DEFINITION" | "ACCOUNTS";
export type AsyncStorageKey = DefinitionKey;
export type AsyncStorageKey = DefinitionKey | "CACHED_PROFILE";

export enum WeaponsSort {
Power = "POWER",
Expand Down

0 comments on commit e629f73

Please sign in to comment.