saluki_core/components/decoders/
context.rs

1use crate::accounting::ComponentRegistry;
2use crate::health::Health;
3use crate::runtime::SupervisorHandle;
4use crate::{
5    components::ComponentContext,
6    topology::{EventsDispatcher, PayloadsConsumer, TopologyContext},
7};
8
9/// Decoder context.
10pub struct DecoderContext {
11    topology_context: TopologyContext,
12    component_context: ComponentContext,
13    component_registry: ComponentRegistry,
14    health_handle: Option<Health>,
15    dispatcher: EventsDispatcher,
16    consumer: PayloadsConsumer,
17    supervisor_handle: SupervisorHandle,
18}
19
20impl DecoderContext {
21    /// Creates a new `DecoderContext`.
22    pub fn new(
23        topology_context: &TopologyContext, component_context: &ComponentContext,
24        component_registry: ComponentRegistry, health_handle: Health, dispatcher: EventsDispatcher,
25        consumer: PayloadsConsumer, supervisor_handle: SupervisorHandle,
26    ) -> Self {
27        Self {
28            topology_context: topology_context.clone(),
29            component_context: component_context.clone(),
30            component_registry,
31            health_handle: Some(health_handle),
32            dispatcher,
33            consumer,
34            supervisor_handle,
35        }
36    }
37
38    /// Consumes the health handle of this decoder context.
39    ///
40    /// # Panics
41    ///
42    /// Panics if the health handle has already been taken.
43    pub fn take_health_handle(&mut self) -> Health {
44        self.health_handle.take().expect("health handle already taken")
45    }
46
47    /// Gets a reference to the topology context.
48    pub fn topology_context(&self) -> &TopologyContext {
49        &self.topology_context
50    }
51
52    /// Gets a reference to the component context.
53    pub fn component_context(&self) -> &ComponentContext {
54        &self.component_context
55    }
56
57    /// Gets a reference to the component registry.
58    pub fn component_registry(&mut self) -> &ComponentRegistry {
59        &self.component_registry
60    }
61
62    /// Gets a reference to the events dispatcher.
63    pub fn dispatcher(&self) -> &EventsDispatcher {
64        &self.dispatcher
65    }
66
67    /// Gets a mutable reference to the payloads consumer.
68    pub fn payloads(&mut self) -> &mut PayloadsConsumer {
69        &mut self.consumer
70    }
71
72    /// Returns a handle to the supervisor that this component is spawned on.
73    ///
74    /// Dynamic child processes can be spawned via the supervisor handle and thus have their lifecycle
75    /// coupled to the component itself: if the component restarts, or the component's supervisor dies,
76    /// the dynamic child processes will also be terminated automatically as well.
77    pub fn spawn_handle(&self) -> &SupervisorHandle {
78        &self.supervisor_handle
79    }
80}