can't remember

This commit is contained in:
wrk 2023-06-01 17:06:48 +02:00
parent 37e686b8c9
commit 18dd20db3c

View File

@ -13,7 +13,7 @@ use std::{
net::ToSocketAddrs, net::ToSocketAddrs,
path::Path, path::Path,
sync::Arc, sync::Arc,
time::SystemTime, time::{Duration, SystemTime},
}; };
use async_native_tls::TlsStream; use async_native_tls::TlsStream;
@ -171,7 +171,7 @@ pub struct Context {
default_system: Option<StoredSystem>, default_system: Option<StoredSystem>,
invalid_system: Option<StoredSystem>, invalid_system: Option<StoredSystem>,
systems: HashMap<String, StoredSystem>, systems: HashMap<String, StoredSystem>,
tasks: Vec<StoredSystem>, tasks: Vec<(Duration, StoredSystem)>,
factory: Arc<RwLock<Factory>>, factory: Arc<RwLock<Factory>>,
} }
@ -310,12 +310,13 @@ impl Context {
) )
} }
pub async fn run_tasks(&mut self, tx: mpsc::Sender<Response>) { pub async fn run_interval_tasks(&mut self, tx: mpsc::Sender<Response>) {
for mut task in std::mem::take(&mut self.tasks) { for (duration, mut task) in std::mem::take(&mut self.tasks) {
let fact = self.factory.clone(); let fact = self.factory.clone();
let task_tx = tx.clone(); let task_tx = tx.clone();
tokio::spawn(async move { tokio::spawn(async move {
loop { loop {
tokio::time::sleep(duration).await;
let resp = task.run( let resp = task.run(
&IrcPrefix { &IrcPrefix {
nick: "", nick: "",
@ -406,13 +407,25 @@ impl Irc {
self self
} }
pub async fn add_interval_task<I, S: for<'a> System + Send + Sync + 'static>(
&mut self,
duration: Duration,
system: impl for<'a> IntoSystem<I, System = S>,
) -> &mut Self {
{
let mut context = self.context.write().await;
context.tasks.push((duration, Box::new(system.into_system())));
}
self
}
pub async fn add_task<I, S: for<'a> System + Send + Sync + 'static>( pub async fn add_task<I, S: for<'a> System + Send + Sync + 'static>(
&mut self, &mut self,
system: impl for<'a> IntoSystem<I, System = S>, system: impl for<'a> IntoSystem<I, System = S>,
) -> &mut Self { ) -> &mut Self {
{ {
let mut context = self.context.write().await; let mut context = self.context.write().await;
context.tasks.push(Box::new(system.into_system())); context.tasks.push((Duration::ZERO, Box::new(system.into_system())));
} }
self self
} }
@ -542,7 +555,7 @@ impl Irc {
{ {
let mut context = self.context.write().await; let mut context = self.context.write().await;
context.register(); context.register();
context.run_tasks(tx).await; context.run_interval_tasks(tx).await;
} }
let stream = self.stream.take().unwrap(); let stream = self.stream.take().unwrap();