Files
gridpilot.gg/adapters/racing/persistence/inmemory/InMemoryDriverStatsRepository.ts
2026-01-16 15:20:25 +01:00

43 lines
1.6 KiB
TypeScript

/**
* Infrastructure Adapter: InMemoryDriverStatsRepository
*
* In-memory implementation of IDriverStatsRepository.
* Stores computed driver statistics for caching and frontend queries.
*/
import type { DriverStats } from '@core/racing/application/use-cases/DriverStatsUseCase';
import type { DriverStatsRepository } from '@core/racing/domain/repositories/DriverStatsRepository';
import type { Logger } from '@core/shared/domain/Logger';
export class InMemoryDriverStatsRepository implements DriverStatsRepository {
private stats = new Map<string, DriverStats>();
constructor(private readonly logger: Logger) {
this.logger.info('[InMemoryDriverStatsRepository] Initialized.');
}
async getDriverStats(driverId: string): Promise<DriverStats | null> {
this.logger.debug(`[InMemoryDriverStatsRepository] Getting stats for driver: ${driverId}`);
return this.stats.get(driverId) ?? null;
}
getDriverStatsSync(driverId: string): DriverStats | null {
this.logger.debug(`[InMemoryDriverStatsRepository] Getting stats (sync) for driver: ${driverId}`);
return this.stats.get(driverId) ?? null;
}
async saveDriverStats(driverId: string, stats: DriverStats): Promise<void> {
this.logger.debug(`[InMemoryDriverStatsRepository] Saving stats for driver: ${driverId}`);
this.stats.set(driverId, stats);
}
async getAllStats(): Promise<Map<string, DriverStats>> {
this.logger.debug('[InMemoryDriverStatsRepository] Getting all stats');
return new Map(this.stats);
}
async clear(): Promise<void> {
this.logger.info('[InMemoryDriverStatsRepository] Clearing all stats');
this.stats.clear();
}
}