61个版本
0.19.2 | 2024年7月17日 |
---|---|
0.18.8 | 2024年5月10日 |
0.18.2 | 2024年3月27日 |
0.17.2 | 2023年12月7日 |
0.0.1 | 2020年10月20日 |
#54 in 数据结构
12,862 每月下载量
在 18 个crate中使用 (15 个直接使用)
1MB
27K SLoC
Yrs
Yrs(读作:线)是Yjs框架的Rust版本。
这是一个用于基于冲突免费复制数据类型的协同文档编辑的库。这允许在客户端设备上提供共享文档编辑体验,而无需显式要求托管单个服务器 - CRDT可以自行解决潜在的更新冲突,无需中央权威机构 - 同时还提供一流的离线编辑功能,在未相互连接的情况下修改文档副本,然后在连接启用后自动同步。
此库包含Rust API,其他项目在此存储库中将使用该API
示例
use yrs::*;
fn main() {
let doc = Doc::new();
let text = doc.get_or_insert_text("name");
// every operation in Yrs happens in scope of a transaction
let mut txn = doc.transact_mut();
// append text to our collaborative document
text.push(&mut txn, "Hello from yrs!");
// add formatting section to part of the text
text.format(&mut txn, 11, 3, HashMap::from([
("link".into(), "https://github.com/y-crdt/y-crdt".into())
]));
// simulate update with remote peer
let remote_doc = Doc::new();
let remote_text = remote_doc.get_or_insert_text("name");
let mut remote_txn = remote_doc.transact_mut();
// in order to exchange data with other documents
// we first need to create a state vector
let state_vector = remote_txn.state_vector();
// now compute a differential update based on remote document's
// state vector
let bytes = txn.encode_diff_v1(&state_vector);
// both update and state vector are serializable, we can pass them
// over the wire now apply update to a remote document
let update = Update::decode_v1(&bytes).unwrap();
remote_txn.apply_update(update);
// display raw text (no attributes)
println!("{}", remote_text.get_string(&remote_txn));
// create sequence of text chunks with optional format attributes
let diff = remote_text.diff(&remote_txn, YChange::identity);
}
文档
功能
我们正在努力使功能与Yjs项目兼容。当前功能列表
- Yjs更新二进制格式(v1)。
- Yjs更新二进制格式(v2)。
- 支持状态向量、增量差异更新和合并。
- 对传入更新的订阅事件。
- 支持共享(CRDT)类型
- 文本
- 数组
- 映射
- XML数据类型(XmlFragment、XmlElement、XmlText、XmlHook)
- 子文档
- 特定数据类型的订阅事件
- 对Unicode代码点的跨平台支持
- 事务起源
- 撤销管理器
内部文档
Yrs实现了与Yjs相同的算法并使用相同的数据结构。我们希望获得更好的性能,因为我们能够手动管理内存。最终,Yrs可能会通过从这个包中生成wasm模块来取代Yjs模块。
在这个包中,我们希望使用更好的术语,并尝试使用新的数据结构来索引数据。
对文档的每次更改都会生成一小块信息,这些信息被整合到文档中。在Yrs中,我们称这些小块为块。将它们视为构建您的文档的小乐高积木。每个块都由一个唯一标识符标识,该标识符由客户端ID和序列号组成(参见:Lamport时钟)。当您收到重复的块时,您会丢弃它。在某些情况下,可以将块组合在一起形成更大的块。如果只需要组成块的一部分,则可以拆解块。
有关实现算法的更多信息,请参见此处
依赖关系
~1–2.2MB
~45K SLoC