DocsTutorialComposing CRDTs

Composing CRDTs

In Loro, you can build complex data structures using basic CRDTs such as List, MovableList, Map and Tree. These containers can include sub-containers, which in turn can contain more sub-containers, allowing for the composition of intricate data structures.

It’s important to note that documents in Loro must adhere to a tree structure. This means that while a parent can have multiple children, each child is restricted to only one parent. Therefore, the document forms a tree rather than a graph (like a DAG).

By leveraging these fundamental CRDTs, you can effectively model the states and the updates of documents that conform to the JSON schema.

const  = new ();
const  = .("map");
let  = 0;
// Events from a child are propagated to all ancestor nodes.
.(() => {
  .();
  ++;
});
 
// Create a sub container for map
// { map: { list: [] } }
const  = .("list", new ());
.(0);
.(1);
 
// Create a sub container for list
// { map: { list: [0, 1, LoroText] } }
const  = .(2, new ());
(.()).({ : { : [0, 1, ""] } });
{
  // Commit will trigger the event, because list is a sub container of map
  .();
  await new (() => (, 1));
  ().(1);
}
 
.(0, "Hello, ");
.(7, "World!");
(.()).({ : { : [0, 1, "Hello, World!"] } });
{
  // Commit will trigger the event, because text is a descendant of map
  .();
  await new (() => (, 1));
  ().(2);
}