mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-23 19:49:50 +02:00
feat: discrimination
This commit is contained in:
1 parent
e7337145d2
commit
44711a58c7
2 files changed
+4
-4
No files matched your search
@@ -165,8 +165,8 @@ export abstract class AbstractAPI {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function Cache(
|
export function Cache<Arguments extends unknown[]>(
|
||||||
discriminator: (...args: unknown[]) => string = () => "",
|
discriminator: (...args: Arguments) => string = () => "",
|
||||||
) {
|
) {
|
||||||
const cache = new WeakMap<object, Map<string, unknown>>();
|
const cache = new WeakMap<object, Map<string, unknown>>();
|
||||||
|
|
||||||
@@ -177,7 +177,7 @@ export function Cache(
|
|||||||
) {
|
) {
|
||||||
const original = descriptor.value;
|
const original = descriptor.value;
|
||||||
|
|
||||||
descriptor.value = async function (...args: unknown[]) {
|
descriptor.value = async function (...args: Arguments) {
|
||||||
const key = discriminator(...args);
|
const key = discriminator(...args);
|
||||||
const thisCache = cache.get(this) ?? new Map<string, unknown>();
|
const thisCache = cache.get(this) ?? new Map<string, unknown>();
|
||||||
|
|
||||||
|
|||||||
@@ -168,7 +168,7 @@ export class ServerAPI extends AbstractAPI {
|
|||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Cache((id) => id)
|
@Cache<[string]>((id) => id)
|
||||||
async tank(id: string) {
|
async tank(id: string) {
|
||||||
const tankList = await this.tankList();
|
const tankList = await this.tankList();
|
||||||
const tankListEntry = tankList.list.find((tank) => tank.id === id);
|
const tankListEntry = tankList.list.find((tank) => tank.id === id);
|
||||||
|
|||||||
Reference in new issue
Block a user