Skip to content

Commit

Permalink
perf(stores): rehydrate data before retrieving instance
Browse files Browse the repository at this point in the history
  • Loading branch information
Vexcited committed Sep 2, 2024
1 parent ab60838 commit 3ed1d20
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/stores/account/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const useCurrentAccount = create<CurrentAccountStore>()((set, get) => ({
account: null,
linkedAccounts: [],

mutateProperty: (key, value) => {
mutateProperty: <T extends keyof PrimaryAccount>(key: T, value: PrimaryAccount[T]) => {
log(`mutate property ${key} in storage`, "current:update");

// Since "instance" is a runtime only key,
Expand Down Expand Up @@ -66,16 +66,6 @@ export const useCurrentAccount = create<CurrentAccountStore>()((set, get) => ({
set({ account });
useAccounts.setState({ lastOpenedAccountID: account.localID });

// Account is currently not authenticated,
if (typeof account.instance === "undefined") {
log("instance undefined, reloading...", "[switchTo]");
// Automatically reconnect the main instance.
const { instance, authentication } = await reload(account);
get().mutateProperty("authentication", authentication);
get().mutateProperty("instance", instance);
log("instance reload done !", "[switchTo]");
}

// Rehydrate every store that needs it.
await Promise.all([
[useTimetableStore, "timetable"] as const,
Expand All @@ -92,6 +82,16 @@ export const useCurrentAccount = create<CurrentAccountStore>()((set, get) => ({
return store.persist.rehydrate();
}));

// Account is currently not authenticated,
if (typeof account.instance === "undefined") {
log("instance undefined, reloading...", "[switchTo]");
// Automatically reconnect the main instance.
const { instance, authentication } = await reload(account);
get().mutateProperty("authentication", authentication);
get().mutateProperty("instance", instance);
log("instance reload done !", "[switchTo]");
}

const accounts = useAccounts.getState().accounts;
const linkedAccounts = account.linkedExternalLocalIDs.map((linkedID) => {
return {...accounts.find((acc) => acc.localID === linkedID)};
Expand Down

0 comments on commit 3ed1d20

Please sign in to comment.