Skip to content

Commit

Permalink
refactor(runtime): return status instead (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
fu050409 authored Sep 26, 2024
1 parent 80fe3be commit d8f688a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
6 changes: 3 additions & 3 deletions crates/aionbot-adapter-onebot/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use aionbot_core::runtime::{Runtime, StateManager};
use aionbot_core::runtime::{Runtime, RuntimeStatus, StateManager};
use anyhow::Result;
use onebot_v11::connect::ws_reverse::ReverseWsConnect;

Expand Down Expand Up @@ -55,7 +55,7 @@ impl Runtime for OnebotRuntime {
Ok(())
}

async fn run(&self) -> Result<()> {
Ok(())
async fn run(&self) -> Result<RuntimeStatus> {
Ok(RuntimeStatus::Exit)
}
}
29 changes: 26 additions & 3 deletions crates/aionbot-core/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}

Expand Down Expand Up @@ -95,5 +111,12 @@ pub trait Runtime {
async move { Ok(()) }
}

fn run(&self) -> impl std::future::Future<Output = Result<()>> + Send;
fn run(&self) -> impl std::future::Future<Output = Result<RuntimeStatus>> + Send;
}

pub enum RuntimeStatus {
Pending,
Exit,
Next,
Restart,
}

0 comments on commit d8f688a

Please sign in to comment.