## Choosing a data type This is the decision that matters most, and it is worth two minutes. | Type | Concurrent edits resolve as | Reach for it when | |---|---|---| | `BcrdtText` | Both edits survive, each author's run stays contiguous | Anything a person types into at length | | `BcrdtArray` | Both insertions survive, in a stable order | Lists, ordered collections | | `BcrdtMap` | **One value wins**, deterministically but arbitrarily | Fields where a later value legitimately replaces an earlier one | | `BcrdtSet` | An add wins over a concurrent remove - if it added something (re-adding a present element is a no-op) and its entry is the one kept (concurrent adds of one element keep one entry) | Tags, membership, flags | | `BcrdtCounter` | **Every change adds up** - concurrent increments and decrements are all counted | Votes, likes, quantities, scores | | `BcrdtXmlFragment` | Children merge like array elements, attributes like map keys, text like `BcrdtText` | Structured documents - what a ProseMirror- or TipTap-style editor edits | A number in a `BcrdtMap` is a field, not a counter: two people who both read 5 and both write 6 end at 6, not 7. `BcrdtCounter` keeps each replica's changes under a key of its own, so `Add`, `Increment` and `Decrement` never conflict and the total is the sum of every replica's share - and undo takes back exactly this replica's changes: ```csharp BcrdtCounter likes = document.Map.EnsureCounter("likes"); // or document.Counter("likes") at the root likes.Increment(); long total = likes.Value; ``` A structured document - headings, paragraphs, lists, each with attributes and rich text - is a tree, and modelling it as markup in a string, or as maps of arrays, loses the merge that matters: two people editing different paragraphs, or the same one, must both keep their work. `BcrdtXmlFragment` is that tree, after Yjs's `Y.XmlFragment`: its children are `BcrdtXmlElement`s - a fixed name, attributes, children of their own - and `BcrdtXmlText`s, which are `BcrdtText` in every respect, marks included: ```csharp BcrdtXmlFragment body = document.XmlFragment("body"); // or document.Crdt.GetXmlFragment("body") var heading = new BcrdtXmlElement("h1"); // built detached, inserted in one transaction heading.SetAttribute("id", "intro"); heading.Push(new BcrdtXmlText("Hello")); body.Push(heading, new BcrdtXmlElement("p")); string markup = body.ToXmlString(); //