crdt_lf_drift 0.1.0+2
crdt_lf_drift: ^0.1.0+2 copied to clipboard
drift storage adapter for CRDT LF library objects, providing persistence for Change and Snapshot objects.
// ignore_for_file: avoid_print just for example
import 'dart:io' as io;
import 'package:crdt_lf/crdt_lf.dart';
import 'package:crdt_lf_drift/crdt_lf_drift.dart';
Future<void> main() async {
const changesToInsert = 3;
final documentId = PeerId.parse('784ff372-6f0a-4fe9-8e63-19b72fd18c23');
// Resolve the database next to this script so the example works regardless
// of the current working directory.
final dbLocation =
'${io.File.fromUri(io.Platform.script).parent.path}/crdt_example.db';
final storage = CRDTDrift.open(io.File(dbLocation));
final changeStorage = storage.changeStorageForDocument(
documentId.toString(),
);
final changesToSave = <Change>[];
final document = CRDTDocument(peerId: documentId)
..importChanges(await changeStorage.getChanges())
..localChanges.listen(changesToSave.add);
final list = CRDTListHandler<String>(document, 'list');
final length = list.value.length;
for (var i = length; i < length + changesToInsert; i++) {
list.insert(i, 'Item $i');
}
print('${document.exportChanges().length} changes ');
print('${list.value}');
await Future<void>.delayed(const Duration(seconds: 1));
print('${changesToSave.length} changes to save');
await changeStorage.saveChanges(changesToSave);
print('changes saved');
await storage.close();
print('database closed');
}