Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ eframe = { version = "0.31.1", default-features = false, features = [
] }

# plugins
async-trait = "0.1.89"
smol = "2"
async-channel = "2"
futures-lite = "2"
Expand Down
107 changes: 58 additions & 49 deletions client/src/plugin/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ fn fuzzy_match(query: &str, entries: Vec<crate::model::Entry>) -> Vec<crate::mod
.collect::<Vec<_>>()
}

#[async_trait::async_trait]
pub trait Plugin {
fn id() -> &'static str;
fn priority() -> u32;
Expand Down Expand Up @@ -113,23 +112,28 @@ pub trait Plugin {
}
}

async fn main(
fn main(
&mut self,
mut plugin_channel_out: async_channel::Sender<crate::Message>,
) -> anyhow::Result<()> {
self.update_entries()?;

let (mut app_channel_out, mut plugin_channel_in) = async_channel::bounded(100);
self.register_plugin(&mut plugin_channel_out, &mut app_channel_out)?;
let mut last_query = String::from("");

loop {
self.update(
&mut plugin_channel_out,
&mut plugin_channel_in,
&mut last_query,
)
.await?;
) -> impl std::future::Future<Output = anyhow::Result<()>> + Send + '_
where
Self: Send,
{
async move {
self.update_entries()?;

let (mut app_channel_out, mut plugin_channel_in) = async_channel::bounded(100);
self.register_plugin(&mut plugin_channel_out, &mut app_channel_out)?;
let mut last_query = String::from("");

loop {
self.update(
&mut plugin_channel_out,
&mut plugin_channel_in,
&mut last_query,
)
.await?;
}
}
}

Expand All @@ -145,42 +149,47 @@ pub trait Plugin {
Ok(())
}

async fn update(
&mut self,
plugin_channel_out: &mut async_channel::Sender<crate::Message>,
plugin_channel_in: &mut async_channel::Receiver<crate::model::PluginRequest>,
last_query: &mut String,
) -> anyhow::Result<()> {
let plugin_request_option = match Self::update_timeout() {
Some(update_timeout) => {
futures_lite::future::or(async { plugin_channel_in.recv().await.ok() }, async {
smol::Timer::after(update_timeout).await;
Some(crate::model::PluginRequest::Timeout)
})
.await
fn update<'a>(
&'a mut self,
plugin_channel_out: &'a mut async_channel::Sender<crate::Message>,
plugin_channel_in: &'a mut async_channel::Receiver<crate::model::PluginRequest>,
last_query: &'a mut String,
) -> impl std::future::Future<Output = anyhow::Result<()>> + Send + 'a
where
Self: Send,
{
async move {
let plugin_request_option = match Self::update_timeout() {
Some(update_timeout) => {
futures_lite::future::or(async { plugin_channel_in.recv().await.ok() }, async {
smol::Timer::after(update_timeout).await;
Some(crate::model::PluginRequest::Timeout)
})
.await
}
None => plugin_channel_in.recv().await.ok(),
};
if plugin_request_option.is_none() {
return Ok(());
}
None => plugin_channel_in.recv().await.ok(),
};
if plugin_request_option.is_none() {
return Ok(());
}
let plugin_request = plugin_request_option.unwrap();

match plugin_request {
crate::model::PluginRequest::Search(query) => {
self.search(&query, plugin_channel_out)?;
*last_query = query;
}
crate::model::PluginRequest::Timeout => {
self.update_entries()?;
self.search(last_query, plugin_channel_out)?;
let plugin_request = plugin_request_option.unwrap();

match plugin_request {
crate::model::PluginRequest::Search(query) => {
self.search(&query, plugin_channel_out)?;
*last_query = query;
}
crate::model::PluginRequest::Timeout => {
self.update_entries()?;
self.search(last_query, plugin_channel_out)?;
}
crate::model::PluginRequest::Activate(entry) => {
self.activate(entry, plugin_channel_out)?
}
}
crate::model::PluginRequest::Activate(entry) => {
self.activate(entry, plugin_channel_out)?
}
}

return Ok(());
Ok(())
}
}

fn sort(&mut self) {
Expand Down