saluki_core/components/transforms/
context.rs

1use crate::accounting::ComponentRegistry;
2use crate::health::Health;
3use crate::{
4    components::{ComponentContext, ComponentSpawner},
5    topology::{EventsConsumer, EventsDispatcher, TopologyContext},
6};
7
8/// Transform context.
9pub struct TransformContext {
10    topology_context: TopologyContext,
11    component_context: ComponentContext,
12    component_registry: ComponentRegistry,
13    health_handle: Option<Health>,
14    dispatcher: EventsDispatcher,
15    consumer: EventsConsumer,
16    spawner: ComponentSpawner,
17}
18
19impl TransformContext {
20    /// Creates a new `TransformContext`.
21    pub fn new(
22        topology_context: &TopologyContext, component_context: &ComponentContext,
23        component_registry: ComponentRegistry, health_handle: Health, dispatcher: EventsDispatcher,
24        consumer: EventsConsumer, spawner: ComponentSpawner,
25    ) -> Self {
26        Self {
27            topology_context: topology_context.clone(),
28            component_context: component_context.clone(),
29            component_registry,
30            health_handle: Some(health_handle),
31            dispatcher,
32            consumer,
33            spawner,
34        }
35    }
36
37    /// Consumes the health handle of this transform context.
38    ///
39    /// # Panics
40    ///
41    /// Panics if the health handle has already been taken.
42    pub fn take_health_handle(&mut self) -> Health {
43        self.health_handle.take().expect("health handle already taken")
44    }
45
46    /// Returns a reference to the topology context.
47    pub fn topology_context(&self) -> &TopologyContext {
48        &self.topology_context
49    }
50
51    /// Returns a reference to the component context.
52    pub fn component_context(&self) -> &ComponentContext {
53        &self.component_context
54    }
55
56    /// Returns a reference to the events dispatcher.
57    pub fn dispatcher(&self) -> &EventsDispatcher {
58        &self.dispatcher
59    }
60
61    /// Returns a mutable reference to the events consumer.
62    pub fn events(&mut self) -> &mut EventsConsumer {
63        &mut self.consumer
64    }
65
66    /// Returns a mutable reference to the component registry.
67    pub fn component_registry(&self) -> &ComponentRegistry {
68        &self.component_registry
69    }
70
71    /// Returns a spawner for supervised child tasks belonging to this component.
72    ///
73    /// All child tasks spawned through this mechanism are tied to the lifecycle of the component itself, such that
74    /// they're automatically shutdown/stopped when the component is stopped during topology shutdown, etc.
75    pub fn spawner(&self) -> &ComponentSpawner {
76        &self.spawner
77    }
78}