refactor: Replace postage with flume

This commit is contained in:
Michael Aaron Murphy 2022-03-27 16:02:25 +02:00 committed by Michael Murphy
parent 805bf6333e
commit e578f2d19c
11 changed files with 192 additions and 112 deletions

View file

@ -3,8 +3,6 @@
use futures_lite::*;
use pop_launcher::*;
use postage::mpsc;
use postage::prelude::{Sink, Stream};
use smol::process::{Child, ChildStdout, Command, Stdio};
use std::cell::Cell;
use std::io;
@ -17,10 +15,10 @@ enum Event {
}
pub async fn main() {
let (mut event_tx, mut event_rx) = mpsc::channel::<Event>(8);
let (event_tx, event_rx) = flume::bounded::<Event>(8);
// Channel for cancelling searches that are in progress.
let (interrupt_tx, interrupt_rx) = mpsc::channel::<()>(1);
let (interrupt_tx, interrupt_rx) = flume::bounded::<()>(1);
// Indicates if a search is being performed in the background.
let active = Rc::new(Cell::new(false));
@ -34,7 +32,7 @@ pub async fn main() {
// Manages the external process, tracks search results, and executes activate requests
let search_handler = async move {
while let Some(search) = event_rx.recv().await {
while let Ok(search) = event_rx.recv_async().await {
match search {
Event::Activate(id) => {
if let Some(selection) = app.search_results.get(id as usize) {
@ -62,7 +60,7 @@ pub async fn main() {
let request_handler = async move {
let interrupt = || {
let active = active.clone();
let mut tx = interrupt_tx.clone();
let tx = interrupt_tx.clone();
async move {
if active.get() {
tracing::debug!("sending interrupt");
@ -78,7 +76,7 @@ pub async fn main() {
Ok(request) => match request {
// Launch the default application with the selected file
Request::Activate(id) => {
event_tx.send(Event::Activate(id)).await?;
event_tx.send_async(Event::Activate(id)).await?;
}
// Interrupt any active searches being performed
@ -93,7 +91,7 @@ pub async fn main() {
None => &query,
};
event_tx.send(Event::Search(query.to_owned())).await?;
event_tx.send_async(Event::Search(query.to_owned())).await?;
active.set(true);
}
@ -106,7 +104,7 @@ pub async fn main() {
}
}
Ok::<(), postage::sink::SendError<Event>>(())
Ok::<(), flume::SendError<Event>>(())
};
let _ = future::zip(request_handler, search_handler).await;
@ -115,7 +113,7 @@ pub async fn main() {
/// Maintains state for search requests
struct SearchContext {
pub active: Rc<Cell<bool>>,
pub interrupt_rx: mpsc::Receiver<()>,
pub interrupt_rx: flume::Receiver<()>,
pub out: smol::Unblock<io::Stdout>,
pub search_results: Vec<PathBuf>,
}
@ -177,7 +175,7 @@ impl SearchContext {
'stream: loop {
let interrupt = async {
let _ = self.interrupt_rx.recv().await;
let _ = self.interrupt_rx.recv_async().await;
None
};

View file

@ -74,10 +74,9 @@ impl App {
let mut handles = Vec::new();
let mut sinks = pactl_sinks();
let sinks = pactl_sinks();
use postage::prelude::Stream;
while let Some(id) = sinks.recv().await {
while let Ok(id) = sinks.recv_async().await {
handles.push(smol::spawn(async move {
let args = &[arg1, id.as_str(), arg2];
let _ = command_spawn(cmd, args).await;
@ -134,8 +133,8 @@ async fn command_spawn(cmd: &str, args: &[&str]) -> io::Result<()> {
Ok(())
}
fn pactl_sinks() -> postage::mpsc::Receiver<String> {
let (mut tx, rx) = postage::mpsc::channel(4);
fn pactl_sinks() -> flume::Receiver<String> {
let (tx, rx) = flume::bounded(4);
smol::spawn(async move {
let child = smol::process::Command::new("pactl")
@ -149,8 +148,7 @@ fn pactl_sinks() -> postage::mpsc::Receiver<String> {
let mut lines = futures_lite::io::BufReader::new(stdout).lines();
while let Some(Ok(line)) = lines.next().await {
if let Some(stripped) = line.strip_prefix("Sink #") {
use postage::prelude::Sink;
let _ = tx.send(stripped.trim().to_owned()).await;
let _ = tx.send_async(stripped.trim().to_owned()).await;
}
}
}

View file

@ -4,9 +4,8 @@
use crate::*;
use pop_launcher::*;
use flume::Sender;
use futures_lite::{AsyncBufReadExt, StreamExt};
use postage::mpsc::Sender;
use postage::prelude::*;
use smol::process::{Command, Stdio};
use std::collections::VecDeque;
use std::{
@ -69,7 +68,7 @@ impl App {
}
async fn reload(&mut self) {
let (tx, mut rx) = postage::mpsc::channel::<ScriptInfo>(8);
let (tx, rx) = flume::bounded::<ScriptInfo>(8);
let mut queue = VecDeque::new();
@ -88,7 +87,7 @@ impl App {
};
let script_receiver = async {
'outer: while let Some(script) = rx.recv().await {
'outer: while let Ok(script) = rx.recv_async().await {
tracing::debug!("appending script: {:?}", script);
for cached_script in &self.scripts {
if cached_script.name == script.name {
@ -149,7 +148,7 @@ struct ScriptInfo {
async fn load_from(path: &Path, paths: &mut VecDeque<PathBuf>, tx: Sender<ScriptInfo>) {
if let Ok(directory) = path.read_dir() {
for entry in directory.filter_map(Result::ok) {
let mut tx = tx.clone();
let tx = tx.clone();
let path = entry.path();
if path.is_dir() {
@ -200,7 +199,7 @@ async fn load_from(path: &Path, paths: &mut VecDeque<PathBuf>, tx: Sender<Script
}
}
let _ = tx.send(info).await;
let _ = tx.send_async(info).await;
})
.detach();
}

View file

@ -197,8 +197,7 @@ async fn fetch_favicon(url: &str, favicon_path: &Path, client: &HttpClient) -> O
let content_type = response
.headers()
.get(CONTENT_TYPE)
.map(|header| header.to_str().ok())
.flatten()
.and_then(|header| header.to_str().ok())
.unwrap();
if !ALLOWED_FAVICON_MIME.contains(&content_type) {