feat(crdt): add @robonen/crdt package

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.
This commit is contained in:
2026-06-07 16:28:58 +07:00
parent 70a8678743
commit 008d85a8fd
35 changed files with 1152 additions and 0 deletions
+29
View File
@@ -0,0 +1,29 @@
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;
}
}