Path: blob/main/replay/backend/storage/HistoryDb.ts
1030 views
import BaseDb from './BaseDb';1import { IHistoryRecord } from "~shared/interfaces/IHistoryRecord";23const MAX_RECORD_COUNT = 100;45export default class HistoryDb extends BaseDb<IHistoryRecord[]> {6private byId: { [id: string]: IHistoryRecord } = {};78constructor() {9super('History', []);10for (const record of this.allData) {11if (this.byId[record.id]) {12this.deleteById(record.id);13}14this.byId[record.id] = record;15}16}1718public insert(record: IHistoryRecord): void {19this.byId[record.id] = record;20this.allData.unshift(record);21const removedRecords = this.allData.splice(MAX_RECORD_COUNT - 1);22for (const removedRecord of removedRecords) {23delete this.byId[removedRecord.id];24}25}2627public findById(id: string) {28return this.byId[id];29}3031public deleteById(id: string) {32const record = this.byId[id];33if (!record) return;3435delete this.byId[id];36const index = this.allData.indexOf(record);37if (index < 0) return;3839this.allData.splice(index, 1);40}4142public fetchAll(): IHistoryRecord[] {43return [...this.allData];44}45}464748