PNCounter
Defined in: src/crdt/PNCounter.ts:17
Positive/Negative counter — supports both increments and decrements
by tracking two grow-only counters: p (sum of increments) and
n (sum of decrements). Final value = p.value() - n.value().
Use this when the count can go up AND down — items in a cart, connected sessions, available inventory. For pure-increment workloads use the lighter GCounter.
const a = PNCounter.empty().increment(‘node-a’, 5); const b = PNCounter.empty().decrement(‘node-b’, 2); a.merge(b).value() // → 3
Implements
Section titled “Implements”Crdt<PNCounter>
Methods
Section titled “Methods”decrement()
Section titled “decrement()”decrement(
replica,delta?):PNCounter
Defined in: src/crdt/PNCounter.ts:32
Parameters
Section titled “Parameters”replica
Section titled “replica”string
delta?
Section titled “delta?”number = 1
Returns
Section titled “Returns”PNCounter
equals()
Section titled “equals()”equals(
other):boolean
Defined in: src/crdt/PNCounter.ts:52
Parameters
Section titled “Parameters”PNCounter
Returns
Section titled “Returns”boolean
increment()
Section titled “increment()”increment(
replica,delta?):PNCounter
Defined in: src/crdt/PNCounter.ts:27
Parameters
Section titled “Parameters”replica
Section titled “replica”string
delta?
Section titled “delta?”number = 1
Returns
Section titled “Returns”PNCounter
merge()
Section titled “merge()”merge(
other):PNCounter
Defined in: src/crdt/PNCounter.ts:39
Join two replicas. Must be a join-semilattice operation: total, idempotent, commutative, associative.
Parameters
Section titled “Parameters”PNCounter
Returns
Section titled “Returns”PNCounter
Implementation of
Section titled “Implementation of”toJSON()
Section titled “toJSON()”toJSON():
PNCounterJson
Defined in: src/crdt/PNCounter.ts:43
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/PNCounter.ts:37
Returns
Section titled “Returns”number
empty()
Section titled “empty()”
staticempty():PNCounter
Defined in: src/crdt/PNCounter.ts:23
Returns
Section titled “Returns”PNCounter
fromJSON()
Section titled “fromJSON()”
staticfromJSON(json):PNCounter
Defined in: src/crdt/PNCounter.ts:47
Parameters
Section titled “Parameters”Returns
Section titled “Returns”PNCounter