Files
clash-verge-rev/crates/clash-verge-task/src/countdown.rs
T
Tunglies 929bb6a0a1 refactor(backend): share single-owner drivers for background tasks (#8759)
* docs(backend): define background task ownership

* refactor(backend): give background operations single task owners

* test(backend): remove actor migration regression cases

* docs(changelog): drop backend actor migration entry

* refactor(backend): extract workspace task drivers

* fix(timer): omit profile identifier from panic log

* docs(backend): define workspace crate boundaries

* refactor(lightweight): use the task countdown driver

* refactor(lightweight): name countdown callbacks

* refactor(task): simplify driver startup and coalescing requests

* refactor(core): bind service handoff to the sidecar generation

* refactor(tun): coalesce background run state reconciliation

* refactor(core): own sidecar exit resources in one session

* refactor(core): remove automatic Windows service handoff

* refactor(task): simplify coalescing job signals

* refactor(runstate): keep operation status with service state

* refactor(dns): coalesce background override synchronization

* refactor(sysproxy): own the local guard under its operation lock

* fix(tray): read menu settings at publication

* fix(dns): retain script lock across caller cancellation

* refactor(profiles): own selected node restoration in one task

* refactor(timer): own subscription schedules in the scheduler

* test(timer): remove the added application test

* fix(service): renew provider sync retry budget on refresh
2026-11-29 00:42:52 +00:00

61 lines
1.8 KiB
Rust

use std::{future::Future, time::Duration};
use tokio::{sync::mpsc, time::sleep};
enum Command<T> {
Arm(T),
Disarm,
}
pub struct Countdown<T> {
commands: mpsc::UnboundedSender<Command<T>>,
}
impl<T> Countdown<T> {
pub fn arm(&self, input: T) {
let _ = self.commands.send(Command::Arm(input));
}
pub fn disarm(&self) {
let _ = self.commands.send(Command::Disarm);
}
}
/// Create a countdown with a caller-owned driver. An active countdown ignores
/// additional arms. Disarm and channel closure take precedence over expiry.
///
/// Return `None` from `delay` to reject an arm before starting the countdown.
/// The delay future may be dropped on disarm, including while it loads the duration.
pub fn new<T, Fut>(
mut delay: impl FnMut(T) -> Option<Fut> + Send,
mut elapsed: impl FnMut(T) + Send,
) -> (Countdown<T>, impl Future<Output = ()> + Send)
where
T: Copy + Send,
Fut: Future<Output = Duration> + Send,
{
let (commands, mut receiver) = mpsc::unbounded_channel();
let driver = async move {
while let Some(command) = receiver.recv().await {
let Command::Arm(input) = command else { continue };
let Some(delay) = delay(input) else { continue };
let countdown = async { sleep(delay.await).await };
tokio::pin!(countdown);
loop {
tokio::select! {
biased;
command = receiver.recv() => match command {
Some(Command::Arm(_)) => {}
Some(Command::Disarm) => break,
None => return,
},
() = &mut countdown => {
elapsed(input);
break;
}
}
}
}
};
(Countdown { commands }, driver)
}