diff --git a/crates/aionbot-adapter-onebot/src/lib.rs b/crates/aionbot-adapter-onebot/src/lib.rs index 1c559e0..2f3f7e5 100644 --- a/crates/aionbot-adapter-onebot/src/lib.rs +++ b/crates/aionbot-adapter-onebot/src/lib.rs @@ -4,10 +4,8 @@ pub mod ws; use std::sync::Arc; -use aionbot_core::{ - event::Event, - runtime::{Runtime, StateManager}, -}; +use aionbot_core::event::Event; +use aionbot_core::runtime::{Runtime, RuntimeStatus, StateManager}; use anyhow::Result; use tokio::sync::broadcast::Receiver; use ws::Onebot; @@ -66,8 +64,8 @@ impl Runtime for OnebotRuntime { Ok(()) } - async fn run(&mut self) -> Result<()> { + async fn run(&mut self) -> Result { self.receiver.as_mut().unwrap().recv().await?; - Ok(()) + Ok(RuntimeStatus::Exit) } } diff --git a/crates/aionbot-core/src/runtime.rs b/crates/aionbot-core/src/runtime.rs index 56846f7..4ba9531 100644 --- a/crates/aionbot-core/src/runtime.rs +++ b/crates/aionbot-core/src/runtime.rs @@ -51,13 +51,29 @@ where self } - pub async fn run(&mut self) -> Result<()> { + async fn prepare(&mut self) -> Result<()> { self.runtime.prepare().await?; if let Some(setup) = self.setup.take() { self.runtime.setup(setup); } self.runtime.finalize().await?; - self.runtime.run().await + Ok(()) + } + + pub async fn run(&mut self) -> Result<()> { + self.prepare().await?; + + loop { + match self.runtime.run().await? { + RuntimeStatus::Pending => {} + RuntimeStatus::Exit => break, + RuntimeStatus::Next => {} + RuntimeStatus::Restart => { + self.runtime.prepare().await?; + } + } + } + Ok(()) } } @@ -95,5 +111,12 @@ pub trait Runtime { async move { Ok(()) } } - fn run(&mut self) -> impl std::future::Future> + Send; + fn run(&mut self) -> impl std::future::Future> + Send; +} + +pub enum RuntimeStatus { + Pending, + Exit, + Next, + Restart, }