refactor: remove extra layer of sync
This commit is contained in:
parent
8f0721f00a
commit
f54fdef099
2 changed files with 10 additions and 28 deletions
|
@ -1,4 +1,4 @@
|
|||
use std::time::Duration;
|
||||
use std::{mem, time::Duration};
|
||||
|
||||
use helix_event::register_hook;
|
||||
use helix_vcs::FileBlame;
|
||||
|
@ -8,13 +8,13 @@ use helix_view::{
|
|||
handlers::{BlameEvent, Handlers},
|
||||
DocumentId,
|
||||
};
|
||||
use tokio::{sync::oneshot, time::Instant};
|
||||
use tokio::time::Instant;
|
||||
|
||||
use crate::job;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct BlameHandler {
|
||||
worker: Option<oneshot::Receiver<anyhow::Result<FileBlame>>>,
|
||||
file_blame: Option<anyhow::Result<FileBlame>>,
|
||||
doc_id: DocumentId,
|
||||
show_blame_for_line_in_statusline: Option<u32>,
|
||||
}
|
||||
|
@ -27,36 +27,18 @@ impl helix_event::AsyncHook for BlameHandler {
|
|||
event: Self::Event,
|
||||
_timeout: Option<tokio::time::Instant>,
|
||||
) -> Option<tokio::time::Instant> {
|
||||
if let Some(worker) = &mut self.worker {
|
||||
if worker.try_recv().is_ok() {
|
||||
self.finish_debounce();
|
||||
return None;
|
||||
}
|
||||
}
|
||||
|
||||
self.doc_id = event.doc_id;
|
||||
self.show_blame_for_line_in_statusline = event.line;
|
||||
let (tx, rx) = oneshot::channel();
|
||||
|
||||
tokio::spawn(async move {
|
||||
let result = FileBlame::try_new(event.path);
|
||||
let _ = tx.send(result);
|
||||
});
|
||||
|
||||
self.worker = Some(rx);
|
||||
|
||||
self.file_blame = Some(FileBlame::try_new(event.path));
|
||||
Some(Instant::now() + Duration::from_millis(50))
|
||||
}
|
||||
|
||||
fn finish_debounce(&mut self) {
|
||||
let doc_id = self.doc_id;
|
||||
let line_blame = self.show_blame_for_line_in_statusline;
|
||||
if let Some(worker) = self.worker.take() {
|
||||
let result = mem::take(&mut self.file_blame);
|
||||
if let Some(result) = result {
|
||||
tokio::spawn(async move {
|
||||
let Ok(result) = worker.await else {
|
||||
return;
|
||||
};
|
||||
|
||||
job::dispatch(move |editor, _| {
|
||||
let Some(doc) = editor.document_mut(doc_id) else {
|
||||
return;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use anyhow::Context as _;
|
||||
use anyhow::Result;
|
||||
use std::cell::RefCell;
|
||||
use parking_lot::Mutex;
|
||||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
|
||||
|
@ -24,7 +24,7 @@ enum LineBlameUnit {
|
|||
#[derive(Debug)]
|
||||
pub struct FileBlame {
|
||||
/// A map from line numbers to blame for that line
|
||||
blame: RefCell<HashMap<u32, LineBlameUnit>>,
|
||||
blame: Mutex<HashMap<u32, LineBlameUnit>>,
|
||||
/// The owning repository for this file's `ObjectId`s
|
||||
repo: gix::ThreadSafeRepository,
|
||||
}
|
||||
|
@ -47,7 +47,7 @@ impl FileBlame {
|
|||
let blame_line = line.saturating_sub(inserted_lines) + removed_lines;
|
||||
let repo = self.repo.to_thread_local();
|
||||
|
||||
let mut blame = self.blame.borrow_mut();
|
||||
let mut blame = self.blame.lock();
|
||||
let line_blame_unit = blame.get_mut(&blame_line);
|
||||
|
||||
let commit = match line_blame_unit {
|
||||
|
@ -120,7 +120,7 @@ impl FileBlame {
|
|||
.entries;
|
||||
|
||||
Ok(Self {
|
||||
blame: RefCell::new(
|
||||
blame: Mutex::new(
|
||||
file_blame
|
||||
.into_iter()
|
||||
.flat_map(|blame| {
|
||||
|
|
Loading…
Add table
Reference in a new issue