diff --git a/packages/logic/src/actions/turn/general/index.ts b/packages/logic/src/actions/turn/general/index.ts index df99b8d..4260a11 100644 --- a/packages/logic/src/actions/turn/general/index.ts +++ b/packages/logic/src/actions/turn/general/index.ts @@ -63,6 +63,11 @@ export const isGeneralTurnCommandKey = ( export class GeneralTurnCommandLoader { + private readonly cache = new Map< + GeneralTurnCommandKey, + Promise + >(); + constructor( private readonly importers: Record< GeneralTurnCommandKey, @@ -73,11 +78,17 @@ export class GeneralTurnCommandLoader { async load( key: GeneralTurnCommandKey ): Promise { + const cached = this.cache.get(key); + if (cached) { + return cached; + } const importer = this.importers[key]; if (!importer) { throw new Error(`Unknown general turn command key: ${key}`); } - return importer(); + const loading = importer(); + this.cache.set(key, loading); + return loading; } } diff --git a/packages/logic/src/actions/turn/nation/index.ts b/packages/logic/src/actions/turn/nation/index.ts index c61b1c8..72a1a01 100644 --- a/packages/logic/src/actions/turn/nation/index.ts +++ b/packages/logic/src/actions/turn/nation/index.ts @@ -42,6 +42,11 @@ export const isNationTurnCommandKey = ( export class NationTurnCommandLoader { + private readonly cache = new Map< + NationTurnCommandKey, + Promise + >(); + constructor( private readonly importers: Record< NationTurnCommandKey, @@ -52,11 +57,17 @@ export class NationTurnCommandLoader { async load( key: NationTurnCommandKey ): Promise { + const cached = this.cache.get(key); + if (cached) { + return cached; + } const importer = this.importers[key]; if (!importer) { throw new Error(`Unknown nation turn command key: ${key}`); } - return importer(); + const loading = importer(); + this.cache.set(key, loading); + return loading; } }