Skip to main content

uqa_client/notifications/http/
cancellation.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7use std::fmt;
8use tokio::sync::watch;
9
10/// A monotonic, async-waking cancellation signal for an HTTP subscription's whole lifetime. It is independent of SQL and cannot be reset or used to restore cancelled authority.
11#[derive(Clone)]
12pub struct NotificationCancellation {
13    signal: watch::Sender<bool>,
14}
15
16impl NotificationCancellation {
17    pub fn new() -> Self {
18        Self {
19            signal: watch::channel(false).0,
20        }
21    }
22
23    pub fn cancel(&self) {
24        self.signal.send_replace(true);
25    }
26
27    pub fn is_cancelled(&self) -> bool {
28        *self.signal.borrow()
29    }
30
31    pub(super) async fn cancelled(&self) {
32        let mut receiver = self.signal.subscribe();
33        loop {
34            if *receiver.borrow_and_update() {
35                return;
36            }
37            if receiver.changed().await.is_err() {
38                return;
39            }
40        }
41    }
42}
43
44impl Default for NotificationCancellation {
45    fn default() -> Self {
46        Self::new()
47    }
48}
49
50impl fmt::Debug for NotificationCancellation {
51    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
52        formatter
53            .debug_struct("NotificationCancellation")
54            .field("cancelled", &self.is_cancelled())
55            .finish()
56    }
57}