use std::sync::Arc;
use std::time::Duration;
use serenity::all::{ChannelId, Http};
use tokio_util::sync::CancellationToken;
use uuid::Uuid;
use crate::brain::agent::service::background_tasks::BackgroundTaskManager;
const TICK: Duration = Duration::from_secs(8);
pub(crate) struct TypingGuard(pub(crate) CancellationToken);
impl Drop for TypingGuard {
fn drop(&mut self) {
self.0.cancel();
}
}
pub(crate) fn spawn_typing(
http: Arc<Http>,
channel: ChannelId,
cancel: CancellationToken,
background: Option<Arc<BackgroundTaskManager>>,
session_id: Uuid,
) {
tokio::spawn(async move {
let _ = channel.broadcast_typing(&http).await;
loop {
tokio::select! {
_ = cancel.cancelled() => break,
_ = tokio::time::sleep(TICK) => {
let _ = channel.broadcast_typing(&http).await;
}
}
}
let Some(manager) = background else {
return;
};
while manager.running_for(session_id) > 0 {
let _ = channel.broadcast_typing(&http).await;
tokio::time::sleep(TICK).await;
}
});
}