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 of the live chain starts.
10 SyncLiveChainStarted,
11 /// Event triggered when the synchronization process of the immutable chain starts.
12 SyncImmutableChainStarted,
13 /// Event triggered when the synchronization process of the live chain completes
14 /// (live chain synchronization never stops, this event triggers when first tip is
15 /// reached).
16 SyncLiveChainCompleted,
17 /// Event triggered when the synchronization process of the immutable chain completes.
18 SyncImmutableChainCompleted,
19 /// Event triggered when the number of current synchronization tasks changes.
20 SyncTasksChanged {
21 /// The current number of synchronization tasks.
22 current_sync_tasks: u16,
23 },
24 /// Event triggered when the live tip slot changes.
25 LiveTipSlotChanged {
26 /// The immutable tip slot.
27 immutable_slot: Slot,
28 /// The new live tip slot.
29 live_slot: Slot,
30 },
31 /// Event triggered when the immutable tip slot changes.
32 ImmutableTipSlotChanged {
33 /// The new immutable tip slot.
34 immutable_slot: Slot,
35 /// The live tip slot.
36 live_slot: Slot,
37 },
38 /// Event triggered when the indexed slot progresses.
39 IndexedSlotProgressed {
40 /// The latest indexed slot.
41 slot: Slot,
42 },
43 /// Event triggered when backward data is purged.
44 BackwardDataPurged,
45 /// Event triggered when forward data is purged.
46 ForwardDataPurged {
47 /// Number of purged slots.
48 purge_slots: u64,
49 },
50}
51
52pub(crate) type EventListenerFn<T> = Box<dyn Fn(&T) + Send + Sync + 'static>;
53
54/// A trait that allows adding and dispatching events to listeners.
55pub(crate) trait EventTarget<T: Send + Sync> {
56 /// Adds an event listener to the target.
57 ///
58 /// # Arguments
59 /// * `listener` - A function that will be called whenever an event of type `T`
60 /// occurs.
61 fn add_event_listener(&mut self, listener: EventListenerFn<T>);
62
63 /// Dispatches an event to all registered listeners.
64 ///
65 /// # Arguments
66 /// * `message` - The event message to be dispatched.
67 fn dispatch_event(&self, message: T);
68}