use rustc_hash::FxHashMap;
use yrs::StateVector;
use yrs::Update;
use yrs::block::ClientID;
#[derive(Default)]
pub struct Reorder {
held: Vec<Update>,
}
impl std::fmt::Debug for Reorder {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Reorder")
.field("held", &self.held.len())
.finish()
}
}
impl Reorder {
pub fn hold(&mut self, update: Update) {
self.held.push(update);
}
#[must_use]
pub fn take_next(&mut self, state: &StateVector) -> Option<Update> {
let reach = clocks(state);
let index = self
.held
.iter()
.position(|update| is_ready(update, &reach))?;
Some(self.held.swap_remove(index))
}
#[must_use]
pub fn is_waiting(&self) -> bool {
!self.held.is_empty()
}
}
fn clocks(state: &StateVector) -> FxHashMap<ClientID, u32> {
state
.iter()
.map(|(&client, &clock)| (client, clock))
.collect()
}
fn is_ready(update: &Update, reach: &FxHashMap<ClientID, u32>) -> bool {
let known = |client: &ClientID| reach.get(client).copied().unwrap_or(0);
let continues = update
.state_vector_lower()
.iter()
.all(|(client, &clock)| clock <= known(client));
if !continues {
return false;
}
let mut after = reach.clone();
extend(&mut after, update);
update.delete_set().iter().all(|(client, ranges)| {
let known = after.get(client).copied().unwrap_or(0);
ranges.iter().all(|range| range.end <= known)
})
}
fn extend(reach: &mut FxHashMap<ClientID, u32>, update: &Update) {
for (client, ranges) in update.insertions(true).iter() {
if let Some(end) = ranges.iter().map(|range| range.end).max() {
let clock = reach.entry(*client).or_default();
*clock = (*clock).max(end);
}
}
}
#[cfg(test)]
mod tests {
use yrs::Doc;
use yrs::GetString;
use yrs::ReadTxn;
use yrs::Text;
use yrs::Transact;
use yrs::updates::decoder::Decode;
use super::*;
fn insert(doc: &Doc, index: u32, chunk: &str) -> Vec<u8> {
let text = doc.get_or_insert_text("text");
let mut txn = doc.transact_mut();
text.insert(&mut txn, index, chunk);
txn.encode_update_v1()
}
fn decode(update: &[u8]) -> Update {
Update::decode_v1(update).expect("decodes")
}
fn apply_ready(reorder: &mut Reorder, doc: &Doc) {
let mut txn = doc.transact_mut();
while let Some(update) = reorder.take_next(&txn.state_vector()) {
txn.apply_update(update).expect("applies");
}
}
#[test]
fn an_update_yrs_keeps_pending_holds_back_its_clients_later_updates() {
let alice = Doc::with_client_id(1);
let typed = insert(&alice, 0, "x");
let bob = Doc::with_client_id(2);
bob.transact_mut()
.apply_update(decode(&typed))
.expect("applies");
let first = insert(&bob, 1, "y");
let second = insert(&bob, 2, "z");
let carol = Doc::with_client_id(3);
let mut reorder = Reorder::default();
reorder.hold(decode(&second));
reorder.hold(decode(&first));
apply_ready(&mut reorder, &carol);
assert!(
reorder.is_waiting(),
"bob's second update waits while his first is pending in yrs"
);
reorder.hold(decode(&typed));
apply_ready(&mut reorder, &carol);
assert!(!reorder.is_waiting());
let text = carol.get_or_insert_text("text");
let txn = carol.transact();
assert!(txn.store().pending_update().is_none());
assert_eq!(text.get_string(&txn), "xyz");
}
}