diff options
Diffstat (limited to 'qml_plugin/src/lib.rs')
| -rw-r--r-- | qml_plugin/src/lib.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/qml_plugin/src/lib.rs b/qml_plugin/src/lib.rs index e4ad346..f9f095e 100644 --- a/qml_plugin/src/lib.rs +++ b/qml_plugin/src/lib.rs @@ -1,5 +1,7 @@ use std::sync::LazyLock; +use tokio::task; + static RUNTIME: LazyLock<tokio::runtime::Runtime> = LazyLock::new( || { tokio::runtime::Builder::new_multi_thread() @@ -9,5 +11,37 @@ static RUNTIME: LazyLock<tokio::runtime::Runtime> = LazyLock::new( }, ); +/// for example duckduckgo we wanna cancel first request if user types new query +/// using this guarentee that only one request is running at a time or at least +/// the first one is being aborted +#[derive(Default)] +pub struct ExclusiveExecutor { + handle: Option<task::JoinHandle<()>>, +} + +impl ExclusiveExecutor { + pub fn new() -> Self { + Self { + handle: None, + } + } + + #[track_caller] + pub fn spawn<F>(&mut self, future: F) + where + F: Future<Output = ()> + Send + 'static, + F::Output: Send + 'static, + { + if let Some(handle) = self + .handle + .take() + { + handle.abort(); + } + + self.handle = Some(RUNTIME.spawn(future)) + } +} + mod launcher; mod web; |
