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