mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-12-01 22:02:45 +01:00
feat: use decorators
This commit is contained in:
1 parent
44711a58c7
commit
8e3b5de249
5 files changed
+278
-316
No files matched your search
@@ -13,16 +13,23 @@ import type { Tanks } from "../../protos/tanks";
|
||||
export abstract class AbstractAPI {
|
||||
abstract tankList(): Promise<TankList>;
|
||||
abstract tank(id: string): Promise<Tank>;
|
||||
abstract tanks(): Promise<Tanks>;
|
||||
abstract popularTanks(): Promise<PopularTanks>;
|
||||
|
||||
private _tanksCache: Tanks | undefined;
|
||||
protected abstract _tanks(): Promise<Tanks>;
|
||||
async tanks() {
|
||||
if (this._tanksCache === undefined) {
|
||||
this._tanksCache = await this._tanks();
|
||||
}
|
||||
abstract strings(locale: string): Promise<Strings>;
|
||||
abstract gameStrings(locale: string): Promise<Record<string, string>>;
|
||||
protected abstract _groupedGameStrings(
|
||||
locale: string,
|
||||
group: string,
|
||||
): Promise<Record<string, string>>;
|
||||
abstract gameStringGroups(): Promise<string[]>;
|
||||
|
||||
return this._tanksCache;
|
||||
}
|
||||
abstract avatar(id: string): Promise<Avatar>;
|
||||
abstract avatars(): Promise<Avatars>;
|
||||
abstract avatarList(): Promise<AvatarList>;
|
||||
|
||||
abstract background(id: string): Promise<Background>;
|
||||
abstract backgrounds(): Promise<Backgrounds>;
|
||||
|
||||
private assertLocale(locale: string) {
|
||||
const supported = locales.supported.find(
|
||||
@@ -36,137 +43,26 @@ export abstract class AbstractAPI {
|
||||
return supported;
|
||||
}
|
||||
|
||||
private _stringsCache: Record<string, Strings> = {};
|
||||
protected abstract _strings(locale: string): Promise<Strings>;
|
||||
async strings(locale: string) {
|
||||
this.assertLocale(locale);
|
||||
|
||||
if (this._stringsCache[locale] === undefined) {
|
||||
this._stringsCache[locale] = await this._strings(locale);
|
||||
}
|
||||
|
||||
return this._stringsCache[locale];
|
||||
}
|
||||
|
||||
private _gameStringsCache: Record<string, Record<string, string>> = {};
|
||||
protected abstract _gameStrings(
|
||||
locale: string,
|
||||
): Promise<Record<string, string>>;
|
||||
async gameStrings(locale: string) {
|
||||
this.assertLocale(locale);
|
||||
|
||||
if (this._gameStringsCache[locale] === undefined) {
|
||||
this._gameStringsCache[locale] = await this._gameStrings(locale);
|
||||
}
|
||||
|
||||
return this._gameStringsCache[locale];
|
||||
}
|
||||
|
||||
private _groupedGameStringsCache: Record<
|
||||
string,
|
||||
Record<string, Record<string, string>>
|
||||
> = {};
|
||||
protected abstract _groupedGameStrings(
|
||||
locale: string,
|
||||
group: string,
|
||||
): Promise<Record<string, string>>;
|
||||
@Cache<[string, string, boolean]>((...args) => args.join("-"))
|
||||
async groupedGameStrings(locale: string, group: string, prefix: boolean) {
|
||||
this.assertLocale(locale);
|
||||
|
||||
if (this._groupedGameStringsCache[locale] === undefined) {
|
||||
this._groupedGameStringsCache[locale] = {};
|
||||
}
|
||||
const gameStrings = await this._groupedGameStrings(locale, group);
|
||||
|
||||
if (this._groupedGameStringsCache[locale][group] === undefined) {
|
||||
this._groupedGameStringsCache[locale][group] =
|
||||
await this._groupedGameStrings(locale, group);
|
||||
}
|
||||
|
||||
if (!prefix) return this._groupedGameStringsCache[locale][group];
|
||||
if (!prefix) return gameStrings;
|
||||
|
||||
const strings: Record<string, string> = {};
|
||||
|
||||
for (const key in this._groupedGameStringsCache[locale][group]) {
|
||||
strings[`${group}__${key}`] =
|
||||
this._groupedGameStringsCache[locale][group][key];
|
||||
for (const key in gameStrings) {
|
||||
strings[`${group}__${key}`] = gameStrings[key];
|
||||
}
|
||||
|
||||
return strings;
|
||||
}
|
||||
|
||||
private _avatarCache: Record<string, Avatar> = {};
|
||||
protected abstract _avatar(id: string): Promise<Avatar>;
|
||||
async avatar(id: string) {
|
||||
if (this._avatarCache[id] === undefined) {
|
||||
this._avatarCache[id] = await this._avatar(id);
|
||||
}
|
||||
|
||||
return this._avatarCache[id];
|
||||
}
|
||||
|
||||
private _avatarsCache: Avatars | undefined;
|
||||
protected abstract _avatars(): Promise<Avatars>;
|
||||
async avatars() {
|
||||
if (this._avatarsCache === undefined) {
|
||||
this._avatarsCache = await this._avatars();
|
||||
}
|
||||
|
||||
return this._avatarsCache;
|
||||
}
|
||||
|
||||
private _avatarListCache: AvatarList | undefined;
|
||||
protected abstract _avatarList(): Promise<AvatarList>;
|
||||
async avatarList() {
|
||||
if (this._avatarListCache === undefined) {
|
||||
this._avatarListCache = await this._avatarList();
|
||||
}
|
||||
|
||||
return this._avatarListCache;
|
||||
}
|
||||
|
||||
private _backgroundCache: Record<string, Background> = {};
|
||||
protected abstract _background(id: string): Promise<Background>;
|
||||
async background(id: string) {
|
||||
if (this._backgroundCache[id] === undefined) {
|
||||
this._backgroundCache[id] = await this._background(id);
|
||||
}
|
||||
|
||||
return this._backgroundCache[id];
|
||||
}
|
||||
|
||||
private _backgroundsCache: Backgrounds | undefined;
|
||||
protected abstract _backgrounds(): Promise<Backgrounds>;
|
||||
async backgrounds() {
|
||||
if (this._backgroundsCache === undefined) {
|
||||
this._backgroundsCache = await this._backgrounds();
|
||||
}
|
||||
|
||||
return this._backgroundsCache;
|
||||
}
|
||||
|
||||
private _gameStringGroupsCache: string[] | undefined;
|
||||
protected abstract _gameStringGroups(): Promise<string[]>;
|
||||
async gameStringGroups() {
|
||||
if (this._gameStringGroupsCache === undefined) {
|
||||
this._gameStringGroupsCache = await this._gameStringGroups();
|
||||
}
|
||||
|
||||
return this._gameStringGroupsCache;
|
||||
}
|
||||
|
||||
private _popularTanksCache: PopularTanks | undefined;
|
||||
protected abstract _popularTanks(): Promise<PopularTanks>;
|
||||
async popularTanks() {
|
||||
if (this._popularTanksCache === undefined) {
|
||||
this._popularTanksCache = await this._popularTanks();
|
||||
}
|
||||
|
||||
return this._popularTanksCache;
|
||||
}
|
||||
}
|
||||
|
||||
export function Cache<Arguments extends unknown[]>(
|
||||
discriminator: (...args: Arguments) => string = () => "",
|
||||
discriminator: (...args: Arguments) => string = (...args) => args.join("-"),
|
||||
) {
|
||||
const cache = new WeakMap<object, Map<string, unknown>>();
|
||||
|
||||
|
||||
Reference in new issue
Block a user