1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use crate::Config;
use fuel_core_interfaces::block_importer::{
ImportBlockBroadcast,
ImportBlockMpsc,
};
use parking_lot::Mutex;
use tokio::{
sync::{
broadcast,
mpsc,
},
task::JoinHandle,
};
pub struct Service {
join: Mutex<Option<JoinHandle<()>>>,
sender: mpsc::Sender<ImportBlockMpsc>,
broadcast: broadcast::Sender<ImportBlockBroadcast>,
}
impl Service {
pub async fn new(_config: &Config, _db: ()) -> anyhow::Result<Self> {
let (sender, _receiver) = mpsc::channel(100);
let (broadcast, _receiver) = broadcast::channel(100);
Ok(Self {
sender,
broadcast,
join: Mutex::new(None),
})
}
pub async fn start(&self) {
let mut join = self.join.lock();
if join.is_none() {
*join = Some(tokio::spawn(async {}));
}
}
pub async fn stop(&self) -> Option<JoinHandle<()>> {
let join = self.join.lock().take();
if join.is_some() {
let _ = self.sender.send(ImportBlockMpsc::Stop);
}
join
}
pub fn subscribe(&self) -> broadcast::Receiver<ImportBlockBroadcast> {
self.broadcast.subscribe()
}
pub fn sender(&self) -> &mpsc::Sender<ImportBlockMpsc> {
&self.sender
}
}