supabase_realtime_rs/infrastructure/
task_manager.rs

1use tokio::task::JoinHandle;
2
3/// Manages background tasks with proper lifecycle handling
4pub struct TaskManager {
5    handles: Vec<JoinHandle<()>>,
6}
7
8impl TaskManager {
9    /// Create a new empty task manager
10    pub fn new() -> Self {
11        Self {
12            handles: Vec::new(),
13        }
14    }
15
16    /// Spawn a task and track it
17    pub fn spawn<F>(&mut self, future: F)
18    where
19        F: std::future::Future<Output = ()> + Send + 'static,
20    {
21        let handle = tokio::spawn(future);
22        self.handles.push(handle);
23    }
24
25    /// Abort all tracked tasks and wait for them to finish
26    pub async fn shutdown(self) {
27        for handle in self.handles {
28            handle.abort();
29            // Ignore errors from aborted tasks
30            let _ = handle.await;
31        }
32    }
33
34    /// Abort all tasks without waiting
35    pub fn abort_all(&mut self) {
36        for handle in &self.handles {
37            handle.abort();
38        }
39        self.handles.clear();
40    }
41}
42
43impl Default for TaskManager {
44    fn default() -> Self {
45        Self::new()
46    }
47}