feat: add caching mechanism to GeneralTurnCommandLoader and NationTurnCommandLoader for improved performance
This commit is contained in:
@@ -63,6 +63,11 @@ export const isGeneralTurnCommandKey = (
|
|||||||
|
|
||||||
|
|
||||||
export class GeneralTurnCommandLoader {
|
export class GeneralTurnCommandLoader {
|
||||||
|
private readonly cache = new Map<
|
||||||
|
GeneralTurnCommandKey,
|
||||||
|
Promise<GeneralTurnCommandModule>
|
||||||
|
>();
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly importers: Record<
|
private readonly importers: Record<
|
||||||
GeneralTurnCommandKey,
|
GeneralTurnCommandKey,
|
||||||
@@ -73,11 +78,17 @@ export class GeneralTurnCommandLoader {
|
|||||||
async load(
|
async load(
|
||||||
key: GeneralTurnCommandKey
|
key: GeneralTurnCommandKey
|
||||||
): Promise<GeneralTurnCommandModule> {
|
): Promise<GeneralTurnCommandModule> {
|
||||||
|
const cached = this.cache.get(key);
|
||||||
|
if (cached) {
|
||||||
|
return cached;
|
||||||
|
}
|
||||||
const importer = this.importers[key];
|
const importer = this.importers[key];
|
||||||
if (!importer) {
|
if (!importer) {
|
||||||
throw new Error(`Unknown general turn command key: ${key}`);
|
throw new Error(`Unknown general turn command key: ${key}`);
|
||||||
}
|
}
|
||||||
return importer();
|
const loading = importer();
|
||||||
|
this.cache.set(key, loading);
|
||||||
|
return loading;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -42,6 +42,11 @@ export const isNationTurnCommandKey = (
|
|||||||
|
|
||||||
|
|
||||||
export class NationTurnCommandLoader {
|
export class NationTurnCommandLoader {
|
||||||
|
private readonly cache = new Map<
|
||||||
|
NationTurnCommandKey,
|
||||||
|
Promise<NationTurnCommandModule>
|
||||||
|
>();
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly importers: Record<
|
private readonly importers: Record<
|
||||||
NationTurnCommandKey,
|
NationTurnCommandKey,
|
||||||
@@ -52,11 +57,17 @@ export class NationTurnCommandLoader {
|
|||||||
async load(
|
async load(
|
||||||
key: NationTurnCommandKey
|
key: NationTurnCommandKey
|
||||||
): Promise<NationTurnCommandModule> {
|
): Promise<NationTurnCommandModule> {
|
||||||
|
const cached = this.cache.get(key);
|
||||||
|
if (cached) {
|
||||||
|
return cached;
|
||||||
|
}
|
||||||
const importer = this.importers[key];
|
const importer = this.importers[key];
|
||||||
if (!importer) {
|
if (!importer) {
|
||||||
throw new Error(`Unknown nation turn command key: ${key}`);
|
throw new Error(`Unknown nation turn command key: ${key}`);
|
||||||
}
|
}
|
||||||
return importer();
|
const loading = importer();
|
||||||
|
this.cache.set(key, loading);
|
||||||
|
return loading;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user