Skip to content

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

decrement(replica, delta?): PNCounter

Defined in: src/crdt/PNCounter.ts:32

string

number = 1

PNCounter


equals(other): boolean

Defined in: src/crdt/PNCounter.ts:52

PNCounter

boolean


increment(replica, delta?): PNCounter

Defined in: src/crdt/PNCounter.ts:27

string

number = 1

PNCounter


merge(other): PNCounter

Defined in: src/crdt/PNCounter.ts:39

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

PNCounter

PNCounter

Crdt.merge


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.

PNCounterJson

Crdt.toJSON


value(): number

Defined in: src/crdt/PNCounter.ts:37

number


static empty(): PNCounter

Defined in: src/crdt/PNCounter.ts:23

PNCounter


static fromJSON(json): PNCounter

Defined in: src/crdt/PNCounter.ts:47

PNCounterJson

PNCounter