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:
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user