Hi all,
I’m working on a Yrs library in which I need to potentially update some underlying data in a map in response to the document being updated by other clients when the transaction doesn’t have some specific origin assigned. However, it doesn’t seem possible to obtain a transaction in the callback, presumably since the transaction that triggered the callback (and is passed as a reference to the callback as an argument) is still active.
Here’s a very basic example that demonstrates the problem:
use std::rc::Rc;
use yrs::{DeepObservable, Map, Transact};
fn main() {
let doc = Rc::new(yrs::Doc::new());
let map = Rc::new(doc.get_or_insert_map("test"));
let doc_clone = doc.clone();
let map_clone = map.clone();
let _sub = map.observe_deep(move |transaction, _events| {
let check_origin = yrs::Origin::from("test_origin");
if transaction.origin() == Some(&check_origin) {
return;
}
println!("waiting on txn");
let mut txn = doc_clone.transact_mut_with("test_origin");
println!("obtained txn");
map_clone.insert(&mut txn, "test2", "test2");
});
let mut txn = doc.transact_mut_with("some_other_origin");
map.insert(&mut txn, "test", "test");
drop(txn);
let txn = doc.transact();
let value = map.get(&txn, "test2");
println!("value: {:?}", value);
}
// outputs: waiting on txn
I’ve also experimented with the callbacks that get triggered later, such as observe_after_transaction
, observe_transaction_cleanup
, and observe_update_v1/v2
, with the same results.
What would be the proper way to handle such a situation?
Thanks in advance.