feat: unbounded channel used as ui event

This commit is contained in:
sxyazi 2023-07-19 19:02:30 +08:00
parent 36b13ac611
commit f80a13a33b
No known key found for this signature in database
4 changed files with 18 additions and 20 deletions

View File

@ -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"

View File

@ -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<Sender<Event>> = None;
static mut TX: Option<UnboundedSender<Event>> = None;
pub enum Event {
Quit,
@ -36,7 +36,7 @@ pub enum Event {
impl Event {
#[inline]
pub fn init(tx: Sender<Event>) {
pub fn init(tx: UnboundedSender<Event>) {
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<T>(self, rx: oneshot::Receiver<T>) -> T {
let tx = unsafe { TX.as_ref().unwrap() };
tx.send(self).await.ok();
tx.send(self).ok();
rx.await.unwrap()
}
}

View File

@ -382,7 +382,7 @@ impl Scheduler {
}
pub(super) fn precache_mime(&self, targets: Vec<PathBuf>) {
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<PathBuf>) {
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<PathBuf>) {
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();

View File

@ -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<Event>,
pub rx: Receiver<Event>,
pub tx: UnboundedSender<Event>,
pub rx: UnboundedReceiver<Event>,
term_stop_tx: Option<oneshot::Sender<()>>,
term_stop_rx: Option<oneshot::Receiver<()>>,
@ -16,7 +16,7 @@ pub struct Signals {
impl Signals {
pub fn start() -> Result<Self> {
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;
}
}