(executor: Executor)
| 18 | |
| 19 | impl AutoExecutor { |
| 20 | pub(crate) fn new(executor: Executor) -> (Self, Sender<()>, Sender<Duration>) { |
| 21 | let (tx, mut rx) = channel::<()>(10); |
| 22 | let (auto_delay_tx, mut auto_delay_rx) = channel::<Duration>(10); |
| 23 | let (auto_tx, auto_rx) = std::sync::mpsc::channel::<()>(); |
| 24 | let is_auto = Arc::new(AtomicBool::new(false)); |
| 25 | |
| 26 | let (reset_tx, mut reset_rx) = channel::<()>(10); |
| 27 | |
| 28 | // 创建定时器 |
| 29 | let timer = slint::Timer::default(); |
| 30 | |
| 31 | let executor = Self { |
| 32 | timer, |
| 33 | executor, |
| 34 | is_auto: is_auto.clone(), |
| 35 | auto_rx: Some(auto_rx), |
| 36 | }; |
| 37 | |
| 38 | // 监听控制信号的任务 |
| 39 | let is_auto_clone = is_auto.clone(); |
| 40 | tokio::spawn(async move { |
| 41 | let mut start = true; |
| 42 | while rx.recv().await.is_some() { |
| 43 | if start { |
| 44 | is_auto_clone.store(true, Ordering::Relaxed); |
| 45 | start = false; |
| 46 | } else { |
| 47 | is_auto_clone.store(false, Ordering::Relaxed); |
| 48 | start = true; |
| 49 | if let Err(e) = reset_tx.send(()).await { |
| 50 | eprintln!("auto reset channel closed: {e}"); |
| 51 | return; |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | }); |
| 56 | |
| 57 | tokio::spawn(async move { |
| 58 | let mut current_delay: Option<Sleep> = None; |
| 59 | |
| 60 | loop { |
| 61 | tokio::select! { |
| 62 | Some(delay) = auto_delay_rx.recv() => { |
| 63 | current_delay = Some(tokio::time::sleep(delay)); |
| 64 | } |
| 65 | |
| 66 | // 延迟完成 |
| 67 | _ = async { |
| 68 | if let Some(sleep) = current_delay { |
| 69 | sleep.await |
| 70 | } else { |
| 71 | std::future::pending::<()>().await |
| 72 | } |
| 73 | } => { |
| 74 | if let Err(e) = auto_tx.send(()) { |
| 75 | eprintln!("auto trigger channel closed: {e}"); |
| 76 | return; |
| 77 | } |
nothing calls this directly
no outgoing calls
no test coverage detected