web worker test
Some checks failed
Deploy SSR / deploy (push) Failing after 56s

This commit is contained in:
Lee
2024-09-13 16:38:45 +01:00
parent 671f520635
commit 7112d3f53b
8 changed files with 82 additions and 33 deletions

View File

@ -5,10 +5,10 @@ import localFont from "next/font/local";
import BackgroundImage from "../components/background-image";
import DatabaseLoader from "../components/loaders/database-loader";
import NavBar from "../components/navbar/navbar";
import { QueryProvider } from "../components/providers/query-provider";
import { ThemeProvider } from "../components/providers/theme-provider";
import { Toaster } from "../components/ui/toaster";
import { TooltipProvider } from "../components/ui/tooltip";
import { QueryProvider } from "@/components/providers/query-provider";
import { ThemeProvider } from "@/components/providers/theme-provider";
import { Toaster } from "@/components/ui/toaster";
import { TooltipProvider } from "@/components/ui/tooltip";
import "./globals.css";
const siteFont = localFont({

View File

@ -1,4 +1,4 @@
export const diffColors: Record<string, string> = {
const diffColors: Record<string, string> = {
easy: "MediumSeaGreen",
normal: "#59b0f4",
hard: "tomato",
@ -45,6 +45,5 @@ export function accuracyToColor(acc: number): string {
return badge.color;
}
}
// Return a default color if no badge matches
return "#000000"; // black or any default color
return "#000000"; // fallback color
}

View File

@ -0,0 +1,16 @@
import * as Comlink from "comlink";
import { scoresaberFetcher } from "@/common/data-fetcher/impl/scoresaber";
export interface WorkerApi {
getName: typeof getName;
}
const workerApi: WorkerApi = {
getName,
};
async function getName() {
return await scoresaberFetcher.lookupPlayer("76561198449412074");
}
Comlink.expose(workerApi);

View File

@ -0,0 +1,5 @@
import * as Comlink from "comlink";
import { WorkerApi } from "@/common/workers/worker";
export const scoresaberReloadedWorker = () =>
Comlink.wrap<WorkerApi>(new Worker(new URL("./worker.ts", import.meta.url)));

View File

@ -8,6 +8,8 @@ import { useQuery } from "@tanstack/react-query";
import PlayerHeader from "./player-header";
import PlayerRankChart from "./player-rank-chart";
import PlayerScores from "./player-scores";
import { useEffect } from "react";
import { scoresaberReloadedWorker } from "@/common/workers/workers";
const REFRESH_INTERVAL = 5 * 60 * 1000; // 5 minutes
@ -31,6 +33,13 @@ export default function PlayerData({
refetchInterval: REFRESH_INTERVAL,
});
useEffect(() => {
(async () => {
const worker = scoresaberReloadedWorker();
console.log(await worker.getName());
})();
}, []);
if (data && (!isLoading || !isError)) {
player = data;
}

View File

@ -15,6 +15,41 @@ import Pagination from "../input/pagination";
import { Button } from "../ui/button";
import Score from "./score/score";
type Props = {
initialScoreData?: ScoreSaberPlayerScoresPage;
player: ScoreSaberPlayer;
sort: ScoreSort;
page: number;
};
type PageState = {
/**
* The current page
*/
page: number;
/**
* The current sort
*/
sort: ScoreSort;
};
const leaderboards = {
ScoreSaber: {
capabilities: {
search: true,
},
queries: {
lookupScores: (player: ScoreSaberPlayer, pageState: PageState) =>
scoresaberFetcher.lookupPlayerScores({
playerId: player.id,
sort: ScoreSort.top,
page: 1,
}),
},
},
};
const scoreSort = [
{
name: "Top",
@ -52,31 +87,13 @@ const scoreAnimation: Variants = {
},
};
type Props = {
initialScoreData?: ScoreSaberPlayerScoresPage;
player: ScoreSaberPlayer;
sort: ScoreSort;
page: number;
};
type PageState = {
/**
* The current page
*/
page: number;
/**
* The current sort
*/
sort: ScoreSort;
};
export default function PlayerScores({
initialScoreData,
player,
sort,
page,
}: Props) {
const leaderboard = leaderboards.ScoreSaber;
const { width } = useWindowDimensions();
const controls = useAnimation();
@ -96,13 +113,8 @@ export default function PlayerScores({
isLoading,
refetch,
} = useQuery({
queryKey: ["playerScores", player.id, pageState],
queryFn: () =>
scoresaberFetcher.lookupPlayerScores({
playerId: player.id,
sort: pageState.sort,
page: pageState.page,
}),
queryKey: ["playerScores", player.id, leaderboard, pageState],
queryFn: () => leaderboard.queries.lookupScores(player, pageState),
staleTime: 30 * 1000, // Cache data for 30 seconds
});