use crate::ir::NodeId;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ComponentLifecycleEvent {
Mounted,
Unmounted,
}
pub type ComponentLifecycleCallback = Box<dyn Fn(NodeId, ComponentLifecycleEvent) + Send + Sync>;
pub struct ComponentLifecycle {
callback: Option<ComponentLifecycleCallback>,
}
impl ComponentLifecycle {
pub fn new() -> Self {
Self { callback: None }
}
pub fn on<F>(&mut self, callback: F)
where
F: Fn(NodeId, ComponentLifecycleEvent) + Send + Sync + 'static,
{
self.callback = Some(Box::new(callback));
}
pub fn notify_mounted(&self, node_id: NodeId) {
if let Some(ref callback) = self.callback {
callback(node_id, ComponentLifecycleEvent::Mounted);
}
}
pub fn notify_unmounted(&self, node_id: NodeId) {
if let Some(ref callback) = self.callback {
callback(node_id, ComponentLifecycleEvent::Unmounted);
}
}
}
impl Default for ComponentLifecycle {
fn default() -> Self {
Self::new()
}
}