From 2504833a2986f1b684a34df38e27608436c10111 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tr=C3=A8sAbhi?= <43380238+tresabhi@users.noreply.github.com> Date: Fri, 17 Apr 2026 23:52:33 -0500 Subject: [PATCH] fix: optimize async cache --- .../src/components/TankCard/index.tsx | 2 +- packages/website-ue/src/core/api/abstract.ts | 30 +++++++++++----- .../src/pages/[...locale]/_index.astro | 36 ------------------- .../src/pages/[...locale]/index.astro | 6 ++++ 4 files changed, 29 insertions(+), 45 deletions(-) delete mode 100644 packages/website-ue/src/pages/[...locale]/_index.astro diff --git a/packages/website-ue/src/components/TankCard/index.tsx b/packages/website-ue/src/components/TankCard/index.tsx index cadf68bc5..19b8563e3 100644 --- a/packages/website-ue/src/components/TankCard/index.tsx +++ b/packages/website-ue/src/components/TankCard/index.tsx @@ -1,8 +1,8 @@ +import type { PopularTank } from "@protos/blitzkit/popular_tanks"; import { api } from "../../core/api/dynamic"; import { useAwait } from "../../hooks/useAwait"; import { useGameStrings } from "../../hooks/useGameStrings"; import { useLocale } from "../../hooks/useLocale"; -import type { PopularTank } from "../../protos/popular_tanks"; import { LinkI18n } from "../LinkI18n"; import { Text } from "../Text"; import styles from "./index.module.css"; diff --git a/packages/website-ue/src/core/api/abstract.ts b/packages/website-ue/src/core/api/abstract.ts index d293b5681..b35f0d2c2 100644 --- a/packages/website-ue/src/core/api/abstract.ts +++ b/packages/website-ue/src/core/api/abstract.ts @@ -64,7 +64,7 @@ export abstract class AbstractAPI { export function Cache( discriminator: (...args: Arguments) => string = (...args) => args.join("-"), ) { - const cache = new WeakMap>(); + const cache = new WeakMap>>(); return function ( _target: unknown, @@ -73,18 +73,32 @@ export function Cache( ) { const original = descriptor.value; - descriptor.value = async function (...args: Arguments) { + descriptor.value = function (...args: Arguments) { const key = discriminator(...args); - const thisCache = cache.get(this) ?? new Map(); - if (!cache.has(this)) cache.set(this, thisCache); - if (thisCache.has(key)) return thisCache.get(key); + let thisCache = cache.get(this); - const result = await original.apply(this, args); + if (!thisCache) { + thisCache = new Map(); + cache.set(this, thisCache); + } - thisCache.set(key, result); + if (thisCache.has(key)) { + return thisCache.get(key); + } - return result; + const promise = (async () => { + try { + return await original.apply(this, args); + } catch (error) { + thisCache!.delete(key); + throw error; + } + })(); + + thisCache.set(key, promise); + + return promise; }; }; } diff --git a/packages/website-ue/src/pages/[...locale]/_index.astro b/packages/website-ue/src/pages/[...locale]/_index.astro deleted file mode 100644 index a7db16f7c..000000000 --- a/packages/website-ue/src/pages/[...locale]/_index.astro +++ /dev/null @@ -1,36 +0,0 @@ ---- -import locales from "@blitzkit/i18n/locales.json"; -import type { GetStaticPaths } from "astro"; -import ColorScale from "../../components/ColorScale.astro"; -import HomeCosmetics from "../../components/HomeCosmetics.astro"; -import HomeGames from "../../components/HomeGames.astro"; -import HomeHero from "../../components/HomeHero.astro"; -import HomePlayers from "../../components/HomePlayers.astro"; -import HomeTanks from "../../components/HomeTanks.astro"; -import HomeTechnical from "../../components/HomeTechnical.astro"; -import { tools } from "../../core/tools/tools"; -import BaseLayout from "../../layouts/BaseLayout.astro"; - -export const getStaticPaths = (() => { - return [ - ...locales.supported - .filter(({ locale }) => locale !== locales.default) - .map(({ locale }) => ({ params: { locale }, props: { locale } })), - { params: { locale: undefined }, props: { locale: locales.default } }, - ]; -}) satisfies GetStaticPaths; - -const { locale } = Astro.props; ---- - - - - - {Object.values(tools).map((tool) => )} - - - - - - - diff --git a/packages/website-ue/src/pages/[...locale]/index.astro b/packages/website-ue/src/pages/[...locale]/index.astro index b14fbcaf6..a7db16f7c 100644 --- a/packages/website-ue/src/pages/[...locale]/index.astro +++ b/packages/website-ue/src/pages/[...locale]/index.astro @@ -3,7 +3,10 @@ import locales from "@blitzkit/i18n/locales.json"; import type { GetStaticPaths } from "astro"; import ColorScale from "../../components/ColorScale.astro"; import HomeCosmetics from "../../components/HomeCosmetics.astro"; +import HomeGames from "../../components/HomeGames.astro"; import HomeHero from "../../components/HomeHero.astro"; +import HomePlayers from "../../components/HomePlayers.astro"; +import HomeTanks from "../../components/HomeTanks.astro"; import HomeTechnical from "../../components/HomeTechnical.astro"; import { tools } from "../../core/tools/tools"; import BaseLayout from "../../layouts/BaseLayout.astro"; @@ -25,6 +28,9 @@ const { locale } = Astro.props; {Object.values(tools).map((tool) => )} + + +