blowater/app/time.ts

24 lines
508 B
TypeScript
Raw Normal View History

2024-01-01 17:28:10 +00:00
import { NostrEvent } from "../libs/nostr.ts/nostr.ts";
2023-06-30 14:05:57 +00:00
import { getTags } from "./nostr.ts";
export class LamportTime {
2023-11-25 14:29:50 +00:00
private time = 0;
2023-11-18 17:11:07 +00:00
2023-11-25 14:29:50 +00:00
fromEvents(events: Iterable<NostrEvent>) {
2023-11-18 17:11:07 +00:00
for (const event of events) {
const ts = getTags(event).lamport_timestamp;
2023-11-25 14:29:50 +00:00
if (ts) {
this.set(ts);
2023-11-18 17:11:07 +00:00
}
}
}
2023-06-30 14:05:57 +00:00
now() {
this.time++;
return this.time;
}
set(t: number) {
this.time = Math.max(this.time, t);
}
}