From f80a13a33b389a0371f714aaece0b1f7e10055c3 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Wed, 19 Jul 2023 19:02:30 +0800 Subject: [PATCH] feat: unbounded channel used as ui event --- Cargo.toml | 8 ++++---- src/core/event.rs | 12 +++++------- src/core/tasks/scheduler.rs | 6 +++--- src/ui/signals.rs | 12 ++++++------ 4 files changed, 18 insertions(+), 20 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7665ac93..ad29a8f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,7 +33,7 @@ trash = "^3" unicode-width = "^0" xdg = "^2" -# [profile.release] -# strip = true -# lto = true -# panic = "abort" +[profile.release] +strip = true +lto = true +panic = "abort" diff --git a/src/core/event.rs b/src/core/event.rs index a629c03a..e7f5153b 100644 --- a/src/core/event.rs +++ b/src/core/event.rs @@ -2,12 +2,12 @@ use std::{collections::BTreeMap, path::PathBuf}; use anyhow::Result; use crossterm::event::KeyEvent; -use tokio::sync::{mpsc::Sender, oneshot}; +use tokio::sync::{mpsc::UnboundedSender, oneshot}; use super::{files::FilesOp, input::InputOpt, manager::PreviewData, select::SelectOpt}; use crate::config::open::Opener; -static mut TX: Option> = None; +static mut TX: Option> = None; pub enum Event { Quit, @@ -36,7 +36,7 @@ pub enum Event { impl Event { #[inline] - pub fn init(tx: Sender) { + pub fn init(tx: UnboundedSender) { unsafe { TX.replace(tx); } @@ -45,14 +45,12 @@ impl Event { #[inline] pub fn emit(self) { let tx = unsafe { TX.as_ref().unwrap() }; - tokio::spawn(async { - tx.send(self).await.ok(); - }); + tx.send(self).ok(); } pub async fn wait(self, rx: oneshot::Receiver) -> T { let tx = unsafe { TX.as_ref().unwrap() }; - tx.send(self).await.ok(); + tx.send(self).ok(); rx.await.unwrap() } } diff --git a/src/core/tasks/scheduler.rs b/src/core/tasks/scheduler.rs index d0ce5655..3b8ca5bb 100644 --- a/src/core/tasks/scheduler.rs +++ b/src/core/tasks/scheduler.rs @@ -382,7 +382,7 @@ impl Scheduler { } pub(super) fn precache_mime(&self, targets: Vec) { - let name = format!("Mimetype"); + let name = format!("Preload mimetype for {} files", targets.len()); let id = self.running.write().add(name); let _ = self.todo.send_blocking({ @@ -395,14 +395,14 @@ impl Scheduler { } pub(super) fn precache_image(&self, targets: Vec) { - let name = format!("Image"); + let name = format!("Precache of {} image files", targets.len()); let id = self.running.write().add(name); self.precache.image(id, targets).ok(); } pub(super) fn precache_video(&self, targets: Vec) { - let name = format!("Video"); + let name = format!("Precache of {} video files", targets.len()); let id = self.running.write().add(name); self.precache.video(id, targets).ok(); diff --git a/src/ui/signals.rs b/src/ui/signals.rs index 46670b64..99b10974 100644 --- a/src/ui/signals.rs +++ b/src/ui/signals.rs @@ -2,13 +2,13 @@ use anyhow::Result; use crossterm::event::{Event as CrosstermEvent, EventStream}; use futures::StreamExt; use libc::{SIGHUP, SIGINT, SIGQUIT, SIGTERM}; -use tokio::{select, sync::{mpsc::{self, Receiver, Sender}, oneshot}, task::JoinHandle}; +use tokio::{select, sync::{mpsc::{self, UnboundedReceiver, UnboundedSender}, oneshot}, task::JoinHandle}; use crate::core::Event; pub struct Signals { - pub tx: Sender, - pub rx: Receiver, + pub tx: UnboundedSender, + pub rx: UnboundedReceiver, term_stop_tx: Option>, term_stop_rx: Option>, @@ -16,7 +16,7 @@ pub struct Signals { impl Signals { pub fn start() -> Result { - let (tx, rx) = mpsc::channel(500); + let (tx, rx) = mpsc::unbounded_channel(); let (term_tx, term_rx) = oneshot::channel(); let mut signals = @@ -37,7 +37,7 @@ impl Signals { while let Some(signal) = signals.next().await { match signal { SIGHUP | SIGTERM | SIGINT | SIGQUIT => { - if tx.send(Event::Quit).await.is_err() { + if tx.send(Event::Quit).is_err() { break; } } @@ -63,7 +63,7 @@ impl Signals { CrosstermEvent::Resize(cols, rows) => Event::Resize(cols, rows), _ => continue, }; - if tx.send(event).await.is_err() { + if tx.send(event).is_err() { break; } }