Skip to content

GCounter

Defined in: src/crdt/GCounter.ts:21

Grow-only counter. Each replica tracks its own monotonic count and the global value is the sum. Merging takes the max per replica — which means losing or replaying messages is harmless: the counter never goes backwards.

Use this when only increments matter — page views, message counts, total bytes uploaded. For workloads that also need decrements (cart sizes, available stock) reach for PNCounter.

Math sanity: merge is the per-key max of the two state maps, which is the standard join-semilattice on Map<ReplicaId, ℕ>.

const a = GCounter.empty().increment(‘node-a’, 3); const b = GCounter.empty().increment(‘node-b’, 5); a.merge(b).value() // → 8 a.merge(b).merge(b).value() // → 8 (idempotent)

equals(other): boolean

Defined in: src/crdt/GCounter.ts:67

Equality by value — two counters with the same per-replica counts.

GCounter

boolean


increment(replica, delta?): GCounter

Defined in: src/crdt/GCounter.ts:31

Bump the count for replica by delta (default 1). delta must be >= 0 — increments are the only allowed operation.

string

number = 1

GCounter


merge(other): GCounter

Defined in: src/crdt/GCounter.ts:46

Join two replicas. Must be a join-semilattice operation: total, idempotent, commutative, associative.

GCounter

GCounter

Crdt.merge


toJSON(): GCounterJson

Defined in: src/crdt/GCounter.ts:57

Wire-friendly representation — every CRDT must be JSON-encodable so it can travel through the cluster transport without bespoke codecs. toJSON() is the inverse of the static fromJSON factory each impl exposes.

GCounterJson

Crdt.toJSON


value(): number

Defined in: src/crdt/GCounter.ts:40

Total count = sum of every replica’s contribution.

number


static empty(): GCounter

Defined in: src/crdt/GCounter.ts:25

A counter at zero.

GCounter


static fromJSON(json): GCounter

Defined in: src/crdt/GCounter.ts:61

GCounterJson

GCounter