From 345c9344a37740fd084bf310be6e56e483c86d5e Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Mon, 3 Aug 2026 16:56:06 +0800 Subject: lib(tokio): ExclusiveExecutor --- qml_plugin/src/lib.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'qml_plugin/src') 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 = LazyLock::new( || { tokio::runtime::Builder::new_multi_thread() @@ -9,5 +11,37 @@ static RUNTIME: LazyLock = 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>, +} + +impl ExclusiveExecutor { + pub fn new() -> Self { + Self { + handle: None, + } + } + + #[track_caller] + pub fn spawn(&mut self, future: F) + where + F: Future + 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; -- cgit v1.2.3