fix: discard empty textchanges, drain range

This commit is contained in:
əlemi 2023-11-30 03:04:26 +01:00
parent 3881bb38ea
commit 3197c083bf

View file

@ -112,43 +112,46 @@ impl ControllerWorker<TextChange> for BufferControllerWorker {
res = self.operations.recv() => match res { res = self.operations.recv() => match res {
None => break, None => break,
Some(change) => { Some(change) => {
match self.buffer.view().get(change.span.clone()) { if !change.is_empty() {
None => tracing::error!("received illegal span from client"), let view = self.buffer.view();
Some(span) => { match view.get(change.span.clone()) {
let diff = TextDiff::from_chars(span, &change.content); None => tracing::error!("received illegal span from client: {:?} but buffer is of len {}", change.span, view.len()),
Some(span) => {
let diff = TextDiff::from_chars(span, &change.content);
let mut i = 0; let mut i = 0;
let mut ops = Vec::new(); let mut ops = Vec::new();
for diff in diff.iter_all_changes() { for diff in diff.iter_all_changes() {
match diff.tag() { match diff.tag() {
ChangeTag::Equal => i += 1, ChangeTag::Equal => i += 1,
ChangeTag::Delete => match self.buffer.delete(change.span.start + i) { ChangeTag::Delete => match self.buffer.delete(change.span.start + i) {
Ok(op) => ops.push(op), Ok(op) => ops.push(op),
Err(e) => tracing::error!("could not apply deletion: {}", e), Err(e) => tracing::error!("could not apply deletion: {}", e),
}, },
ChangeTag::Insert => { ChangeTag::Insert => {
for c in diff.value().chars() { for c in diff.value().chars() {
match self.buffer.insert(change.span.start + i, c) { match self.buffer.insert(change.span.start + i, c) {
Ok(op) => { Ok(op) => {
ops.push(op); ops.push(op);
i += 1; i += 1;
}, },
Err(e) => tracing::error!("could not apply insertion: {}", e), Err(e) => tracing::error!("could not apply insertion: {}", e),
}
} }
} },
}, }
} }
}
for op in ops { for op in ops {
match self.send_op(&mut tx, &op).await { match self.send_op(&mut tx, &op).await {
Err(e) => tracing::error!("server refused to broadcast {}: {}", op, e), Err(e) => tracing::error!("server refused to broadcast {}: {}", op, e),
Ok(()) => { Ok(()) => {
self.content.send(self.buffer.view()).unwrap_or_warn("could not send buffer update"); self.content.send(self.buffer.view()).unwrap_or_warn("could not send buffer update");
}, },
}
} }
} },
}, }
} }
} }
}, },
@ -161,7 +164,7 @@ impl ControllerWorker<TextChange> for BufferControllerWorker {
Ok(op) => { Ok(op) => {
self.buffer.merge(op); self.buffer.merge(op);
self.content.send(self.buffer.view()).unwrap_or_warn("could not send buffer update"); self.content.send(self.buffer.view()).unwrap_or_warn("could not send buffer update");
for tx in self.pollers.drain(0..self.pollers.len()) { for tx in self.pollers.drain(..) {
tx.send(()).unwrap_or_warn("could not wake up poller"); tx.send(()).unwrap_or_warn("could not wake up poller");
} }
}, },