Expand description
§Loro
Make your JSON data collaborative and version-controlled
Loro is a high-performance CRDTs framework offering Rust, JavaScript and Swift APIs.
Designed for local-first software, it enables effortless collaboration in app states.
§Examples
§Map/List/Text
use loro::{LoroDoc, LoroList, LoroText, LoroValue, ToJson};
use serde_json::json;
let doc = LoroDoc::new();
let map = doc.get_map("map");
map.insert("key", "value").unwrap();
map.insert("true", true).unwrap();
map.insert("null", LoroValue::Null).unwrap();
map.insert("deleted", LoroValue::Null).unwrap();
map.delete("deleted").unwrap();
let list = map.insert_container("list", LoroList::new()).unwrap();
list.insert(0, "List").unwrap();
list.insert(1, 9).unwrap();
let text = map.insert_container("text", LoroText::new()).unwrap();
text.insert(0, "Hello world!").unwrap();
assert_eq!(
doc.get_deep_value().to_json_value(),
json!({
"map": {
"key": "value",
"true": true,
"null": null,
"list": ["List", 9],
"text": "Hello world!"
}
})
);
§Rich Text
use loro::{ExpandType, LoroDoc, ToJson};
use serde_json::json;
let doc = LoroDoc::new();
let text = doc.get_text("text");
text.insert(0, "Hello world!").unwrap();
text.mark(0..5, "bold", true).unwrap();
assert_eq!(
text.get_richtext_value().to_json_value(),
json!([
{ "insert": "Hello", "attributes": {"bold": true} },
{ "insert": " world!" },
])
);
text.unmark(3..5, "bold").unwrap();
assert_eq!(
text.get_richtext_value().to_json_value(),
json!([
{ "insert": "Hel", "attributes": {"bold": true} },
{ "insert": "lo world!" },
])
);
§Sync
use loro::{LoroDoc, ToJson, ExpandType};
use serde_json::json;
let doc = LoroDoc::new();
let text = doc.get_text("text");
text.insert(0, "Hello world!").unwrap();
let bytes = doc.export_from(&Default::default());
let doc_b = LoroDoc::new();
doc_b.import(&bytes).unwrap();
assert_eq!(doc.get_deep_value(), doc_b.get_deep_value());
let text_b = doc_b.get_text("text");
text_b
.mark(0..5, "bold", true)
.unwrap();
doc.import(&doc_b.export_from(&doc.oplog_vv())).unwrap();
assert_eq!(
text.get_richtext_value().to_json_value(),
json!([
{ "insert": "Hello", "attributes": {"bold": true} },
{ "insert": " world!" },
])
);
§Save
use loro::LoroDoc;
let doc = LoroDoc::new();
let text = doc.get_text("text");
text.insert(0, "123").unwrap();
let snapshot = doc.export_snapshot();
let new_doc = LoroDoc::new();
new_doc.import(&snapshot).unwrap();
assert_eq!(new_doc.get_deep_value(), doc.get_deep_value());
Re-exports§
pub use loro_kv_store as kv_store;
Modules§
Macros§
- loro_
value - A macro for creating
LoroValue
. It works just like thejson!
macro inserde_json
.
Structs§
- Change
Meta Change
is a grouped continuous operations that share the same id, timestamp, commit message.- Change
Modifier - Commit
Options - Options for configuring a commit operation.
- Configure
- Counter
Span - This struct supports reverse repr:
from
can be less thanto
. We need this because it’ll make merging deletions easier. - DocAnalysis
- First
Commit From Peer Payload - The payload of the first commit from a peer callback.
- Fractional
Index - Frontiers
NotIncluded - ID
- It’s the unique ID of an Op represented by PeerID and Counter.
- IdLp
- It’s the unique ID of an Op represented by PeerID and Lamport clock. It’s used to define the total order of Ops.
- IdSpan
- This struct supports reverse repr: CounterSpan’s from can be less than to. But we should use it conservatively. We need this because it’ll make merging deletions easier.
- ImVersion
Vector - Immutable version vector
- Import
Blob Metadata - Import
Status - Inner
Undo Manager - UndoManager is responsible for managing undo/redo from the current peer’s perspective.
- Internal
String - Json
Change - Json
Future OpWrapper - JsonOp
- Json
Schema - Loro
Binary Value - LoroDoc
LoroDoc
is the entry for the whole document. When it’s dropped, all the associated [Handler
]s will be invalidated.- Loro
List - LoroList container. It’s used to model array.
- Loro
List Value - LoroMap
- LoroMap container.
- Loro
MapValue - Loro
Movable List - LoroMovableList container
- Loro
String Value - Loro
Text - LoroText container. It’s used to model plaintext/richtext.
- Loro
Tree - LoroTree container. It’s used to model movable trees.
- Loro
Unknown - Unknown container.
- MemKv
Store - PreCommit
Callback Payload - The payload of the pre commit callback.
- Style
Config - Style
Config Map - Subscription
- A handle to a subscription created by GPUI. When dropped, the subscription is cancelled and the callback will no longer be invoked.
- Tree
Delta Item - The semantic action in movable tree.
- Tree
Diff - Tree
Diff Item - TreeID
- Each node of movable tree has a unique
TreeID
generated by Loro. - Tree
Node - A tree node in the LoroTree.
- Undo
Item Meta - The metadata of an undo item.
- Undo
Manager - UndoManager can be used to undo and redo the changes made to the document with a certain peer.
- Update
Options - Options for controlling the text update behavior.
- Version
Range - Version
Vector - VersionVector is a map from PeerID to Counter. Its a right-open interval.
- Version
Vector Diff
Enums§
- Cannot
Find Relative Position - Change
Travel Error - Container
- All the CRDT containers supported by Loro.
- ContainerID
- ContainerID includes the Op’s ID and the type. So it’s impossible to have the same ContainerID with conflict ContainerType.
- Container
Type - Encoded
Blob Mode - Event
Trigger Kind - The kind of the event trigger.
- Expand
Type - Whether to expand the style when inserting new text around it.
- Export
Mode - The mode of the export.
- Frontiers
- Frontiers representation.
- Index
- Json
Future Op - Json
List Op - Json
MapOp - Json
Movable List Op - Json
OpContent - Json
Text Op - Json
Tree Op - Loro
Encode Error - Loro
Error - Loro
Tree Error - Loro
Value - LoroValue is used to represents the state of CRDT at a given version.
- Text
Delta - Tree
External Diff - Tree
Parent Id - Undo
OrRedo - Update
Timeout Error - Value
OrContainer - It’s a type that can be either a value or a container.
Constants§
- LORO_
VERSION - The version of the loro crate
Traits§
- Apply
Diff - Container
Trait - The common trait for all the containers. It’s used internally, you can’t implement it directly.
- Into
Container Id - KvStore
- ToJson
Functions§
Type Aliases§
- Counter
- If it’s the nth Op of a peer, the counter will be n.
- First
Commit From Peer Callback - The callback of the first commit from a peer.
- Lamport
- It’s the Lamport clock
- Local
Update Callback - The callback of the local update.
- Loro
Result - OnPop
- OnPush
- When a undo/redo item is pushed, the undo manager will call the on_push callback to get the meta data of the undo item. The returned cursors will be recorded for a new pushed undo item.
- PeerID
- Unique id for each peer. It’s a random u64 by default.
- Peer
IdUpdate Callback - The callback of the peer id change. The second argument is the next counter for the peer.
- PreCommit
Callback - Timestamp