summaryrefslogtreecommitdiff
path: root/qml_plugin
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2026-08-03 16:56:06 +0800
committerlittleblack111 <littleblack11111@gmail.com>2026-08-04 23:07:44 +0800
commit345c9344a37740fd084bf310be6e56e483c86d5e (patch)
treecba6870b0a35414224656ee59e916b3e8cb9d35c /qml_plugin
parentc3845a03db9f79bbf273449961f46e38f455fd8f (diff)
lib(tokio): ExclusiveExecutor
Diffstat (limited to 'qml_plugin')
-rw-r--r--qml_plugin/src/lib.rs34
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;