Skip to main content

A todo list with containers: merged per field

Each todo is a small tree: a text and a done flag, in a list that peers can reorder at the same time. One peer can tick a todo while another edits its text, and both edits survive. This is model B of Which handler do you need?. The same list as an aggregate is the aggregate recipe.

A todo list with containers: merged per fieldSource ↗

A Flutter todo list built with container handlers: each todo is a small map of handlers, a collaborative text and a done flag. Two peers can edit the text and check the same todo at the same time, or move it, and both edits are kept.

Flutter nativeFlutter web✓ tested in CI
import 'package:crdt_lf/crdt_lf.dart';

/// The kind of the `done` flag. A generic handler needs a tag: write it once.
final doneKind = CRDTRegisterHandler.spec<bool>('todo.done');

/// The todo list of [document]: a movable list with one map per todo.
CRDTMovableListRefHandler todosOf(CRDTDocument document) {
// The kinds this peer must rebuild when the todos of another peer arrive.
document
..register(CRDTMapRefHandler.spec)
..register(CRDTFugueTextHandler.spec)
..register(doneKind);
return document.handler(CRDTMovableListRefHandler.spec, 'todos');
}

/// Adds a todo that reads [text] at the end of [todos].
CRDTMapRefHandler addTodo(CRDTMovableListRefHandler todos, {String text = ''}) {
// One transaction: the other peers get the todo with both its fields.
return todos.doc.runInTransaction(() {
final todo = todos.insertChild(
todos.childRefs().length,
CRDTMapRefHandler.spec,
);
final title = todo.child('text', CRDTFugueTextHandler.spec);
if (text.isNotEmpty) {
title.insert(0, text);
}
todo.child('done', doneKind).set(false);
return todo;
});
}

/// The fields of one todo, each merged on its own.
extension TodoFields on CRDTMapRefHandler {
/// The text of the todo: merged character by character.
CRDTFugueTextHandler get text => child('text', CRDTFugueTextHandler.spec);

/// Whether the todo is done: the last write wins.
CRDTRegisterHandler<bool> get done => child('done', doneKind);
}

Key points

  • Each todo is a CRDTMapRefHandler with a text and a register: a change to one field leaves the other field as it is.
  • A CRDTMovableListRefHandler keeps the identity of a todo when two peers move it at the same time: it is never duplicated.
  • Register the kinds of the children (register), so a peer rebuilds the todos it receives from another peer.
  • The builder of the list follows the list alone: it rebuilds when a todo is added, removed or moved. Each tile follows its own handlers, so an edit of one todo rebuilds nothing else.
  • The page shows one tile per reference of the list, so a tile index is the index removeChildAt and move expect.
  • ReorderableListView.onReorderItem is recent in Flutter. On an older Flutter, use onReorder and lower to by one when it is after from.
  • The document lives in memory. To keep it on the device, open it from a storage backend, as in the flutter-note recipe and on the Persistence page.