Skip to main content

crdt_lf 5.4.0 copy "crdt_lf: ^5.4.0" to clipboard
crdt_lf: ^5.4.0 copied to clipboard

Conflict-free replicated data type (CRDT) - Local-first implementation provided in dart

example/main.dart

// Copyright 2025 Mattia Pispisa
// SPDX-License-Identifier: MIT

// A copy of a recipe, written by `melos run recipes_sync`.
// Edit the recipe, then run it again:
// packages/_internal/crdt_lf_recipes/lib/two_peers/main.dart

import 'dart:io';

import 'package:crdt_lf/crdt_lf.dart';

/// Lets two peers edit one text at the same time, and returns what each reads.
(String, String) mergeTwoPeers() {
  final alice = CRDTDocument();
  final bob = CRDTDocument();

  // The same handler id on both peers: it is the same text.
  final aliceText = CRDTFugueTextHandler(alice, 'note');
  final bobText = CRDTFugueTextHandler(bob, 'note');

  aliceText.insert(0, 'Hello world');
  bob.importChanges(alice.exportChanges()); // Bob now reads "Hello world"

  // Both edit at the same time, with no connection.
  aliceText.insert(0, 'Big ');
  bobText.insert(11, '!');

  // They meet again and exchange what the other is missing.
  bob.importChanges(alice.exportChanges());
  alice.importChanges(bob.exportChanges());

  stdout
    ..writeln(aliceText.value) // Big Hello world!
    ..writeln(bobText.value); // Big Hello world!

  final result = (aliceText.value, bobText.value);
  alice.dispose();
  bob.dispose();
  return result;
}

void main() {
  mergeTwoPeers();
}
8
likes
160
points
1.43k
downloads

Documentation

API reference

Publisher

verified publishermattiapispisa.it

Weekly Downloads

Conflict-free replicated data type (CRDT) - Local-first implementation provided in dart

Homepage
Repository (GitHub)
View/report issues
Contributing

Topics

#crdt #local-first #fugue #hlc

License

MIT (license)

Dependencies

crypto, hlc_dart

More

Packages that depend on crdt_lf