I’m trying to save a Yjs doc to a file and want to know if I’m doing this correctly. I’m using Yrs and was thinking to use “encodeStateAsUpdate” with an empty StateVector to store the doc as a list of u8s in a file.
let doc = Doc::new();
let text = doc.get_or_insert_text("test");
{
let mut txn = doc.transact_mut();
text.push(&mut txn, "hello world");
}
let encoded_doc = doc
.transact()
.encode_state_as_update_v1(&StateVector::default());
let new_doc = Doc::new();
{
let mut txn = new_doc.transact_mut();
let update = Update::decode_v1(&encoded_doc).unwrap();
txn.apply_update(update);
}
println!("New doc: {:?}", new_doc);
The output of this is
Doc { store: StoreRef(AtomicRefCell { value: 2249705684 { root types: {"test": YText(start: (<2249705684#0>))}, blocks: { 2249705684: [Item(<2249705684#0>, len: 11, parent: <root>: 'hello world')] } } }) }
New doc: Doc { store: StoreRef(AtomicRefCell { value: 3235410531 { root types: {"test": UnknownRef(start: (<2249705684#0>))}, blocks: { 2249705684: [Item(<2249705684#0>, len: 11, parent: <root>: 'hello world')] } } }) }
As you can see, the doc that is loaded from the encoded doc has a different type with “UnknownRef”
What is the correct way for me to do this?