blowater/time.ts

25 lines
583 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) {}
now() {
this.time++;
return this.time;
}
set(t: number) {
this.time = Math.max(this.time, t);
}
}
export function fromEvents(events: Iterable<NostrEvent>): LamportTime {
let time = 0;
for (const event of events) {
const ts = getTags(event).lamport_timestamp;
if (ts && ts > time) {
time = ts;
}
}
return new LamportTime(time);
}