import type { Strings } from "@blitzkit/i18n"; import locales from "@blitzkit/i18n/locales.json"; import type { Avatar } from "@protos/blitzkit/avatar"; import type { AvatarList } from "@protos/blitzkit/avatar_list"; import type { Avatars } from "@protos/blitzkit/avatars"; import type { Background } from "@protos/blitzkit/background"; import type { Backgrounds } from "@protos/blitzkit/backgrounds"; import type { PopularTanks } from "@protos/blitzkit/popular_tanks"; import type { Tank } from "@protos/blitzkit/tank"; import type { TankList } from "@protos/blitzkit/tank_list"; import type { Tanks } from "@protos/blitzkit/tanks"; import type { Tiers } from "@protos/blitzkit/tiers"; export abstract class AbstractAPI { abstract tankList(): Promise; abstract tank(id: string): Promise; abstract tanks(): Promise; abstract popularTanks(): Promise; abstract tiers(): Promise; abstract strings(locale: string): Promise; abstract gameStrings(locale: string): Promise>; protected abstract _groupedGameStrings( locale: string, group: string, ): Promise>; abstract gameStringGroups(): Promise; abstract avatar(id: string): Promise; abstract avatars(): Promise; abstract avatarList(): Promise; abstract background(id: string): Promise; abstract backgrounds(): Promise; private assertLocale(locale: string) { const supported = locales.supported.find( (supported) => supported.locale === locale, ); if (supported === undefined) { throw new Error(`Unsupported locale: ${locale}`); } return supported; } @Cache<[string, string, boolean]>((...args) => args.join("-")) async groupedGameStrings(locale: string, group: string, prefix: boolean) { this.assertLocale(locale); const gameStrings = await this._groupedGameStrings(locale, group); if (!prefix) return gameStrings; const strings: Record = {}; for (const key in gameStrings) { strings[`${group}__${key}`] = gameStrings[key]; } return strings; } } export function Cache( discriminator: (...args: Arguments) => string = (...args) => args.join("-"), ) { const cache = new WeakMap>>(); return function ( _target: unknown, _propertyKey: string, descriptor: PropertyDescriptor, ) { const original = descriptor.value; descriptor.value = function (...args: Arguments) { const key = discriminator(...args); let thisCache = cache.get(this); if (!thisCache) { thisCache = new Map(); cache.set(this, thisCache); } if (thisCache.has(key)) { return thisCache.get(key); } const promise = (async () => { try { return await original.apply(this, args); } catch (error) { thisCache!.delete(key); throw error; } })(); thisCache.set(key, promise); return promise; }; }; }