Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ulixee
GitHub Repository: ulixee/secret-agent
Path: blob/main/replay/backend/storage/HistoryDb.ts
1030 views
1
import BaseDb from './BaseDb';
2
import { IHistoryRecord } from "~shared/interfaces/IHistoryRecord";
3
4
const MAX_RECORD_COUNT = 100;
5
6
export default class HistoryDb extends BaseDb<IHistoryRecord[]> {
7
private byId: { [id: string]: IHistoryRecord } = {};
8
9
constructor() {
10
super('History', []);
11
for (const record of this.allData) {
12
if (this.byId[record.id]) {
13
this.deleteById(record.id);
14
}
15
this.byId[record.id] = record;
16
}
17
}
18
19
public insert(record: IHistoryRecord): void {
20
this.byId[record.id] = record;
21
this.allData.unshift(record);
22
const removedRecords = this.allData.splice(MAX_RECORD_COUNT - 1);
23
for (const removedRecord of removedRecords) {
24
delete this.byId[removedRecord.id];
25
}
26
}
27
28
public findById(id: string) {
29
return this.byId[id];
30
}
31
32
public deleteById(id: string) {
33
const record = this.byId[id];
34
if (!record) return;
35
36
delete this.byId[id];
37
const index = this.allData.indexOf(record);
38
if (index < 0) return;
39
40
this.allData.splice(index, 1);
41
}
42
43
public fetchAll(): IHistoryRecord[] {
44
return [...this.allData];
45
}
46
}
47
48