use std::ops::Deref;
use std::sync::Mutex;
use dashmap::DashMap;
use crate::{App, AppContext, AppError, Plugin};
pub struct AppBuilder {
pub(crate) context: AppContext,
}
impl Deref for AppBuilder {
type Target = AppContext;
fn deref(&self) -> &AppContext {
&self.context
}
}
impl AppBuilder {
pub fn add_plugin<T>(&mut self, plugin: T) -> &mut Self
where
T: Plugin + 'static,
{
self.context.add_plugin(plugin);
self
}
pub fn add_component<T>(&mut self, component: T) -> &mut Self
where
T: Send + Sync + 'static,
{
self.context.add_component(component);
self
}
pub async fn build(&mut self) -> Result<App, AppError> {
let context = std::mem::replace(
&mut self.context,
AppContext {
components: DashMap::new(),
plugins: DashMap::new(),
pending_plugins: Mutex::new(Vec::new()),
},
);
context.build_app().await
}
}