blowater/time.ts

26 lines
602 B
TypeScript
Raw Normal View History

2023-08-28 17:58:05 +00:00
import { NostrEvent } from "./lib/nostr-ts/nostr.ts";
2023-06-30 14:05:57 +00:00
import { getTags } from "./nostr.ts";
export class LamportTime {
constructor(private time: number) {}
2023-11-18 17:11:07 +00:00
static FromEvents(events: Iterable<NostrEvent>) {
let time = 0;
for (const event of events) {
const ts = getTags(event).lamport_timestamp;
if (ts && ts > time) {
time = ts;
}
}
return new LamportTime(time);
}
2023-06-30 14:05:57 +00:00
now() {
this.time++;
return this.time;
}
set(t: number) {
this.time = Math.max(this.time, t);
}
}