Introduce SystemTime to Event in iced_sentinel
This commit is contained in:
parent
8591e5a148
commit
3d90665f9d
5 changed files with 74 additions and 28 deletions
|
|
@ -2,3 +2,4 @@
|
|||
|
||||
pub use web_time::Duration;
|
||||
pub use web_time::Instant;
|
||||
pub use web_time::SystemTime;
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@ pub fn time(window: window::Id, name: impl AsRef<str>) -> Timer {
|
|||
|
||||
#[cfg(feature = "enable")]
|
||||
mod internal {
|
||||
use crate::core::time::Instant;
|
||||
use crate::core::time::{Instant, SystemTime};
|
||||
use crate::core::window;
|
||||
use crate::style::theme;
|
||||
|
||||
|
|
@ -104,6 +104,7 @@ mod internal {
|
|||
Timer {
|
||||
stage,
|
||||
start: Instant::now(),
|
||||
start_system_time: SystemTime::now(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -111,12 +112,14 @@ mod internal {
|
|||
pub struct Timer {
|
||||
stage: timing::Stage,
|
||||
start: Instant,
|
||||
start_system_time: SystemTime,
|
||||
}
|
||||
|
||||
impl Timer {
|
||||
pub fn finish(self) {
|
||||
lock().sentinel.report_timing(Timing {
|
||||
stage: self.stage,
|
||||
start: self.start_system_time,
|
||||
duration: self.start.elapsed(),
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
use crate::core::time::SystemTime;
|
||||
use crate::style::theme;
|
||||
use crate::{Input, Timing, SOCKET_ADDRESS};
|
||||
|
||||
|
|
@ -13,7 +14,10 @@ pub struct Client {
|
|||
|
||||
impl Client {
|
||||
pub fn report_theme_change(&mut self, palette: theme::Palette) {
|
||||
let _ = self.sender.try_send(Input::ThemeChanged(palette));
|
||||
let _ = self.sender.try_send(Input::ThemeChanged {
|
||||
at: SystemTime::now(),
|
||||
palette,
|
||||
});
|
||||
}
|
||||
|
||||
pub fn report_timing(&mut self, timing: Timing) {
|
||||
|
|
@ -38,7 +42,14 @@ async fn run(mut receiver: mpsc::Receiver<Input>) {
|
|||
loop {
|
||||
match _connect().await {
|
||||
Ok(mut stream) => {
|
||||
let _ = send(&mut stream, Input::Connected(version)).await;
|
||||
let _ = send(
|
||||
&mut stream,
|
||||
Input::Connected {
|
||||
at: SystemTime::now(),
|
||||
version,
|
||||
},
|
||||
)
|
||||
.await;
|
||||
|
||||
while let Some(input) = receiver.recv().await {
|
||||
if send(&mut stream, input).await.is_err() {
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ pub use semver::Version;
|
|||
pub mod client;
|
||||
pub mod timing;
|
||||
|
||||
use crate::core::time::SystemTime;
|
||||
use crate::style::theme;
|
||||
use crate::timing::Timing;
|
||||
|
||||
|
|
@ -18,17 +19,42 @@ pub const SOCKET_ADDRESS: &str = "127.0.0.1:9167";
|
|||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub enum Input {
|
||||
Connected(Version),
|
||||
Connected {
|
||||
at: SystemTime,
|
||||
version: Version,
|
||||
},
|
||||
ThemeChanged {
|
||||
at: SystemTime,
|
||||
palette: theme::Palette,
|
||||
},
|
||||
TimingMeasured(Timing),
|
||||
ThemeChanged(theme::Palette),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Event {
|
||||
Connected(Version),
|
||||
Disconnected,
|
||||
Connected {
|
||||
at: SystemTime,
|
||||
version: Version,
|
||||
},
|
||||
Disconnected {
|
||||
at: SystemTime,
|
||||
},
|
||||
ThemeChanged {
|
||||
at: SystemTime,
|
||||
palette: theme::Palette,
|
||||
},
|
||||
TimingMeasured(Timing),
|
||||
ThemeChanged(theme::Palette),
|
||||
}
|
||||
|
||||
impl Event {
|
||||
pub fn at(&self) -> SystemTime {
|
||||
match self {
|
||||
Self::Connected { at, .. }
|
||||
| Self::Disconnected { at }
|
||||
| Self::ThemeChanged { at, .. } => *at,
|
||||
Self::TimingMeasured(timing) => timing.start,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run() -> impl Stream<Item = Event> {
|
||||
|
|
@ -48,12 +74,27 @@ pub fn run() -> impl Stream<Item = Event> {
|
|||
Err(_error) => Some((None, State::Disconnected)),
|
||||
},
|
||||
State::Connected(stream) => match receive(stream).await {
|
||||
Ok((_, Event::Disconnected)) | Err(_) => {
|
||||
Some((Some(Event::Disconnected), State::Disconnected))
|
||||
Ok((stream, input)) => {
|
||||
let event = match dbg!(input) {
|
||||
Input::Connected { at, version } => {
|
||||
Event::Connected { at, version }
|
||||
}
|
||||
Ok((stream, message)) => {
|
||||
Some((Some(message), State::Connected(stream)))
|
||||
Input::TimingMeasured(timing) => {
|
||||
Event::TimingMeasured(timing)
|
||||
}
|
||||
Input::ThemeChanged { at, palette } => {
|
||||
Event::ThemeChanged { at, palette }
|
||||
}
|
||||
};
|
||||
|
||||
Some((Some(event), State::Connected(stream)))
|
||||
}
|
||||
Err(_) => Some((
|
||||
Some(Event::Disconnected {
|
||||
at: SystemTime::now(),
|
||||
}),
|
||||
State::Disconnected,
|
||||
)),
|
||||
},
|
||||
}
|
||||
})
|
||||
|
|
@ -73,7 +114,7 @@ async fn connect() -> Result<net::TcpStream, io::Error> {
|
|||
|
||||
async fn receive(
|
||||
mut stream: BufStream<net::TcpStream>,
|
||||
) -> Result<(BufStream<net::TcpStream>, Event), io::Error> {
|
||||
) -> Result<(BufStream<net::TcpStream>, Input), io::Error> {
|
||||
let mut bytes = Vec::new();
|
||||
|
||||
loop {
|
||||
|
|
@ -87,21 +128,10 @@ async fn receive(
|
|||
|
||||
match bincode::deserialize(&bytes) {
|
||||
Ok(input) => {
|
||||
return Ok((
|
||||
stream,
|
||||
match dbg!(input) {
|
||||
Input::Connected(version) => Event::Connected(version),
|
||||
Input::TimingMeasured(timing) => {
|
||||
Event::TimingMeasured(timing)
|
||||
}
|
||||
Input::ThemeChanged(palette) => {
|
||||
Event::ThemeChanged(palette)
|
||||
}
|
||||
},
|
||||
));
|
||||
return Ok((stream, input));
|
||||
}
|
||||
Err(_) => {
|
||||
// TODO: Log decoding error
|
||||
log::warn!("Error decoding sentinel message");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use crate::core::time::Duration;
|
||||
use crate::core::time::{Duration, SystemTime};
|
||||
use crate::core::window;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
|
@ -8,6 +8,7 @@ use serde::{Deserialize, Serialize};
|
|||
)]
|
||||
pub struct Timing {
|
||||
pub stage: Stage,
|
||||
pub start: SystemTime,
|
||||
pub duration: Duration,
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue