Skip to content

Commit

Permalink
feat(runtime): implement runtime trait
Browse files Browse the repository at this point in the history
  • Loading branch information
fu050409 committed Sep 24, 2024
1 parent e3a145b commit d5cf31a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
25 changes: 24 additions & 1 deletion crates/aionbot-adapter-onebot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::sync::Arc;

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

pub extern crate aionbot_core;

Expand All @@ -17,11 +18,20 @@ impl Adapter for aionbot_core::event::Event {
}
}

#[derive(Default)]
pub struct OnebotRuntime {
connect: Option<Arc<ReverseWsConnect>>,
state: Arc<StateManager>,
}

impl Default for OnebotRuntime {
fn default() -> Self {
Self {
connect: None,
state: Arc::new(StateManager::default()),
}
}
}

impl Runtime for OnebotRuntime {
fn set_manager(mut self, manager: Arc<StateManager>) -> Self {
self.state = manager;
Expand All @@ -32,6 +42,19 @@ impl Runtime for OnebotRuntime {
&self.state
}

async fn prepare(&mut self) -> Result<()> {
self.connect = Some(ReverseWsConnect::new(Default::default()).await?);
Ok(())
}

fn setup(&mut self, setup: Box<dyn FnOnce(&Self) + Send + Sync>) {
setup(self);
}

async fn finalize(&mut self) -> Result<()> {
Ok(())
}

async fn run(&self) -> Result<()> {
Ok(())
}
Expand Down
20 changes: 20 additions & 0 deletions crates/aionbot-core/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@ pub struct Builder<R: Runtime + Default> {
handler: Arc<Option<Handler>>,
runtime: R,
state: Arc<StateManager>,
setup: Option<Box<dyn FnOnce(&R) + Send + Sync>>,
}

impl<R> Builder<R>
where
R: Runtime + Default + Send + 'static,
{
pub fn setup(&mut self, setup: Box<dyn FnOnce(&R) + Send + Sync>) {
self.setup = Some(setup);
}

pub fn invoke_handler(mut self, entries: Vec<Entry>) -> Self {
self.handler = Arc::new(Some(Handler::new(entries)));
self
Expand All @@ -47,6 +52,12 @@ where
}

pub async fn run(&mut self) -> Result<()> {
println!("Starting bot...");
self.runtime.prepare().await?;
if let Some(setup) = self.setup.take() {
self.runtime.setup(setup);
}
self.runtime.finalize().await?;
self.runtime.run().await
}
}
Expand All @@ -62,6 +73,7 @@ where
handler: Arc::new(None),
runtime,
state: Arc::clone(&manager),
setup: None,
}
}
}
Expand All @@ -72,9 +84,17 @@ pub trait Runtime {

fn manager(&self) -> &StateManager;

fn prepare(&mut self) -> impl std::future::Future<Output = Result<()>> + Send {
async move { Ok(()) }
}

fn setup(&mut self, setup: Box<dyn FnOnce(&Self) + Send + Sync>) {
setup(self)
}

fn finalize(&mut self) -> impl std::future::Future<Output = Result<()>> + Send {
async move { Ok(()) }
}

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

0 comments on commit d5cf31a

Please sign in to comment.