cat_gateway/cardano/
event.rs

1//! Event types for Chain Indexer.
2
3use cardano_blockchain_types::Slot;
4
5/// Represents various events that can occur in the chain indexer.
6/// These events are used to track the state and progress of indexing operations.
7#[derive(Debug, Clone)]
8pub(crate) enum ChainIndexerEvent {
9    /// Event triggered when the synchronization process starts.
10    SyncStarted,
11    /// Event triggered when the synchronization process completes.
12    SyncCompleted,
13    /// Event triggered when the number of current synchronization tasks changes.
14    SyncTasksChanged {
15        /// The current number of synchronization tasks.
16        current_sync_tasks: u16,
17    },
18    /// Event triggered when the live tip slot changes.
19    LiveTipSlotChanged {
20        /// The new live tip slot.
21        slot: Slot,
22    },
23    /// Event triggered when the immutable tip slot changes.
24    ImmutableTipSlotChanged {
25        /// The new immutable tip slot.
26        slot: Slot,
27    },
28    /// Event triggered when the indexed slot progresses.
29    IndexedSlotProgressed {
30        /// The latest indexed slot.
31        slot: Slot,
32    },
33    /// Event triggered when backward data is purged.
34    BackwardDataPurged,
35    /// Event triggered when forward data is purged.
36    ForwardDataPurged {
37        /// Number of purged slots.
38        purge_slots: u64,
39    },
40}
41
42pub(crate) type EventListenerFn<T> = Box<dyn Fn(&T) + Send + Sync + 'static>;
43
44/// A trait that allows adding and dispatching events to listeners.
45pub(crate) trait EventTarget<T: Send + Sync> {
46    /// Adds an event listener to the target.
47    ///
48    /// # Arguments
49    /// * `listener` - A function that will be called whenever an event of type `T`
50    ///   occurs.
51    fn add_event_listener(&mut self, listener: EventListenerFn<T>);
52
53    /// Dispatches an event to all registered listeners.
54    ///
55    /// # Arguments
56    /// * `message` - The event message to be dispatched.
57    fn dispatch_event(&self, message: T);
58}