Skip to content

Collaborative list

A shared to-do list, held as a BcrdtArray whose elements are each a BcrdtMap with a title and a done flag. Two independent replicas of the document demo-list are shown; add, rename, tick and reorder in either one.

Two replicas of one list

Add an item in either replica, rename it, tick it, move it up or down - and watch the other follow. A rename is sent when the title field loses focus or you press Enter; everything else is sent at once. The footer counts the items, the ticked items and the engine's storage blocks.

Opening two replicas…

Worth trying. Take Replica B offline. In A, rename an item. In B, tick the same item and add a new one. Bring B back: the rename and the tick both survive on the same item, and B's new item is now in A as well. Then try the reorder arrows on both replicas while one is offline. Moving an item removes it and inserts a copy of it at the new position, so two people moving the same item concurrently can produce two copies of it, and a rename or tick made concurrently with somebody else's move of that item is lost, because it lands on the element the move removed. Those are genuine limitations of the operation rather than bugs, and they are called out in the library's own documentation for Move.

Why a list of maps

The nesting is the point. If each item were stored as a single string or a serialized blob, then one person renaming an item while another ticks it would be two writes of the same value, and one of them would have to lose. Because an item is a real collaborative map, the rename touches the title key and the tick touches the done key, they never contend, and both survive. Two people editing the same key of the same item still resolves last-writer-wins - the merge is per key, not per keystroke.

Ordering is handled by the sequence itself. Concurrent insertions at the same position are placed in an order every replica independently agrees on, and a run inserted by one person stays contiguous rather than interleaving with a run inserted by someone else. Concurrent deletion of the same item is idempotent: it disappears once and never comes back.

The same list in code

Everything the buttons above do is one call on a BcrdtArray or on one of its BcrdtMap elements. A new item is built detached - a BcrdtMap not yet in any document - and its writes are replayed when it joins, so the element and both of its keys reach the other replica as one update rather than three.

BcrdtArray items = document.Array("items");

var item = new BcrdtMap();                  // detached until it is inserted
item.Set("title", "Buy milk");
item.Set("done", false);
items.Push(item);                         // one update: the element and both keys

items.GetMap(0)?.Set("done", true);        // a tick writes one key of one item
items.Move(0, 2);                         // delete, then insert a copy where it goes
items.Delete(1);

items.ObserveDeep((events, transaction) => { /* any change, nested ones included */ });

Two habits keep a list like this correct in the browser. Key every rendered row by its item (@key="item"): remote insertions and deletions shift positions, and a row reused for a different item would send a half-typed title to the wrong one. And read Move's destination as a position in the list before the element is lifted out - one step down is Move(index, index + 2).

An application with view models rather than markup can project the same array through BcrdtListBinding<T>, an IReadOnlyList<T> that raises INotifyCollectionChanged as the list changes - locally or remotely.

An unhandled error has occurred. Reload 🗙