Files
AFFiNE-Mirror/packages/common/nbstore/src/utils/clock.ts
T
2024-12-07 08:05:02 +00:00

39 lines
755 B
TypeScript

export class ClockMap {
max: Date = new Date(0);
constructor(private readonly map: Map<string, Date>) {
for (const value of map.values()) {
if (value.getTime() > this.max.getTime()) {
this.max = value;
}
}
}
get(id: string): Date {
return this.map.get(id) ?? new Date(0);
}
set(id: string, value: Date) {
this.map.set(id, value);
if (value.getTime() > this.max.getTime()) {
this.max = value;
}
}
setIfBigger(id: string, value: Date) {
if (value.getTime() > this.get(id).getTime()) {
this.set(id, value);
return true;
}
return false;
}
clear() {
this.map.clear();
this.max = new Date(0);
}
keys() {
return Array.from(this.map.keys());
}
}