Skip to content

Commit

Permalink
Merge pull request #210 from giselles-ai/memoize-heavy-used-functions
Browse files Browse the repository at this point in the history
Optimize heavily used functions with React.cache
  • Loading branch information
satococoa authored Dec 11, 2024
2 parents e93f511 + 9c2b0fe commit 3a5d83b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
7 changes: 6 additions & 1 deletion lib/supabase/get-user.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isAuthRetryableFetchError } from "@supabase/supabase-js";
import { cache } from "react";
import { withRetry } from "../utils";
import { createClient } from "./server";

Expand All @@ -11,7 +12,7 @@ import { createClient } from "./server";
* IMPORTANT: This function will throw an error if executed while the user is not authenticated.
* Make sure the user is logged in before calling this function.
*/
export const getUser = async () => {
const getUser = async () => {
const supabase = await createClient();

const getUserFunc = async () => {
Expand Down Expand Up @@ -39,3 +40,7 @@ export const getUser = async () => {

return user;
};

const cachedGetUser = cache(getUser);

export { cachedGetUser as getUser };
6 changes: 5 additions & 1 deletion services/accounts/fetch-current-user.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { db, supabaseUserMappings, users } from "@/drizzle";
import { getUser } from "@/lib/supabase";
import { eq } from "drizzle-orm";
import { cache } from "react";

export async function fetchCurrentUser() {
async function fetchCurrentUser() {
const supabaseUser = await getUser();
const user = await db
.select({ dbId: users.dbId })
Expand All @@ -17,3 +18,6 @@ export async function fetchCurrentUser() {
}
return user[0];
}

const cachedFetchCurrentUser = cache(fetchCurrentUser);
export { cachedFetchCurrentUser as fetchCurrentUser };
6 changes: 5 additions & 1 deletion services/teams/fetch-current-team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import {
import { getGiselleSession } from "@/lib/giselle-session";
import { getUser } from "@/lib/supabase";
import { and, asc, eq } from "drizzle-orm";
import { cache } from "react";
import type { CurrentTeam } from "./types";

/**
* Fetches the current team of the user.
* This function uses session to get the teamDbId.
* If the user does not have a team, the first team is returned.
*/
export async function fetchCurrentTeam(): Promise<CurrentTeam> {
async function fetchCurrentTeam(): Promise<CurrentTeam> {
const supabaseUser = await getUser();
const session = await getGiselleSession();
const teamDbId = session?.teamDbId;
Expand All @@ -32,6 +33,9 @@ export async function fetchCurrentTeam(): Promise<CurrentTeam> {
return team;
}

const cachedFetchCurrentTeam = cache(fetchCurrentTeam);
export { cachedFetchCurrentTeam as fetchCurrentTeam };

async function fetchTeam(teamDbId: number, supabaseUserId: string) {
const result = await db
.select({
Expand Down

0 comments on commit 3a5d83b

Please sign in to comment.