From 0954f9c58c5f7d5e87c7cdcecfcbb3c751adb17f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E9=9B=85=20=C2=B7=20Misaki=20Masa?= Date: Fri, 9 Aug 2024 11:38:47 +0800 Subject: [PATCH] fix: directory loading status (#1439) --- yazi-core/src/tab/preview.rs | 49 +++++++++++------------ yazi-fm/src/lives/folder.rs | 11 +---- yazi-plugin/preset/components/current.lua | 2 +- yazi-plugin/preset/plugins/folder.lua | 2 +- 4 files changed, 27 insertions(+), 37 deletions(-) diff --git a/yazi-core/src/tab/preview.rs b/yazi-core/src/tab/preview.rs index 5db41225..5fad116b 100644 --- a/yazi-core/src/tab/preview.rs +++ b/yazi-core/src/tab/preview.rs @@ -15,12 +15,12 @@ pub struct Preview { pub skip: usize, previewer_ct: Option, - folder_loader: Option<(Url, JoinHandle<()>)>, + folder_loader: Option>, } impl Preview { pub fn go(&mut self, file: File, mime: &str, force: bool) { - if !force && self.content_unchanged(&file.url, &file.cha) { + if !force && self.content_unchanged(&file.url, file.cha) { return; } @@ -37,34 +37,31 @@ impl Preview { } } - pub fn go_folder(&mut self, file: File, check: Option, force: bool) { - if !force && self.content_unchanged(&file.url, &file.cha) { + pub fn go_folder(&mut self, file: File, dir: Option, force: bool) { + let (cha, url) = (file.cha, file.url()); + self.go(file, MIME_DIR, force); + + if self.content_unchanged(&url, cha) { return; } - let url = file.url(); - self.go(file, MIME_DIR, force); + self.folder_loader.take().map(|h| h.abort()); + self.folder_loader = Some(tokio::spawn(async move { + let Some(new) = Files::assert_stale(&url, dir.unwrap_or(Cha::dummy())).await else { + return; + }; + let Ok(rx) = Files::from_dir(&url).await else { return }; - self.folder_loader.take().map(|(_, h)| h.abort()); - self.folder_loader = Some(( - url.clone(), - tokio::spawn(async move { - let Some(cha) = Files::assert_stale(&url, check.unwrap_or(Cha::dummy())).await else { - return; - }; - let Ok(rx) = Files::from_dir(&url).await else { return }; + let stream = + UnboundedReceiverStream::new(rx).chunks_timeout(50000, Duration::from_millis(500)); + pin!(stream); - let stream = - UnboundedReceiverStream::new(rx).chunks_timeout(50000, Duration::from_millis(500)); - pin!(stream); - - let ticket = FilesOp::prepare(&url); - while let Some(chunk) = stream.next().await { - FilesOp::Part(url.clone(), chunk, ticket).emit(); - } - FilesOp::Done(url, cha, ticket).emit(); - }), - )); + let ticket = FilesOp::prepare(&url); + while let Some(chunk) = stream.next().await { + FilesOp::Part(url.clone(), chunk, ticket).emit(); + } + FilesOp::Done(url, new, ticket).emit(); + })); } #[inline] @@ -92,7 +89,7 @@ impl Preview { } #[inline] - fn content_unchanged(&self, url: &Url, cha: &Cha) -> bool { + fn content_unchanged(&self, url: &Url, cha: Cha) -> bool { match &self.lock { Some(l) => *url == l.url && self.skip == l.skip && cha.hits(l.cha), None => false, diff --git a/yazi-fm/src/lives/folder.rs b/yazi-fm/src/lives/folder.rs index b12bdf80..bbe3f8f5 100644 --- a/yazi-fm/src/lives/folder.rs +++ b/yazi-fm/src/lives/folder.rs @@ -1,6 +1,6 @@ use std::ops::{Deref, Range}; -use mlua::{AnyUserData, Lua, MetaMethod, UserDataFields, UserDataMethods}; +use mlua::{AnyUserData, Lua, UserDataFields}; use yazi_config::LAYOUT; use yazi_plugin::{bindings::Cast, url::Url}; @@ -51,14 +51,7 @@ impl Folder { })?; lua.register_userdata_type::(|reg| { - reg.add_meta_method(MetaMethod::ToString, |lua, me, ()| { - use yazi_fs::FolderStage::{Failed, Loaded, Loading}; - lua.create_string(match me { - Loading => "loading", - Loaded => "loaded", - Failed(_) => "failed", - }) - }); + reg.add_field_method_get("is_loading", |_, me| Ok(*me == yazi_fs::FolderStage::Loading)); })?; Ok(()) diff --git a/yazi-plugin/preset/components/current.lua b/yazi-plugin/preset/components/current.lua index e4d30883..1e5add7d 100644 --- a/yazi-plugin/preset/components/current.lua +++ b/yazi-plugin/preset/components/current.lua @@ -15,7 +15,7 @@ function Current:empty() if self._folder.files.filter then line = ui.Line("No filter results") else - line = ui.Line(self._folder.stage == "loading" and "Loading..." or "No items") + line = ui.Line(self._folder.stage.is_loading and "Loading..." or "No items") end return { diff --git a/yazi-plugin/preset/plugins/folder.lua b/yazi-plugin/preset/plugins/folder.lua index 3f2064d3..14913eee 100644 --- a/yazi-plugin/preset/plugins/folder.lua +++ b/yazi-plugin/preset/plugins/folder.lua @@ -13,7 +13,7 @@ function M:peek() if #folder.files == 0 then return ya.preview_widgets(self, { - ui.Paragraph(self.area, { ui.Line(folder.stage == "loading" and "Loading..." or "No items") }) + ui.Paragraph(self.area, { ui.Line(folder.stage.is_loading and "Loading..." or "No items") }) :align(ui.Paragraph.CENTER), }) end