kaspa_notify/subscription/
array.rs1use crate::{
2 events::{EventArray, EventType},
3 listener::ListenerId,
4 subscription::{compounded, single, CompoundedSubscription, DynSubscription},
5};
6use std::sync::Arc;
7
8pub struct ArrayBuilder {}
9
10impl ArrayBuilder {
11 pub fn single(listener_id: ListenerId, utxos_changed_capacity: Option<usize>) -> EventArray<DynSubscription> {
12 EventArray::from_fn(|i| {
13 let event_type = EventType::try_from(i).unwrap();
14 let subscription: DynSubscription = match event_type {
15 EventType::VirtualChainChanged => Arc::<single::VirtualChainChangedSubscription>::default(),
16 EventType::UtxosChanged => Arc::new(single::UtxosChangedSubscription::with_capacity(
17 single::UtxosChangedState::None,
18 listener_id,
19 utxos_changed_capacity.unwrap_or_default(),
20 )),
21 _ => Arc::new(single::OverallSubscription::new(event_type, false)),
22 };
23 subscription
24 })
25 }
26
27 pub fn compounded(utxos_changed_capacity: Option<usize>) -> EventArray<CompoundedSubscription> {
28 EventArray::from_fn(|i| {
29 let event_type = EventType::try_from(i).unwrap();
30 let subscription: CompoundedSubscription = match event_type {
31 EventType::VirtualChainChanged => Box::<compounded::VirtualChainChangedSubscription>::default(),
32 EventType::UtxosChanged => {
33 Box::new(compounded::UtxosChangedSubscription::with_capacity(utxos_changed_capacity.unwrap_or_default()))
34 }
35 _ => Box::new(compounded::OverallSubscription::new(event_type)),
36 };
37 subscription
38 })
39 }
40}
41
42#[cfg(test)]
43mod tests {
44 use super::*;
45 use crate::events::EVENT_TYPE_ARRAY;
46
47 #[test]
48 fn test_array_builder() {
49 let single = ArrayBuilder::single(0, None);
50 let compounded = ArrayBuilder::compounded(None);
51 EVENT_TYPE_ARRAY.into_iter().for_each(|event| {
52 assert_eq!(
53 event,
54 single[event].event_type(),
55 "single subscription array item {:?} reports wrong event type {:?}",
56 event,
57 single[event].event_type()
58 );
59 assert_eq!(
60 event,
61 compounded[event].event_type(),
62 "compounded subscription array item {:?} reports wrong event type {:?}",
63 event,
64 compounded[event].event_type()
65 );
66 });
67 }
68}