mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-21 18:57:03 +02:00
fix: optimize async cache
This commit is contained in:
1 parent
6d31dcb74b
commit
2504833a29
4 files changed
+29
-45
No files matched your search
@@ -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";
|
||||
|
||||
@@ -64,7 +64,7 @@ export abstract class AbstractAPI {
|
||||
export function Cache<Arguments extends unknown[]>(
|
||||
discriminator: (...args: Arguments) => string = (...args) => args.join("-"),
|
||||
) {
|
||||
const cache = new WeakMap<object, Map<string, unknown>>();
|
||||
const cache = new WeakMap<object, Map<string, Promise<unknown>>>();
|
||||
|
||||
return function (
|
||||
_target: unknown,
|
||||
@@ -73,18 +73,32 @@ export function Cache<Arguments extends unknown[]>(
|
||||
) {
|
||||
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<string, unknown>();
|
||||
|
||||
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;
|
||||
};
|
||||
};
|
||||
}
|
||||
@@ -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;
|
||||
---
|
||||
|
||||
<BaseLayout {locale} accent="amber">
|
||||
<HomeHero {locale} />
|
||||
|
||||
{Object.values(tools).map((tool) => <ColorScale color={tool.accent} />)}
|
||||
|
||||
<HomeTanks {locale} />
|
||||
<HomePlayers {locale} />
|
||||
<HomeGames {locale} />
|
||||
<HomeCosmetics {locale} />
|
||||
<HomeTechnical {locale} />
|
||||
</BaseLayout>
|
||||
@@ -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) => <ColorScale color={tool.accent} />)}
|
||||
|
||||
<HomeTanks {locale} />
|
||||
<HomePlayers {locale} />
|
||||
<HomeGames {locale} />
|
||||
<HomeCosmetics {locale} />
|
||||
<HomeTechnical {locale} />
|
||||
</BaseLayout>
|
||||
Reference in new issue
Block a user