fix up for 8e8c5fa never showing file tree

This commit is contained in:
extrawurst 2022-11-22 10:55:25 +01:00
parent 8e8c5fad55
commit bea70306e2
2 changed files with 52 additions and 17 deletions

View File

@ -6,9 +6,17 @@ use crate::{
}; };
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
///
pub struct FileTreeResult {
///
pub commit: CommitId,
///
pub result: Result<Vec<TreeFile>>,
}
enum JobState { enum JobState {
Request { commit: CommitId, repo: RepoPath }, Request { commit: CommitId, repo: RepoPath },
Response(Result<Vec<TreeFile>>), Response(FileTreeResult),
} }
/// ///
@ -30,7 +38,7 @@ impl AsyncTreeFilesJob {
} }
/// ///
pub fn result(&self) -> Option<Result<Vec<TreeFile>>> { pub fn result(&self) -> Option<FileTreeResult> {
if let Ok(mut state) = self.state.lock() { if let Ok(mut state) = self.state.lock() {
if let Some(state) = state.take() { if let Some(state) = state.take() {
return match state { return match state {
@ -60,7 +68,10 @@ impl AsyncJob for AsyncTreeFilesJob {
std::thread::sleep( std::thread::sleep(
std::time::Duration::from_secs(2), std::time::Duration::from_secs(2),
); );
JobState::Response(files) JobState::Response(FileTreeResult {
commit,
result: files,
})
} }
JobState::Response(result) => { JobState::Response(result) => {
JobState::Response(result) JobState::Response(result)

View File

@ -99,10 +99,7 @@ impl RevisionFilesComponent {
if !same_id { if !same_id {
self.files = None; self.files = None;
self.async_treefiles.spawn(AsyncTreeFilesJob::new( self.request_files(commit);
self.repo.borrow().clone(),
commit,
));
self.revision = self.revision =
Some(get_commit_info(&self.repo.borrow(), &commit)?); Some(get_commit_info(&self.repo.borrow(), &commit)?);
@ -129,17 +126,36 @@ impl RevisionFilesComponent {
ev, ev,
AsyncNotification::Git(AsyncGitNotification::TreeFiles) AsyncNotification::Git(AsyncGitNotification::TreeFiles)
) { ) {
if let Some(last) = self.async_treefiles.take_last() { self.refresh_files()?;
if let Some(Ok(last)) = last.result() { }
let filenames: Vec<&Path> = last
.iter()
.map(|f| f.path.as_path())
.collect();
self.tree =
FileTree::new(&filenames, &BTreeSet::new())?;
self.tree.collapse_but_root();
self.files = Some(last); Ok(())
}
fn refresh_files(&mut self) -> Result<(), anyhow::Error> {
if let Some(last) = self.async_treefiles.take_last() {
if let Some(result) = last.result() {
if self
.revision
.as_ref()
.map(|commit| commit.id == result.commit)
.unwrap_or_default()
{
if let Ok(last) = result.result {
let filenames: Vec<&Path> = last
.iter()
.map(|f| f.path.as_path())
.collect();
self.tree = FileTree::new(
&filenames,
&BTreeSet::new(),
)?;
self.tree.collapse_but_root();
self.files = Some(last);
}
} else if let Some(rev) = &self.revision {
self.request_files(rev.id);
} }
} }
} }
@ -364,6 +380,13 @@ impl RevisionFilesComponent {
title title
} }
fn request_files(&mut self, commit: CommitId) {
self.async_treefiles.spawn(AsyncTreeFilesJob::new(
self.repo.borrow().clone(),
commit,
));
}
} }
impl DrawableComponent for RevisionFilesComponent { impl DrawableComponent for RevisionFilesComponent {
@ -514,6 +537,7 @@ impl Component for RevisionFilesComponent {
fn show(&mut self) -> Result<()> { fn show(&mut self) -> Result<()> {
self.visible = true; self.visible = true;
self.refresh_files()?;
Ok(()) Ok(())
} }
} }