summaryrefslogtreecommitdiff
path: root/qml_plugin/src/lib.rs
blob: 0f1411c277fa32ed3aca8b1fe268abc5d5e1c367 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use std::sync::LazyLock;

use tokio::task;

static RUNTIME: LazyLock<tokio::runtime::Runtime> =
    LazyLock::new(|| tokio::runtime::Builder::new_multi_thread().enable_all().build().unwrap());

/// 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;