navree_kernel/
lifecycle.rs1use std::sync::Arc;
4
5use crate::{AppContext, BoxError, BoxFuture, KernelError, ProviderKey, Resolver};
6
7pub trait Lifecycle: Send + Sync + 'static {
14 fn on_init(&self, _ctx: AppContext) -> BoxFuture<'_, Result<(), BoxError>> {
16 Box::pin(async { Ok(()) })
17 }
18
19 fn on_bootstrap(&self, _ctx: AppContext) -> BoxFuture<'_, Result<(), BoxError>> {
21 Box::pin(async { Ok(()) })
22 }
23
24 fn on_shutdown(&self, _ctx: AppContext) -> BoxFuture<'_, Result<(), BoxError>> {
26 Box::pin(async { Ok(()) })
27 }
28}
29
30pub(crate) type LifecycleResolve = Arc<
32 dyn Fn(Resolver) -> BoxFuture<'static, Result<Arc<dyn Lifecycle>, KernelError>> + Send + Sync,
33>;
34
35#[derive(Clone)]
37pub(crate) struct LifecycleDef {
38 pub(crate) key: ProviderKey,
39 pub(crate) resolve: LifecycleResolve,
40}
41
42impl LifecycleDef {
43 pub(crate) fn of<T: Lifecycle>() -> Self {
44 Self {
45 key: ProviderKey::of::<T>(),
46 resolve: Arc::new(|r| {
47 Box::pin(async move { r.get::<T>().await.map(|hooks| hooks as Arc<dyn Lifecycle>) })
48 }),
49 }
50 }
51}