BlueGit

ict rusty-teams

Files

rusty-teams / crates / chatsvc / src / realtime / mod.rs

mod.rs

mod.rs 3.1 KiB Download Raw
1mod event;
2mod host;
3mod runner;
4
5use std::pin::Pin;
6use std::task::{Context, Poll};
7use std::time::Duration;
8
9use futures_util::Stream;
10use session::Session;
11use tokio::sync::{mpsc, oneshot};
12use tokio::task::JoinHandle;
13
14pub use event::{
15 EventKind, MessageEvent, PresenceUpdate, RealtimeEvent, StatusEvent, StatusKind,
16 TrouterEndpoint, decode_payload,
17};
18pub use host::{DEFAULT_TROUTER_HOST, is_trouter_host};
19
20use crate::error::Result;
21use runner::{Runner, attach};
22
23const STOP_TIMEOUT: Duration = Duration::from_secs(15);
24
25#[derive(Debug, Clone)]
26pub struct RealtimeConfig {
27 pub host: Option<String>,
28 pub default_host: String,
29 pub discover_host: bool,
30 pub ensure_interval: Duration,
31 pub navigation_settle: Duration,
32 pub max_reattach_backoff: Duration,
33}
34
35impl Default for RealtimeConfig {
36 fn default() -> Self {
37 RealtimeConfig {
38 host: None,
39 default_host: DEFAULT_TROUTER_HOST.to_owned(),
40 discover_host: true,
41 ensure_interval: Duration::from_secs(30),
42 navigation_settle: Duration::from_millis(1500),
43 max_reattach_backoff: Duration::from_secs(30),
44 }
45 }
46}
47
48pub struct Realtime {
49 host: String,
50 events: mpsc::UnboundedReceiver<RealtimeEvent>,
51 stop: Option<oneshot::Sender<oneshot::Sender<()>>>,
52 task: JoinHandle<()>,
53}
54
55impl Realtime {
56 /// Works on a parked tab (same origin, localStorage readable); the CDP attachment keeps it from being parked.
57 pub async fn start(session: &Session) -> Result<Realtime> {
58 Self::start_with(session, RealtimeConfig::default()).await
59 }
60
61 pub async fn start_with(session: &Session, config: RealtimeConfig) -> Result<Realtime> {
62 let session = session.clone();
63 let host = host::resolve(&session, &config).await;
64 let attached = attach(&session).await?;
65 let (output, events) = mpsc::unbounded_channel();
66 let (stop, stop_receiver) = oneshot::channel();
67 let mut runner = Runner::new(session, config, host.clone(), output);
68 if let Err(error) = runner.ensure().await {
69 attached.shutdown().await;
70 return Err(error.into());
71 }
72 let task = tokio::spawn(runner.run(attached, stop_receiver));
73 Ok(Realtime {
74 host,
75 events,
76 stop: Some(stop),
77 task,
78 })
79 }
80
81 pub fn host(&self) -> &str {
82 &self.host
83 }
84
85 pub async fn recv(&mut self) -> Option<RealtimeEvent> {
86 self.events.recv().await
87 }
88
89 /// Deletes the registration and closes the page-side socket.
90 pub async fn stop(mut self) {
91 if let Some(stop) = self.stop.take() {
92 let (done, finished) = oneshot::channel();
93 if stop.send(done).is_ok() {
94 let _ = tokio::time::timeout(STOP_TIMEOUT, finished).await;
95 }
96 }
97 let _ = self.task.await;
98 }
99}
100
101impl Stream for Realtime {
102 type Item = RealtimeEvent;
103
104 fn poll_next(
105 mut self: Pin<&mut Self>,
106 context: &mut Context<'_>,
107 ) -> Poll<Option<RealtimeEvent>> {
108 self.events.poll_recv(context)
109 }
110}