mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
feat: unbounded channel used as ui event
This commit is contained in:
parent
36b13ac611
commit
f80a13a33b
4 changed files with 18 additions and 20 deletions
|
|
@ -33,7 +33,7 @@ trash = "^3"
|
||||||
unicode-width = "^0"
|
unicode-width = "^0"
|
||||||
xdg = "^2"
|
xdg = "^2"
|
||||||
|
|
||||||
# [profile.release]
|
[profile.release]
|
||||||
# strip = true
|
strip = true
|
||||||
# lto = true
|
lto = true
|
||||||
# panic = "abort"
|
panic = "abort"
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,12 @@ use std::{collections::BTreeMap, path::PathBuf};
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use crossterm::event::KeyEvent;
|
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 super::{files::FilesOp, input::InputOpt, manager::PreviewData, select::SelectOpt};
|
||||||
use crate::config::open::Opener;
|
use crate::config::open::Opener;
|
||||||
|
|
||||||
static mut TX: Option<Sender<Event>> = None;
|
static mut TX: Option<UnboundedSender<Event>> = None;
|
||||||
|
|
||||||
pub enum Event {
|
pub enum Event {
|
||||||
Quit,
|
Quit,
|
||||||
|
|
@ -36,7 +36,7 @@ pub enum Event {
|
||||||
|
|
||||||
impl Event {
|
impl Event {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn init(tx: Sender<Event>) {
|
pub fn init(tx: UnboundedSender<Event>) {
|
||||||
unsafe {
|
unsafe {
|
||||||
TX.replace(tx);
|
TX.replace(tx);
|
||||||
}
|
}
|
||||||
|
|
@ -45,14 +45,12 @@ impl Event {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn emit(self) {
|
pub fn emit(self) {
|
||||||
let tx = unsafe { TX.as_ref().unwrap() };
|
let tx = unsafe { TX.as_ref().unwrap() };
|
||||||
tokio::spawn(async {
|
tx.send(self).ok();
|
||||||
tx.send(self).await.ok();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn wait<T>(self, rx: oneshot::Receiver<T>) -> T {
|
pub async fn wait<T>(self, rx: oneshot::Receiver<T>) -> T {
|
||||||
let tx = unsafe { TX.as_ref().unwrap() };
|
let tx = unsafe { TX.as_ref().unwrap() };
|
||||||
tx.send(self).await.ok();
|
tx.send(self).ok();
|
||||||
rx.await.unwrap()
|
rx.await.unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -382,7 +382,7 @@ impl Scheduler {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn precache_mime(&self, targets: Vec<PathBuf>) {
|
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 id = self.running.write().add(name);
|
||||||
|
|
||||||
let _ = self.todo.send_blocking({
|
let _ = self.todo.send_blocking({
|
||||||
|
|
@ -395,14 +395,14 @@ impl Scheduler {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn precache_image(&self, targets: Vec<PathBuf>) {
|
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);
|
let id = self.running.write().add(name);
|
||||||
|
|
||||||
self.precache.image(id, targets).ok();
|
self.precache.image(id, targets).ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn precache_video(&self, targets: Vec<PathBuf>) {
|
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);
|
let id = self.running.write().add(name);
|
||||||
|
|
||||||
self.precache.video(id, targets).ok();
|
self.precache.video(id, targets).ok();
|
||||||
|
|
|
||||||
|
|
@ -2,13 +2,13 @@ use anyhow::Result;
|
||||||
use crossterm::event::{Event as CrosstermEvent, EventStream};
|
use crossterm::event::{Event as CrosstermEvent, EventStream};
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
use libc::{SIGHUP, SIGINT, SIGQUIT, SIGTERM};
|
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;
|
use crate::core::Event;
|
||||||
|
|
||||||
pub struct Signals {
|
pub struct Signals {
|
||||||
pub tx: Sender<Event>,
|
pub tx: UnboundedSender<Event>,
|
||||||
pub rx: Receiver<Event>,
|
pub rx: UnboundedReceiver<Event>,
|
||||||
|
|
||||||
term_stop_tx: Option<oneshot::Sender<()>>,
|
term_stop_tx: Option<oneshot::Sender<()>>,
|
||||||
term_stop_rx: Option<oneshot::Receiver<()>>,
|
term_stop_rx: Option<oneshot::Receiver<()>>,
|
||||||
|
|
@ -16,7 +16,7 @@ pub struct Signals {
|
||||||
|
|
||||||
impl Signals {
|
impl Signals {
|
||||||
pub fn start() -> Result<Self> {
|
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 (term_tx, term_rx) = oneshot::channel();
|
||||||
|
|
||||||
let mut signals =
|
let mut signals =
|
||||||
|
|
@ -37,7 +37,7 @@ impl Signals {
|
||||||
while let Some(signal) = signals.next().await {
|
while let Some(signal) = signals.next().await {
|
||||||
match signal {
|
match signal {
|
||||||
SIGHUP | SIGTERM | SIGINT | SIGQUIT => {
|
SIGHUP | SIGTERM | SIGINT | SIGQUIT => {
|
||||||
if tx.send(Event::Quit).await.is_err() {
|
if tx.send(Event::Quit).is_err() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -63,7 +63,7 @@ impl Signals {
|
||||||
CrosstermEvent::Resize(cols, rows) => Event::Resize(cols, rows),
|
CrosstermEvent::Resize(cols, rows) => Event::Resize(cols, rows),
|
||||||
_ => continue,
|
_ => continue,
|
||||||
};
|
};
|
||||||
if tx.send(event).await.is_err() {
|
if tx.send(event).is_err() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue