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: Cache and reload getProfile #2131

Merged
merged 1 commit into from
Jul 23, 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
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