LWWRegister
Defined in: src/crdt/LWWRegister.ts:26
Last-Writer-Wins register. A single value with a timestamp; on
merge the higher timestamp wins. Concurrent writes (same ts on
two replicas) are resolved deterministically by replicaId
lexicographic order — same input on every node, same winner.
Use this for single-value state that’s eventually consistent — a user’s display name, a feature-flag value, the latest-known health status of a service. When concurrent writes are common, pick a CRDT that captures both branches (e.g. an OR-Set of values) instead.
Wall-clock pitfall. Default timestamps come from Date.now(),
which can drift between machines and even go backwards on the same
machine (NTP correction). In practice this means a write from a
faster-clocked node always wins; if that’s a problem, pass a
clock option backed by a hybrid logical clock (HLC) or a Lamport
counter.
const a = LWWRegister.empty
Type Parameters
Section titled “Type Parameters”V
Implements
Section titled “Implements”Crdt<LWWRegister<V>>
Methods
Section titled “Methods”assign()
Section titled “assign()”assign(
replica,value,timestamp?):LWWRegister<V>
Defined in: src/crdt/LWWRegister.ts:44
Set the register to value, stamped with timestamp (default
Date.now()) on behalf of replica. The timestamp is what
merge uses to decide who wins — pass an explicit one if you
want HLC/Lamport semantics.
Parameters
Section titled “Parameters”replica
Section titled “replica”string
V
timestamp?
Section titled “timestamp?”number = ...
Returns
Section titled “Returns”LWWRegister<V>
equals()
Section titled “equals()”equals(
other):boolean
Defined in: src/crdt/LWWRegister.ts:80
Parameters
Section titled “Parameters”LWWRegister<V>
Returns
Section titled “Returns”boolean
merge()
Section titled “merge()”merge(
other):LWWRegister<V>
Defined in: src/crdt/LWWRegister.ts:54
Join two replicas. Must be a join-semilattice operation: total, idempotent, commutative, associative.
Parameters
Section titled “Parameters”LWWRegister<V>
Returns
Section titled “Returns”LWWRegister<V>
Implementation of
Section titled “Implementation of”timestamp()
Section titled “timestamp()”timestamp():
number
Defined in: src/crdt/LWWRegister.ts:52
Timestamp of the last write — 0 for an empty register.
Returns
Section titled “Returns”number
toJSON()
Section titled “toJSON()”toJSON():
LWWRegisterJson<V>
Defined in: src/crdt/LWWRegister.ts:66
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():
V|null
Defined in: src/crdt/LWWRegister.ts:49
Current value, or null if no assign has been called.
Returns
Section titled “Returns”V | null
empty()
Section titled “empty()”
staticempty<V>():LWWRegister<V>
Defined in: src/crdt/LWWRegister.ts:34
Empty register — no value yet. value() returns null.
Type Parameters
Section titled “Type Parameters”V
Returns
Section titled “Returns”LWWRegister<V>
fromJSON()
Section titled “fromJSON()”
staticfromJSON<V>(json):LWWRegister<V>
Defined in: src/crdt/LWWRegister.ts:75
Type Parameters
Section titled “Type Parameters”V
Parameters
Section titled “Parameters”Returns
Section titled “Returns”LWWRegister<V>