saluki_core/components/sources/
context.rs

1use std::sync::Arc;
2
3use memory_accounting::ComponentRegistry;
4use saluki_health::Health;
5
6use crate::{
7    components::ComponentContext,
8    topology::{shutdown::ComponentShutdownHandle, EventsDispatcher, TopologyContext},
9};
10
11struct SourceContextInner {
12    topology_context: TopologyContext,
13    component_context: ComponentContext,
14    component_registry: ComponentRegistry,
15    dispatcher: EventsDispatcher,
16}
17
18/// Source context.
19pub struct SourceContext {
20    shutdown_handle: Option<ComponentShutdownHandle>,
21    health_handle: Option<Health>,
22    inner: Arc<SourceContextInner>,
23}
24
25impl SourceContext {
26    /// Creates a new `SourceContext`.
27    pub fn new(
28        topology_context: &TopologyContext, component_context: &ComponentContext,
29        component_registry: ComponentRegistry, shutdown_handle: ComponentShutdownHandle, health_handle: Health,
30        dispatcher: EventsDispatcher,
31    ) -> Self {
32        Self {
33            shutdown_handle: Some(shutdown_handle),
34            health_handle: Some(health_handle),
35            inner: Arc::new(SourceContextInner {
36                topology_context: topology_context.clone(),
37                component_context: component_context.clone(),
38                component_registry,
39                dispatcher,
40            }),
41        }
42    }
43
44    /// Consumes the shutdown handle of this source context.
45    ///
46    /// # Panics
47    ///
48    /// Panics if the shutdown handle has already been taken.
49    pub fn take_shutdown_handle(&mut self) -> ComponentShutdownHandle {
50        self.shutdown_handle.take().expect("shutdown handle already taken")
51    }
52
53    /// Consumes the health handle of this source context.
54    ///
55    /// # Panics
56    ///
57    /// Panics if the health handle has already been taken.
58    pub fn take_health_handle(&mut self) -> Health {
59        self.health_handle.take().expect("health handle already taken")
60    }
61
62    /// Gets a reference to the topology context.
63    pub fn topology_context(&self) -> &TopologyContext {
64        &self.inner.topology_context
65    }
66
67    /// Gets a reference to the component context.
68    pub fn component_context(&self) -> &ComponentContext {
69        &self.inner.component_context
70    }
71
72    /// Gets a reference to the component registry.
73    pub fn component_registry(&self) -> &ComponentRegistry {
74        &self.inner.component_registry
75    }
76
77    /// Gets a reference to the events dispatcher.
78    pub fn dispatcher(&self) -> &EventsDispatcher {
79        &self.inner.dispatcher
80    }
81}
82
83impl Clone for SourceContext {
84    fn clone(&self) -> Self {
85        Self {
86            shutdown_handle: None,
87            health_handle: None,
88            inner: self.inner.clone(),
89        }
90    }
91}