1.6 KiB
1.6 KiB
pn-counter
(no submodules)
Purpose
Provides PNCounter, a positive-negative counter CRDT that supports both increment and decrement operations. Built from two G-Counters (one for increments, one for decrements), the net value is sum(increments) - sum(decrements). All CRDT invariants (commutativity, associativity, idempotency) are preserved.
Features
increment(nodeId, delta?)— adds to the increment counter fornodeIddecrement(nodeId, delta?)— adds to the decrement counter fornodeIdtotal()— net value: sum of all increments minus sum of all decrementsmerge(state)— element-wise maximum on both increment and decrement counterstoJSON()/fromJSON()— serialization round-tripclone()— deep copy
Usage: Basic Example
import { PNCounter } from "@suaveplan/crdt";
const counter = new PNCounter();
counter.increment("n1", 10);
counter.decrement("n1", 3);
console.log(counter.total()); // 7
Usage: Advanced Example
import { PNCounter } from "@suaveplan/crdt";
const a = new PNCounter("likes");
a.increment("user-1", 5);
const b = new PNCounter("likes");
b.decrement("user-2", 2);
a.merge(b.state());
console.log(a.total()); // 3 (5 - 2)
API Reference
PNCounter
increment(nodeId, delta?)— throwsOPERATION_INVALIDif delta ≤ 0decrement(nodeId, delta?)— throwsOPERATION_INVALIDif delta ≤ 0total(): number— net counter value
Implementation Notes
The value can be negative if decrements exceed increments. This is intentional — PNCounter does not enforce a floor of 0.