Skip to main content

bonds_core/manager/
hooks.rs

1use super::*;
2
3/// Hook registration and dispatch concerns.
4impl BondManager {
5    /// Register closure/type-based hook.
6    pub fn register_hook<H>(&self, hook: H)
7    where
8        H: BondEventHook + 'static,
9    {
10        self.register_hook_arc(Arc::new(hook));
11    }
12
13    /// Register Arc-wrapped hook implementation.
14    pub fn register_hook_arc(&self, hook: Arc<dyn BondEventHook>) {
15        if let Ok(mut hooks) = self.hooks.write() {
16            hooks.push(hook);
17        }
18    }
19
20    /// Emit event to all hooks.
21    ///
22    /// `pub(super)` is important: sibling modules (lifecycle/health) call this.
23    pub(super) fn emit_event(&self, payload: BondEventPayload) {
24        let hooks = match self.hooks.read() {
25            Ok(h) => h.clone(),
26            // If lock is poisoned, skip dispatch but do not fail core operation.
27            Err(_) => return,
28        };
29
30        if hooks.is_empty() {
31            return;
32        }
33
34        let event = BondEvent {
35            occurred_at: Utc::now(),
36            payload,
37        };
38
39        for hook in hooks {
40            hook.on_event(&event);
41        }
42    }
43}