use std::sync::Arc;
use crate::{AppContext, BoxError, BoxFuture, KernelError, ProviderKey, Resolver};
pub trait Lifecycle: Send + Sync + 'static {
fn on_init(&self, _ctx: AppContext) -> BoxFuture<'_, Result<(), BoxError>> {
Box::pin(async { Ok(()) })
}
fn on_bootstrap(&self, _ctx: AppContext) -> BoxFuture<'_, Result<(), BoxError>> {
Box::pin(async { Ok(()) })
}
fn on_shutdown(&self, _ctx: AppContext) -> BoxFuture<'_, Result<(), BoxError>> {
Box::pin(async { Ok(()) })
}
}
pub(crate) type LifecycleResolve = Arc<
dyn Fn(Resolver) -> BoxFuture<'static, Result<Arc<dyn Lifecycle>, KernelError>> + Send + Sync,
>;
#[derive(Clone)]
pub(crate) struct LifecycleDef {
pub(crate) key: ProviderKey,
pub(crate) resolve: LifecycleResolve,
}
impl LifecycleDef {
pub(crate) fn of<T: Lifecycle>() -> Self {
Self {
key: ProviderKey::of::<T>(),
resolve: Arc::new(|r| {
Box::pin(async move { r.get::<T>().await.map(|hooks| hooks as Arc<dyn Lifecycle>) })
}),
}
}
}