improv(service): Plugins shall respond directly, without forwarding middleman

This commit is contained in:
Michael Aaron Murphy 2021-08-16 22:10:17 +02:00
parent 2805d43b2b
commit 2287c40a38
4 changed files with 51 additions and 42 deletions

View file

@ -20,14 +20,16 @@ pub const CONFIG: PluginConfig = PluginConfig {
icon: Some(IconSource::Name(Cow::Borrowed("system-help-symbolic"))),
};
pub struct HelpPlugin {
pub id: usize,
pub details: Slab<PluginHelp>,
pub internal: Sender<Event>,
pub tx: Sender<PluginResponse>,
pub tx: Sender<Event>,
}
impl HelpPlugin {
pub fn new(internal: Sender<Event>, tx: Sender<PluginResponse>) -> Self {
pub fn new(id: usize, internal: Sender<Event>, tx: Sender<Event>) -> Self {
Self {
id,
details: Slab::new(),
internal,
tx,
@ -46,7 +48,13 @@ impl Plugin for HelpPlugin {
async fn activate(&mut self, id: u32) {
if let Some(detail) = self.details.get(id as usize) {
if let Some(help) = detail.help.as_ref() {
let _ = self.tx.send_async(PluginResponse::Fill(help.clone())).await;
let _ = self
.tx
.send_async(Event::Response((
self.id,
PluginResponse::Fill(help.clone()),
)))
.await;
}
}
}
@ -69,19 +77,24 @@ impl Plugin for HelpPlugin {
}
for (id, detail) in self.details.iter() {
if detail.help.is_some() {
let response = PluginResponse::Append(PluginSearchResult {
id: id as u32,
name: detail.name.clone(),
description: detail.description.clone(),
..Default::default()
});
let _ = self
.tx
.send_async(PluginResponse::Append(PluginSearchResult {
id: id as u32,
name: detail.name.clone(),
description: detail.description.clone(),
..Default::default()
}))
.send_async(Event::Response((self.id, response)))
.await;
}
}
let _ = self.tx.send_async(PluginResponse::Finished).await;
let _ = self
.tx
.send_async(Event::Response((self.id, PluginResponse::Finished)))
.await;
}
async fn quit(&mut self, _id: u32) {}