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::{error::Error, shared_components::SyncState};
use futures::{
channel::{mpsc, oneshot},
future::Future,
SinkExt,
};
pub enum CoordinatorMessage {
GetSyncState(oneshot::Sender<SyncState>), WaitForInitialization(oneshot::Sender<Result<(), Error>>), }
pub struct StateSyncClient {
coordinator_sender: mpsc::UnboundedSender<CoordinatorMessage>,
}
impl StateSyncClient {
pub fn new(coordinator_sender: mpsc::UnboundedSender<CoordinatorMessage>) -> Self {
Self { coordinator_sender }
}
pub fn get_state(&self) -> impl Future<Output = Result<SyncState, Error>> {
let mut sender = self.coordinator_sender.clone();
let (cb_sender, cb_receiver) = oneshot::channel();
async move {
sender
.send(CoordinatorMessage::GetSyncState(cb_sender))
.await?;
cb_receiver.await.map_err(|error| error.into())
}
}
pub fn wait_until_initialized(&self) -> impl Future<Output = Result<(), Error>> {
let mut sender = self.coordinator_sender.clone();
let (cb_sender, cb_receiver) = oneshot::channel();
async move {
sender
.send(CoordinatorMessage::WaitForInitialization(cb_sender))
.await?;
cb_receiver.await?
}
}
}