alopex_cli/streaming/
cancel.rs1use std::sync::atomic::{AtomicBool, Ordering};
7use std::sync::Arc;
8use std::time::Duration;
9
10use crate::config;
11
12#[derive(Clone, Debug, Default)]
13pub struct CancelSignal {
14 manual: Arc<AtomicBool>,
15}
16
17impl CancelSignal {
18 pub fn new() -> Self {
19 Self {
20 manual: Arc::new(AtomicBool::new(false)),
21 }
22 }
23
24 #[allow(dead_code)]
25 pub fn cancel(&self) {
26 self.manual.store(true, Ordering::SeqCst);
27 }
28
29 pub fn is_cancelled(&self) -> bool {
30 config::is_interrupted() || self.manual.load(Ordering::SeqCst)
31 }
32
33 pub async fn wait(&self) {
34 while !self.is_cancelled() {
35 tokio::time::sleep(Duration::from_millis(50)).await;
36 }
37 }
38}