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)
Implements
Section titled “Implements”Crdt<GCounter>
Methods
Section titled “Methods”equals()
Section titled “equals()”equals(
other):boolean
Defined in: src/crdt/GCounter.ts:67
Equality by value — two counters with the same per-replica counts.
Parameters
Section titled “Parameters”GCounter
Returns
Section titled “Returns”boolean
increment()
Section titled “increment()”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.
Parameters
Section titled “Parameters”replica
Section titled “replica”string
delta?
Section titled “delta?”number = 1
Returns
Section titled “Returns”GCounter
merge()
Section titled “merge()”merge(
other):GCounter
Defined in: src/crdt/GCounter.ts:46
Join two replicas. Must be a join-semilattice operation: total, idempotent, commutative, associative.
Parameters
Section titled “Parameters”GCounter
Returns
Section titled “Returns”GCounter
Implementation of
Section titled “Implementation of”toJSON()
Section titled “toJSON()”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.
Returns
Section titled “Returns”Implementation of
Section titled “Implementation of”value()
Section titled “value()”value():
number
Defined in: src/crdt/GCounter.ts:40
Total count = sum of every replica’s contribution.
Returns
Section titled “Returns”number
empty()
Section titled “empty()”
staticempty():GCounter
Defined in: src/crdt/GCounter.ts:25
A counter at zero.
Returns
Section titled “Returns”GCounter
fromJSON()
Section titled “fromJSON()”
staticfromJSON(json):GCounter
Defined in: src/crdt/GCounter.ts:61
Parameters
Section titled “Parameters”Returns
Section titled “Returns”GCounter