encode method
Encode to a JSON-compatible structure. encodeValue maps one element
value to a JSON-compatible form.
Implementation
Object encode(Object? Function(T) encodeValue) {
final rows = <Object?>[];
// Emit blocks in start-dot order so the encoding is canonical: the same
// converged state produces identical output regardless of the block
// insertion order (enables content-hashing / dedup of snapshots).
final ordered = _blocks.values.toList()
..sort((x, y) => x.start.compareTo(y.start));
for (final b in ordered) {
rows.add(<Object?>[
b.start.counter,
b.start.replica,
b.parent.counter,
b.parent.replica,
b.side == Side.right ? 1 : 0,
[for (final v in b.values) encodeValue(v)],
_encodeDeleted(b.deleted),
]);
}
return <String, Object?>{'v': 1, 'b': rows};
}