mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-24 03:59:50 +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 { api } from "../../core/api/dynamic";
|
||||||
import { useAwait } from "../../hooks/useAwait";
|
import { useAwait } from "../../hooks/useAwait";
|
||||||
import { useGameStrings } from "../../hooks/useGameStrings";
|
import { useGameStrings } from "../../hooks/useGameStrings";
|
||||||
import { useLocale } from "../../hooks/useLocale";
|
import { useLocale } from "../../hooks/useLocale";
|
||||||
import type { PopularTank } from "../../protos/popular_tanks";
|
|
||||||
import { LinkI18n } from "../LinkI18n";
|
import { LinkI18n } from "../LinkI18n";
|
||||||
import { Text } from "../Text";
|
import { Text } from "../Text";
|
||||||
import styles from "./index.module.css";
|
import styles from "./index.module.css";
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ export abstract class AbstractAPI {
|
|||||||
export function Cache<Arguments extends unknown[]>(
|
export function Cache<Arguments extends unknown[]>(
|
||||||
discriminator: (...args: Arguments) => string = (...args) => args.join("-"),
|
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 (
|
return function (
|
||||||
_target: unknown,
|
_target: unknown,
|
||||||
@@ -73,18 +73,32 @@ export function Cache<Arguments extends unknown[]>(
|
|||||||
) {
|
) {
|
||||||
const original = descriptor.value;
|
const original = descriptor.value;
|
||||||
|
|
||||||
descriptor.value = async function (...args: Arguments) {
|
descriptor.value = function (...args: Arguments) {
|
||||||
const key = discriminator(...args);
|
const key = discriminator(...args);
|
||||||
const thisCache = cache.get(this) ?? new Map<string, unknown>();
|
|
||||||
|
|
||||||
if (!cache.has(this)) cache.set(this, thisCache);
|
let thisCache = cache.get(this);
|
||||||
if (thisCache.has(key)) return thisCache.get(key);
|
|
||||||
|
|
||||||
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 type { GetStaticPaths } from "astro";
|
||||||
import ColorScale from "../../components/ColorScale.astro";
|
import ColorScale from "../../components/ColorScale.astro";
|
||||||
import HomeCosmetics from "../../components/HomeCosmetics.astro";
|
import HomeCosmetics from "../../components/HomeCosmetics.astro";
|
||||||
|
import HomeGames from "../../components/HomeGames.astro";
|
||||||
import HomeHero from "../../components/HomeHero.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 HomeTechnical from "../../components/HomeTechnical.astro";
|
||||||
import { tools } from "../../core/tools/tools";
|
import { tools } from "../../core/tools/tools";
|
||||||
import BaseLayout from "../../layouts/BaseLayout.astro";
|
import BaseLayout from "../../layouts/BaseLayout.astro";
|
||||||
@@ -25,6 +28,9 @@ const { locale } = Astro.props;
|
|||||||
|
|
||||||
{Object.values(tools).map((tool) => <ColorScale color={tool.accent} />)}
|
{Object.values(tools).map((tool) => <ColorScale color={tool.accent} />)}
|
||||||
|
|
||||||
|
<HomeTanks {locale} />
|
||||||
|
<HomePlayers {locale} />
|
||||||
|
<HomeGames {locale} />
|
||||||
<HomeCosmetics {locale} />
|
<HomeCosmetics {locale} />
|
||||||
<HomeTechnical {locale} />
|
<HomeTechnical {locale} />
|
||||||
</BaseLayout>
|
</BaseLayout>
|
||||||
Reference in new issue
Block a user