crdt_lf_hive 0.5.0
crdt_lf_hive: ^0.5.0 copied to clipboard
Hive adapters for CRDT LF library objects, providing persistence for Change and Snapshot objects.
// The example prints its result, and it is not a library entry point.
// ignore_for_file: avoid_print
import 'package:crdt_lf/crdt_lf.dart';
import 'package:crdt_lf_hive/crdt_lf_hive.dart';
import 'package:hive/hive.dart';
Future<void> main() async {
const itemsToAdd = 3;
const dbLocation = './example/db';
const documentId = '784ff372-6f0a-4fe9-8e63-19b72fd18c23';
Hive.init(dbLocation);
CRDTHive.initialize();
final backend = await CRDTHive.open();
// Reads the boxes into a document and follows it: everything written from
// here on is stored without another line of code. The identity comes from
// the backend too, so every run is the same author.
final note = await backend.openDocument(documentId);
final list = CRDTListHandler<String>(note.document, 'list');
print('read back: ${list.value}');
final length = list.value.length;
for (var i = length; i < length + itemsToAdd; i++) {
list.insert(i, 'Item $i');
}
// Writes what is still waiting. Without it the process could end before the
// delayed write runs.
await note.persistence.dispose();
note.document.dispose();
print('now: ${list.value}');
print('documents in the database: ${(await backend.documentIds).length}');
await backend.close();
await CRDTHive.closeAllBoxes();
}