008d85a8fd
Hand-built CRDT primitives: Lamport clock + version vectors, op-log, LWW register/map, RGA sequence, fractional indexing, marks store, sync encode, and doc/replica. Includes eslint flat config + composite tsconfig.
30 lines
742 B
TypeScript
30 lines
742 B
TypeScript
import type { OpId, SiteId } from './id';
|
|
|
|
/**
|
|
* A Lamport clock for one site: hands out monotonically increasing op ids and
|
|
* advances past observed remote ops so locally-generated ids stay causally later.
|
|
*/
|
|
export class LamportClock {
|
|
private counter: number;
|
|
|
|
constructor(public readonly site: SiteId, start = 0) {
|
|
this.counter = start;
|
|
}
|
|
|
|
/** Generate the next op id for a local operation. */
|
|
tick(): OpId {
|
|
this.counter += 1;
|
|
return { site: this.site, clock: this.counter };
|
|
}
|
|
|
|
/** Advance past a remote op so future local ticks are causally after it. */
|
|
observe(id: OpId): void {
|
|
if (id.clock > this.counter)
|
|
this.counter = id.clock;
|
|
}
|
|
|
|
get value(): number {
|
|
return this.counter;
|
|
}
|
|
}
|