1.7 KiB
1.7 KiB
two-p-set
(no submodules)
Purpose
Provides TwoPSet<T>, a Two-Phase Set CRDT. Elements can be added and removed exactly once; removed elements can never be re-added. The set value is the difference between the add-set and the remove-set. This guarantees eventual consistency at the cost of restricting re-addition.
Features
add(element)— adds to the A-set; throwsOPERATION_INVALIDif element was previously removedremove(element)— moves element to the R-set; no-op if not in A-sethas(element)— true if in A-set but not R-setvalue()— returns aSet<T>of live elementsmerge(state)— union-merges both A-set and R-settoJSON()/fromJSON()— serialization round-tripclone()— deep copy
Usage: Basic Example
import { TwoPSet } from "@suaveplan/crdt";
const set = new TwoPSet<string>();
set.add("alice");
set.remove("alice");
console.log(set.has("alice")); // false
Usage: Advanced Example
import { TwoPSet } from "@suaveplan/crdt";
const a = new TwoPSet<number>();
a.add(1);
const b = new TwoPSet<number>();
b.add(2);
a.merge(b.state());
console.log([...a.value()]); // [1, 2]
API Reference
TwoPSet<T>
add(element)— throwsOPERATION_INVALIDif previously removedremove(element)— no-op if element not in A-sethas(element): booleanvalue(): Set<T>merge(state: TwoPSetState<T>): void
Implementation Notes
Elements are serialised to JSON for key comparison, enabling support for object elements with structural equality. The restriction that removed elements cannot be re-added is a fundamental invariant of 2P-Set that enables simple merge logic.