-
Notifications
You must be signed in to change notification settings - Fork 10
/
StorePersistentDriver.ts
55 lines (38 loc) · 1.42 KB
/
StorePersistentDriver.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
export interface StorePersistentDump<StoreState> {
dumpHistory: StorePersistentPacket<StoreState>[];
}
export interface StorePersistentPacket<StoreState> {
data: StoreState;
timestamp: number;
}
export abstract class StorePersistentDriver<StoreState> {
public persistence: boolean = true;
constructor(readonly name: string, readonly lifetime: number = Infinity) {}
public initialState: StoreState = null;
public abstract type: string;
public abstract write(pack: StorePersistentPacket<StoreState>): void;
public abstract read(): StorePersistentPacket<StoreState>;
public abstract saveDump(pack: StorePersistentPacket<StoreState>): number;
public abstract readDump(id: number): StorePersistentPacket<StoreState>;
public abstract resetHistory(): void;
public abstract clear(): void;
public abstract getDumpHistory(): number[];
public abstract removeDump(timestamp: number): void;
public pack(data: StoreState): StorePersistentPacket<StoreState> {
return {
data: data,
timestamp: Date.now(),
};
}
public reset(): StorePersistentPacket<StoreState> {
const pack = this.pack(this.initialState);
this.write(pack);
return pack;
}
public get storeName(): string {
return `store.persistent.${this.type}.${this.name}`.toLowerCase();
}
public get dumpHistoryName(): string {
return `store.persistent.dump.history.${this.type}.${this.name}`.toLowerCase();
}
}