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

162
Cargo.lock generated
View file

@ -200,15 +200,6 @@ dependencies = [
"system-deps", "system-deps",
] ]
[[package]]
name = "atomic"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b88d82667eca772c4aa12f0f1348b3ae643424c8876448f3f7bd5787032e234c"
dependencies = [
"autocfg",
]
[[package]] [[package]]
name = "atomic-waker" name = "atomic-waker"
version = "1.0.0" version = "1.0.0"
@ -253,6 +244,12 @@ dependencies = [
"once_cell", "once_cell",
] ]
[[package]]
name = "bumpalo"
version = "3.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4a45a46ab1f2412e53d3a0ade76ffad2025804294569aae387231a0cd6e0899"
[[package]] [[package]]
name = "byteorder" name = "byteorder"
version = "1.4.3" version = "1.4.3"
@ -357,16 +354,6 @@ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]]
name = "crossbeam-queue"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4dd435b205a4842da59efd07628f921c096bc1cc0a156835b4fa0bcb9a19bcce"
dependencies = [
"cfg-if",
"crossbeam-utils",
]
[[package]] [[package]]
name = "crossbeam-utils" name = "crossbeam-utils"
version = "0.8.7" version = "0.8.7"
@ -544,6 +531,19 @@ dependencies = [
"rustc_version", "rustc_version",
] ]
[[package]]
name = "flume"
version = "0.10.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "843c03199d0c0ca54bc1ea90ac0d507274c28abcc4f691ae8b4eaa375087c76a"
dependencies = [
"futures-core",
"futures-sink",
"nanorand",
"pin-project 1.0.10",
"spin",
]
[[package]] [[package]]
name = "fnv" name = "fnv"
version = "1.0.7" version = "1.0.7"
@ -782,8 +782,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d39cd93900197114fa1fcb7ae84ca742095eed9442088988ae74fa744e930e77" checksum = "d39cd93900197114fa1fcb7ae84ca742095eed9442088988ae74fa744e930e77"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"js-sys",
"libc", "libc",
"wasi", "wasi",
"wasm-bindgen",
] ]
[[package]] [[package]]
@ -1050,6 +1052,15 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35" checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35"
[[package]]
name = "js-sys"
version = "0.3.56"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a38fc24e30fd564ce974c02bf1d337caddff65be6cc4735a1f7eab22a7440f04"
dependencies = [
"wasm-bindgen",
]
[[package]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "1.4.0" version = "1.4.0"
@ -1097,6 +1108,15 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "lock_api"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "88943dd7ef4a2e5a4bfa2753aaab3013e34ce2533d1996fb18ef591e315e2b3b"
dependencies = [
"scopeguard",
]
[[package]] [[package]]
name = "log" name = "log"
version = "0.4.14" version = "0.4.14"
@ -1151,6 +1171,15 @@ version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d"
[[package]]
name = "nanorand"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a51313c5820b0b02bd422f4b44776fbf47961755c74ce64afc73bfad10226c3"
dependencies = [
"getrandom",
]
[[package]] [[package]]
name = "new_mime_guess" name = "new_mime_guess"
version = "4.0.1" version = "4.0.1"
@ -1371,12 +1400,6 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "pollster"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5da3b0203fd7ee5720aa0b5e790b591aa5d3f41c3ed2c34a3a393382198af2f7"
[[package]] [[package]]
name = "pop-launcher" name = "pop-launcher"
version = "1.1.1" version = "1.1.1"
@ -1406,6 +1429,7 @@ version = "1.1.1"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"async-pidfd", "async-pidfd",
"flume",
"fork", "fork",
"freedesktop-desktop-entry", "freedesktop-desktop-entry",
"futures-lite", "futures-lite",
@ -1415,7 +1439,6 @@ dependencies = [
"isahc", "isahc",
"new_mime_guess", "new_mime_guess",
"pop-launcher", "pop-launcher",
"postage",
"regex", "regex",
"ron", "ron",
"serde", "serde",
@ -1442,6 +1465,7 @@ dependencies = [
"async-oneshot", "async-oneshot",
"async-process", "async-process",
"async-trait", "async-trait",
"flume",
"futures", "futures",
"futures-core", "futures-core",
"futures-lite", "futures-lite",
@ -1449,7 +1473,6 @@ dependencies = [
"gen-z", "gen-z",
"num_cpus", "num_cpus",
"pop-launcher", "pop-launcher",
"postage",
"regex", "regex",
"ron", "ron",
"serde", "serde",
@ -1463,22 +1486,6 @@ dependencies = [
"tracing-subscriber", "tracing-subscriber",
] ]
[[package]]
name = "postage"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a63d25391d04a097954b76aba742b6b5b74f213dfe3dbaeeb36e8ddc1c657f0b"
dependencies = [
"atomic",
"crossbeam-queue",
"futures",
"log",
"pin-project 1.0.10",
"pollster",
"static_assertions",
"thiserror",
]
[[package]] [[package]]
name = "ppv-lite86" name = "ppv-lite86"
version = "0.2.16" version = "0.2.16"
@ -1654,6 +1661,12 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "scopeguard"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]] [[package]]
name = "semver" name = "semver"
version = "0.11.0" version = "0.11.0"
@ -1831,6 +1844,15 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "spin"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "511254be0c5bcf062b019a6c89c01a664aa359ded62f78aa72c6fc137c0590e5"
dependencies = [
"lock_api",
]
[[package]] [[package]]
name = "static_assertions" name = "static_assertions"
version = "1.1.0" version = "1.1.0"
@ -2104,6 +2126,60 @@ version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
[[package]]
name = "wasm-bindgen"
version = "0.2.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25f1af7423d8588a3d840681122e72e6a24ddbcb3f0ec385cac0d12d24256c06"
dependencies = [
"cfg-if",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b21c0df030f5a177f3cba22e9bc4322695ec43e7257d865302900290bcdedca"
dependencies = [
"bumpalo",
"lazy_static",
"log",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f4203d69e40a52ee523b2529a773d5ffc1dc0071801c87b3d270b471b80ed01"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bfa8a30d46208db204854cadbb5d4baf5fcf8071ba5bf48190c3e59937962ebc"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d958d035c4438e28c70e4321a2911302f10135ce78a9c7834c0cab4123d06a2"
[[package]] [[package]]
name = "wepoll-ffi" name = "wepoll-ffi"
version = "0.1.2" version = "0.1.2"

View file

@ -16,7 +16,6 @@ human_format = "1.0.3"
human-sort = "0.2.2" human-sort = "0.2.2"
new_mime_guess = "4.0.1" new_mime_guess = "4.0.1"
pop-launcher = { path = "../" } pop-launcher = { path = "../" }
postage = { version = "0.4.1", features = ["futures-traits"] }
regex = "1.5.5" regex = "1.5.5"
ron = "0.7.0" ron = "0.7.0"
serde = "1.0.136" serde = "1.0.136"
@ -34,3 +33,4 @@ isahc = "1.7.0"
url = "2.2.2" url = "2.2.2"
sysfs-class = "0.1.3" sysfs-class = "0.1.3"
anyhow = "1.0.56" anyhow = "1.0.56"
flume = "0.10.12"

View file

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

View file

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

View file

@ -4,9 +4,8 @@
use crate::*; use crate::*;
use pop_launcher::*; use pop_launcher::*;
use flume::Sender;
use futures_lite::{AsyncBufReadExt, StreamExt}; use futures_lite::{AsyncBufReadExt, StreamExt};
use postage::mpsc::Sender;
use postage::prelude::*;
use smol::process::{Command, Stdio}; use smol::process::{Command, Stdio};
use std::collections::VecDeque; use std::collections::VecDeque;
use std::{ use std::{
@ -69,7 +68,7 @@ impl App {
} }
async fn reload(&mut self) { 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(); let mut queue = VecDeque::new();
@ -88,7 +87,7 @@ impl App {
}; };
let script_receiver = async { 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); tracing::debug!("appending script: {:?}", script);
for cached_script in &self.scripts { for cached_script in &self.scripts {
if cached_script.name == script.name { 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>) { async fn load_from(path: &Path, paths: &mut VecDeque<PathBuf>, tx: Sender<ScriptInfo>) {
if let Ok(directory) = path.read_dir() { if let Ok(directory) = path.read_dir() {
for entry in directory.filter_map(Result::ok) { for entry in directory.filter_map(Result::ok) {
let mut tx = tx.clone(); let tx = tx.clone();
let path = entry.path(); let path = entry.path();
if path.is_dir() { 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(); .detach();
} }

View file

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

View file

@ -15,7 +15,6 @@ futures-lite = "1.12.0"
gen-z = "0.1.0" gen-z = "0.1.0"
num_cpus = "1.13.1" num_cpus = "1.13.1"
pop-launcher = { path = "../" } pop-launcher = { path = "../" }
postage = { version = "0.4.1", features = ["futures-traits"] }
regex = "1.5.4" regex = "1.5.4"
ron = "0.7.0" ron = "0.7.0"
serde = { version = "1.0.136", features = ["derive"] } serde = { version = "1.0.136", features = ["derive"] }
@ -29,3 +28,4 @@ tracing = "0.1.31"
tracing-subscriber = { version = "0.3.9", features = ["fmt"] } tracing-subscriber = { version = "0.3.9", features = ["fmt"] }
futures-core = "0.3.21" futures-core = "0.3.21"
async-process = "1.3.0" async-process = "1.3.0"
flume = "0.10.12"

View file

@ -7,11 +7,11 @@ mod plugins;
pub use client::*; pub use client::*;
use crate::plugins::*; use crate::plugins::*;
use flume::{Receiver, Sender};
use futures::SinkExt; use futures::SinkExt;
use futures_core::Stream; use futures_core::Stream;
use futures_lite::{future, StreamExt}; use futures_lite::{future, StreamExt};
use pop_launcher::*; use pop_launcher::*;
use postage::mpsc;
use regex::Regex; use regex::Regex;
use slab::Slab; use slab::Slab;
use std::{ use std::{
@ -44,17 +44,17 @@ pub async fn main() {
} }
}); });
let (output_tx, mut output_rx) = mpsc::channel(16); let (output_tx, output_rx) = flume::bounded(16);
// Service will operate for as long as it is being awaited // Service will operate for as long as it is being awaited
let service = Service::new(output_tx).exec(input_stream); let service = Service::new(output_tx.into_sink()).exec(input_stream);
// Responses from the service will be streamed to stdout // Responses from the service will be streamed to stdout
let responder = async move { let responder = async move {
let stdout = io::stdout(); let stdout = io::stdout();
let stdout = &mut stdout.lock(); let stdout = &mut stdout.lock();
while let Some(response) = output_rx.next().await { while let Ok(response) = output_rx.recv_async().await {
serialize_out(stdout, &response); serialize_out(stdout, &response);
} }
}; };
@ -88,7 +88,7 @@ impl<O: futures::Sink<Response> + Unpin> Service<O> {
} }
pub async fn exec(mut self, input: impl Stream<Item = Request>) { pub async fn exec(mut self, input: impl Stream<Item = Request>) {
let (service_tx, service_rx) = mpsc::channel(1); let (service_tx, service_rx) = flume::bounded(1);
let stream = plugins::external::load::from_paths(); let stream = plugins::external::load::from_paths();
futures_lite::pin!(stream); futures_lite::pin!(stream);
@ -124,8 +124,8 @@ impl<O: futures::Sink<Response> + Unpin> Service<O> {
future::or(f1, f2).await; future::or(f1, f2).await;
} }
async fn response_handler(&mut self, mut service_rx: mpsc::Receiver<Event>) { async fn response_handler(&mut self, service_rx: Receiver<Event>) {
while let Some(event) = service_rx.next().await { while let Ok(event) = service_rx.recv_async().await {
match event { match event {
Event::Request(request) => { Event::Request(request) => {
match request { match request {
@ -144,7 +144,7 @@ impl<O: futures::Sink<Response> + Unpin> Service<O> {
Request::Exit => { Request::Exit => {
for (_key, plugin) in self.plugins.iter_mut() { for (_key, plugin) in self.plugins.iter_mut() {
let tx = plugin.sender_exec(); let tx = plugin.sender_exec();
let _ = tx.send(Request::Exit).await; let _ = tx.send_async(Request::Exit).await;
} }
break; break;
@ -199,12 +199,9 @@ impl<O: futures::Sink<Response> + Unpin> Service<O> {
} }
} }
fn register_plugin< fn register_plugin<P: Plugin, I: Fn(usize, Sender<Event>) -> P + Send + Sync + 'static>(
P: Plugin,
I: Fn(usize, mpsc::Sender<Event>) -> P + Send + Sync + 'static,
>(
&mut self, &mut self,
service_tx: mpsc::Sender<Event>, service_tx: Sender<Event>,
config: PluginConfig, config: PluginConfig,
regex: Option<regex::Regex>, regex: Option<regex::Regex>,
init: I, init: I,
@ -225,7 +222,7 @@ impl<O: futures::Sink<Response> + Unpin> Service<O> {
regex, regex,
isolate_with, isolate_with,
Box::new(move || { Box::new(move || {
let (request_tx, request_rx) = mpsc::channel(8); let (request_tx, request_rx) = flume::bounded(8);
let init = init.clone(); let init = init.clone();
let service_tx = service_tx.clone(); let service_tx = service_tx.clone();
@ -241,7 +238,10 @@ impl<O: futures::Sink<Response> + Unpin> Service<O> {
async fn activate(&mut self, id: Indice) { async fn activate(&mut self, id: Indice) {
if let Some((plugin, meta)) = self.search_result(id as usize) { if let Some((plugin, meta)) = self.search_result(id as usize) {
let _ = plugin.sender_exec().send(Request::Activate(meta.id)).await; let _ = plugin
.sender_exec()
.send_async(Request::Activate(meta.id))
.await;
} }
} }
@ -249,7 +249,7 @@ impl<O: futures::Sink<Response> + Unpin> Service<O> {
if let Some((plugin, meta)) = self.search_result(id as usize) { if let Some((plugin, meta)) = self.search_result(id as usize) {
let _ = plugin let _ = plugin
.sender_exec() .sender_exec()
.send(Request::ActivateContext { .send_async(Request::ActivateContext {
id: meta.id, id: meta.id,
context, context,
}) })
@ -278,13 +278,19 @@ impl<O: futures::Sink<Response> + Unpin> Service<O> {
async fn complete(&mut self, id: Indice) { async fn complete(&mut self, id: Indice) {
if let Some((plugin, meta)) = self.search_result(id as usize) { if let Some((plugin, meta)) = self.search_result(id as usize) {
let _ = plugin.sender_exec().send(Request::Complete(meta.id)).await; let _ = plugin
.sender_exec()
.send_async(Request::Complete(meta.id))
.await;
} }
} }
async fn context(&mut self, id: Indice) { async fn context(&mut self, id: Indice) {
if let Some((plugin, meta)) = self.search_result(id as usize) { if let Some((plugin, meta)) = self.search_result(id as usize) {
let _ = plugin.sender_exec().send(Request::Context(meta.id)).await; let _ = plugin
.sender_exec()
.send_async(Request::Context(meta.id))
.await;
} }
} }
@ -311,14 +317,17 @@ impl<O: futures::Sink<Response> + Unpin> Service<O> {
async fn interrupt(&mut self) { async fn interrupt(&mut self) {
for (_, plugin) in self.plugins.iter_mut() { for (_, plugin) in self.plugins.iter_mut() {
if let Some(sender) = plugin.sender.as_mut() { if let Some(sender) = plugin.sender.as_mut() {
let _ = sender.send(Request::Interrupt).await; let _ = sender.send_async(Request::Interrupt).await;
} }
} }
} }
async fn quit(&mut self, id: Indice) { async fn quit(&mut self, id: Indice) {
if let Some((plugin, meta)) = self.search_result(id as usize) { if let Some((plugin, meta)) = self.search_result(id as usize) {
let _ = plugin.sender_exec().send(Request::Quit(meta.id)).await; let _ = plugin
.sender_exec()
.send_async(Request::Quit(meta.id))
.await;
} }
} }
@ -383,7 +392,7 @@ impl<O: futures::Sink<Response> + Unpin> Service<O> {
if let Some(plugin) = self.plugins.get_mut(isolated) { if let Some(plugin) = self.plugins.get_mut(isolated) {
if plugin if plugin
.sender_exec() .sender_exec()
.send(Request::Search(query.to_owned())) .send_async(Request::Search(query.to_owned()))
.await .await
.is_ok() .is_ok()
{ {
@ -396,7 +405,7 @@ impl<O: futures::Sink<Response> + Unpin> Service<O> {
if let Some(plugin) = self.plugins.get_mut(plugin_id) { if let Some(plugin) = self.plugins.get_mut(plugin_id) {
if plugin if plugin
.sender_exec() .sender_exec()
.send(Request::Search(query.to_owned())) .send_async(Request::Search(query.to_owned()))
.await .await
.is_ok() .is_ok()
{ {
@ -562,7 +571,7 @@ impl<O: futures::Sink<Response> + Unpin> Service<O> {
} }
/// Handles Requests received from a frontend /// Handles Requests received from a frontend
async fn request_handler(input: impl Stream<Item = Request>, mut tx: mpsc::Sender<Event>) { async fn request_handler(input: impl Stream<Item = Request>, tx: Sender<Event>) {
let mut requested_to_exit = false; let mut requested_to_exit = false;
futures_lite::pin!(input); futures_lite::pin!(input);
@ -572,7 +581,7 @@ async fn request_handler(input: impl Stream<Item = Request>, mut tx: mpsc::Sende
requested_to_exit = true requested_to_exit = true
} }
let _ = tx.send(Event::Request(request)).await; let _ = tx.send_async(Event::Request(request)).await;
if requested_to_exit { if requested_to_exit {
break; break;

View file

@ -14,9 +14,8 @@ use std::{
use crate::{Event, Indice, Plugin, PluginResponse, Request}; use crate::{Event, Indice, Plugin, PluginResponse, Request};
use async_oneshot::oneshot; use async_oneshot::oneshot;
use flume::Sender;
use futures_lite::{AsyncWriteExt, FutureExt, StreamExt}; use futures_lite::{AsyncWriteExt, FutureExt, StreamExt};
use postage::mpsc::Sender;
use postage::prelude::*;
use smol::{ use smol::{
process::{Child, Command, Stdio}, process::{Child, Command, Stdio},
Task, Task,
@ -70,13 +69,13 @@ impl ExternalPlugin {
let detached = self.detached.clone(); let detached = self.detached.clone();
let searching = self.searching.clone(); let searching = self.searching.clone();
let (trip_tx, trip_rx) = oneshot::<()>(); let (trip_tx, trip_rx) = oneshot::<()>();
let mut tx = self.tx.clone(); let tx = self.tx.clone();
let name = self.name().to_owned(); let name = self.name().to_owned();
let id = self.id; let id = self.id;
// Spawn a background task to forward JSON responses from the child process. // Spawn a background task to forward JSON responses from the child process.
let task = smol::spawn(async move { let task = smol::spawn(async move {
let mut tx_ = tx.clone(); let tx_ = tx.clone();
let searching_ = searching.clone(); let searching_ = searching.clone();
let name_ = name.clone(); let name_ = name.clone();
@ -91,7 +90,7 @@ impl ExternalPlugin {
searching_.store(false, Ordering::SeqCst); searching_.store(false, Ordering::SeqCst);
} }
let _ = tx_.send(Event::Response((id, response))).await; let _ = tx_.send_async(Event::Response((id, response))).await;
} }
Err(why) => { Err(why) => {
tracing::error!("{}: serde error: {:?}", name_, why); tracing::error!("{}: serde error: {:?}", name_, why);
@ -111,7 +110,7 @@ impl ExternalPlugin {
// Ensure that a task that was searching sends a finished signal if it dies. // Ensure that a task that was searching sends a finished signal if it dies.
if searching.swap(false, Ordering::SeqCst) { if searching.swap(false, Ordering::SeqCst) {
let _ = tx let _ = tx
.send(Event::Response((id, PluginResponse::Finished))) .send_async(Event::Response((id, PluginResponse::Finished)))
.await; .await;
} }
@ -207,7 +206,7 @@ impl Plugin for ExternalPlugin {
} else { } else {
let _ = self let _ = self
.tx .tx
.send(Event::Response((self.id, PluginResponse::Finished))) .send_async(Event::Response((self.id, PluginResponse::Finished)))
.await; .await;
} }
} }

View file

@ -2,8 +2,8 @@
// SPDX-License-Identifier: MPL-2.0 // SPDX-License-Identifier: MPL-2.0
use crate::*; use crate::*;
use flume::Sender;
use pop_launcher::*; use pop_launcher::*;
use postage::mpsc::Sender;
use slab::Slab; use slab::Slab;
use std::borrow::Cow; use std::borrow::Cow;
@ -41,7 +41,7 @@ impl HelpPlugin {
async fn reload(&mut self) { async fn reload(&mut self) {
let (tx, rx) = async_oneshot::oneshot(); let (tx, rx) = async_oneshot::oneshot();
let _ = self.tx.send(Event::Help(tx)).await; let _ = self.tx.send_async(Event::Help(tx)).await;
self.details = rx.await.expect("internal error fetching help info"); self.details = rx.await.expect("internal error fetching help info");
} }
} }
@ -53,7 +53,7 @@ impl Plugin for HelpPlugin {
if let Some(help) = detail.help.as_ref() { if let Some(help) = detail.help.as_ref() {
let _ = self let _ = self
.tx .tx
.send(Event::Response(( .send_async(Event::Response((
self.id, self.id,
PluginResponse::Fill(help.clone()), PluginResponse::Fill(help.clone()),
))) )))
@ -91,13 +91,16 @@ impl Plugin for HelpPlugin {
..Default::default() ..Default::default()
}); });
let _ = self.tx.send(Event::Response((self.id, response))).await; let _ = self
.tx
.send_async(Event::Response((self.id, response)))
.await;
} }
} }
let _ = self let _ = self
.tx .tx
.send(Event::Response((self.id, PluginResponse::Finished))) .send_async(Event::Response((self.id, PluginResponse::Finished)))
.await; .await;
} }

View file

@ -11,8 +11,7 @@ pub use self::help::HelpPlugin;
use crate::{Indice, PluginHelp, Request}; use crate::{Indice, PluginHelp, Request};
use async_trait::async_trait; use async_trait::async_trait;
use postage::mpsc::{Receiver, Sender}; use flume::{Receiver, Sender};
use postage::prelude::*;
use regex::Regex; use regex::Regex;
#[async_trait] #[async_trait]
@ -39,8 +38,8 @@ where
async fn quit(&mut self, id: Indice); async fn quit(&mut self, id: Indice);
async fn run(&mut self, mut rx: Receiver<Request>) { async fn run(&mut self, rx: Receiver<Request>) {
while let Some(request) = rx.recv().await { while let Ok(request) = rx.recv_async().await {
tracing::event!( tracing::event!(
tracing::Level::DEBUG, tracing::Level::DEBUG,
"{}: received {:?}", "{}: received {:?}",
@ -130,7 +129,7 @@ impl PluginConnector {
.. ..
} = self; } = self;
sender.get_or_insert_with(|| init()) sender.get_or_insert_with(init)
} }
/// Drops the sender, which will subsequently drop the plugin forwarder attached to it /// Drops the sender, which will subsequently drop the plugin forwarder attached to it