|
| 1 | +# Container |
| 2 | + |
| 3 | +Containers are the fundamental building blocks in Loro for organizing and structuring collaborative data. They provide typed data structures that automatically merge when concurrent edits occur. |
| 4 | + |
| 5 | +## Container Types |
| 6 | + |
| 7 | +Loro provides several container types, each optimized for different use cases: |
| 8 | + |
| 9 | +- **LoroMap**: Key-value pairs with Last-Write-Wins semantics |
| 10 | +- **LoroList**: Ordered sequences that merge concurrent insertions |
| 11 | +- **LoroText**: Text with character-level merging and rich text support |
| 12 | +- **LoroTree**: Hierarchical tree structures with move operations |
| 13 | +- **LoroMovableList**: Lists with reordering capabilities |
| 14 | +- **LoroCounter**: Numerical values with increment/decrement operations |
| 15 | + |
| 16 | +## Container States: Attached vs Detached |
| 17 | + |
| 18 | +Containers in Loro exist in two distinct states that affect their behavior and identity. |
| 19 | + |
| 20 | +### Detached Containers |
| 21 | + |
| 22 | +A container is **detached** when created directly using constructors: |
| 23 | + |
| 24 | +```ts twoslash |
| 25 | +import { LoroMap, LoroText, LoroList } from "loro-crdt"; |
| 26 | +// ---cut--- |
| 27 | +// These containers are all detached |
| 28 | +const map = new LoroMap(); |
| 29 | +const text = new LoroText(); |
| 30 | +const list = new LoroList(); |
| 31 | +``` |
| 32 | + |
| 33 | +Characteristics of detached containers: |
| 34 | +- Not yet part of any document |
| 35 | +- Have a default placeholder ContainerID |
| 36 | +- Can be used as templates or temporary data structures |
| 37 | +- Will get a proper ContainerID when inserted into a document |
| 38 | + |
| 39 | +### Attached Containers |
| 40 | + |
| 41 | +A container becomes **attached** when it's part of a document hierarchy: |
| 42 | + |
| 43 | +```ts twoslash |
| 44 | +import { LoroDoc, LoroMap, LoroText } from "loro-crdt"; |
| 45 | +// ---cut--- |
| 46 | +const doc = new LoroDoc(); |
| 47 | + |
| 48 | +// Root containers are immediately attached |
| 49 | +const rootMap = doc.getMap("myMap"); |
| 50 | +const rootText = doc.getText("myText"); |
| 51 | + |
| 52 | +// Child containers: the returned value is attached |
| 53 | +const detachedChild = new LoroText(); |
| 54 | +const attachedChild = rootMap.setContainer("child", detachedChild); |
| 55 | +// Note: detachedChild remains detached |
| 56 | +// attachedChild is the attached version with proper ContainerID |
| 57 | +``` |
| 58 | + |
| 59 | +Characteristics of attached containers: |
| 60 | +- Belong to a specific document |
| 61 | +- Have a proper ContainerID that uniquely identifies them |
| 62 | +- Changes are tracked in the document's history |
| 63 | +- Can be synchronized across peers |
| 64 | + |
| 65 | +## Container IDs |
| 66 | + |
| 67 | +Every attached container has a unique ContainerID that identifies it within the distributed system. The ID generation depends on the container type: |
| 68 | + |
| 69 | +- **Root containers**: ID derived from their name (e.g., "myMap" in `doc.getMap("myMap")`) |
| 70 | +- **Child containers**: ID based on the operation that created them (OpID) |
| 71 | + |
| 72 | +This deterministic ID generation ensures that: |
| 73 | +- The same container can be identified across all peers |
| 74 | +- Container IDs are not random but contextually determined |
| 75 | +- A detached container cannot have its final ID until insertion |
| 76 | + |
| 77 | +## Working with Containers |
| 78 | + |
| 79 | +### Creating Root Containers |
| 80 | + |
| 81 | +Root containers are created through the document API and are immediately attached: |
| 82 | + |
| 83 | +```ts twoslash |
| 84 | +import { LoroDoc } from "loro-crdt"; |
| 85 | +// ---cut--- |
| 86 | +const doc = new LoroDoc(); |
| 87 | + |
| 88 | +// These methods create or get root containers |
| 89 | +const map = doc.getMap("settings"); |
| 90 | +const text = doc.getText("content"); |
| 91 | +const list = doc.getList("items"); |
| 92 | +const tree = doc.getTree("hierarchy"); |
| 93 | +``` |
| 94 | + |
| 95 | +### Nesting Containers |
| 96 | + |
| 97 | +Containers can be nested to create complex data structures: |
| 98 | + |
| 99 | +```ts twoslash |
| 100 | +import { LoroDoc, LoroMap, LoroList, LoroText } from "loro-crdt"; |
| 101 | +// ---cut--- |
| 102 | +const doc = new LoroDoc(); |
| 103 | +const rootMap = doc.getMap("root"); |
| 104 | + |
| 105 | +// Method 1: Using setContainer (returns attached container) |
| 106 | +const childText = rootMap.setContainer("description", new LoroText()); |
| 107 | + |
| 108 | +// Method 2: Using insertContainer for lists |
| 109 | +const list = doc.getList("items"); |
| 110 | +const childMap = list.insertContainer(0, new LoroMap()); |
| 111 | +``` |
| 112 | + |
| 113 | +## Container Overwrites |
| 114 | + |
| 115 | +When initializing child containers in parallel, overwrites can occur instead of |
| 116 | +automatic merging. For example: |
| 117 | + |
| 118 | +```ts twoslash |
| 119 | +import { LoroDoc, LoroText } from "loro-crdt"; |
| 120 | +// ---cut--- |
| 121 | +const a: string = "hello"; |
| 122 | +const doc = new LoroDoc(); |
| 123 | +const map = doc.getMap("map"); |
| 124 | + |
| 125 | +// Parallel initialization of child containers |
| 126 | +const docB = doc.fork(); |
| 127 | +const textA = doc.getMap("map").setContainer("text", new LoroText()); |
| 128 | +textA.insert(0, "A"); |
| 129 | +const textB = docB.getMap("map").setContainer("text", new LoroText()); |
| 130 | +textB.insert(0, "B"); |
| 131 | + |
| 132 | +doc.import(docB.export({ mode: "update" })); |
| 133 | +// Result: Either { "meta": { "text": "A" } } or { "meta": { "text": "B" } } |
| 134 | +``` |
| 135 | + |
| 136 | +This behavior poses a significant risk of data loss if the editing history is |
| 137 | +not preserved. Even when the complete history is available and allows for data |
| 138 | +recovery, the recovery process can be complex. |
| 139 | + |
| 140 | +<aside> |
| 141 | +By default, Loro and Automerge preserve the whole editing history in a directed |
| 142 | +acyclic graph like Git. |
| 143 | +</aside> |
| 144 | + |
| 145 | +When a container holds substantial data or serves as the primary storage for |
| 146 | +document content, overwriting it can lead to the unintended hiding/loss of |
| 147 | +critical information. For this reason, it is essential to implement careful and |
| 148 | +systematic container initialization practices to prevent such issues. |
| 149 | + |
| 150 | +### Best Practices |
| 151 | + |
| 152 | +1. When containers might be initialized concurrently, prefer initializing them |
| 153 | + at the root level rather than as nested containers |
| 154 | + |
| 155 | +2. When using map containers: |
| 156 | + - If possible, initialize all child containers during the map container's |
| 157 | + initialization |
| 158 | + - Avoid concurrent creation of child containers with the same key in the map |
| 159 | + container to prevent overwrites |
| 160 | + |
| 161 | +The overwrite behavior occurs because parallel creation of child containers |
| 162 | +results in different container IDs, preventing automatic merging of their |
| 163 | +contents. |
| 164 | + |
| 165 | + |
| 166 | +## Related Concepts |
| 167 | + |
| 168 | +- [Container ID](/docs/advanced/cid): Deep dive into how Container IDs work |
| 169 | +- [Choosing CRDT Types](/docs/concepts/choose_crdt_type): Guide for selecting the right container type |
| 170 | +- [Composition](/docs/tutorial/composition): How to compose containers into complex structures |
0 commit comments