Use a write_count to determine how many writes left to flush

pull/2267/head
Blaž Hrastnik 2 years ago committed by Skyler Hawthorne
parent b0212b3611
commit b155e861ad

@ -657,6 +657,7 @@ pub struct Editor {
// https://stackoverflow.com/a/66875668 // https://stackoverflow.com/a/66875668
pub saves: HashMap<DocumentId, UnboundedSender<Once<DocumentSavedEventFuture>>>, pub saves: HashMap<DocumentId, UnboundedSender<Once<DocumentSavedEventFuture>>>,
pub save_queue: SelectAll<Flatten<UnboundedReceiverStream<Once<DocumentSavedEventFuture>>>>, pub save_queue: SelectAll<Flatten<UnboundedReceiverStream<Once<DocumentSavedEventFuture>>>>,
pub write_count: usize,
pub count: Option<std::num::NonZeroUsize>, pub count: Option<std::num::NonZeroUsize>,
pub selected_register: Option<char>, pub selected_register: Option<char>,
@ -761,6 +762,7 @@ impl Editor {
documents: BTreeMap::new(), documents: BTreeMap::new(),
saves: HashMap::new(), saves: HashMap::new(),
save_queue: SelectAll::new(), save_queue: SelectAll::new(),
write_count: 0,
count: None, count: None,
selected_register: None, selected_register: None,
macro_recording: None, macro_recording: None,
@ -1206,6 +1208,8 @@ impl Editor {
.send(stream::once(Box::pin(future))) .send(stream::once(Box::pin(future)))
.map_err(|err| anyhow!("failed to send save event: {}", err))?; .map_err(|err| anyhow!("failed to send save event: {}", err))?;
self.write_count += 1;
Ok(()) Ok(())
} }
@ -1332,6 +1336,7 @@ impl Editor {
biased; biased;
Some(event) = self.save_queue.next() => { Some(event) = self.save_queue.next() => {
self.write_count -= 1;
EditorEvent::DocumentSaved(event) EditorEvent::DocumentSaved(event)
} }
Some(config_event) = self.config_events.1.recv() => { Some(config_event) = self.config_events.1.recv() => {
@ -1350,7 +1355,8 @@ impl Editor {
} }
pub async fn flush_writes(&mut self) { pub async fn flush_writes(&mut self) {
while let Some(save_event) = self.save_queue.next().await { while self.write_count > 0 {
if let Some(save_event) = self.save_queue.next().await {
match &save_event { match &save_event {
Ok(event) => { Ok(event) => {
let doc = doc_mut!(self, &event.doc_id); let doc = doc_mut!(self, &event.doc_id);
@ -1361,6 +1367,9 @@ impl Editor {
} }
}; };
// TODO: if is_err: break? // TODO: if is_err: break?
self.write_count -= 1;
}
} }
} }
} }

Loading…
Cancel
Save