cargo fmt -all
This commit is contained in:
@@ -12,3 +12,4 @@ common = { path = "../common" }
|
||||
iced = { workspace = true }
|
||||
tokio = { workspace = true }
|
||||
chrono = { workspace = true }
|
||||
reqwest = "0.13.5"
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use chrono::{DateTime, Local, Timelike};
|
||||
use iced::futures::{channel::mpsc, SinkExt, StreamExt};
|
||||
use iced::Subscription;
|
||||
|
||||
@@ -17,15 +18,15 @@ pub const NAMESPACE: &str = "clock.";
|
||||
|
||||
/// Drives the clock, publishing only the kind modules last asked for.
|
||||
pub struct ClockTicker {
|
||||
last: HashMap<ClockKind, String>,
|
||||
want: ClockKind,
|
||||
last: DateTime<Local>,
|
||||
// want: ClockKind,
|
||||
}
|
||||
|
||||
impl Default for ClockTicker {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
last: HashMap::new(),
|
||||
want: ClockKind::Seconds,
|
||||
last: DateTime::default(),
|
||||
// want: ClockKind::Seconds,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -40,58 +41,44 @@ impl ClockTicker {
|
||||
pub fn run(inbox: Inbox) -> Subscription<Wire> {
|
||||
Subscription::run_with(inbox, |inbox| {
|
||||
let key = inbox.key();
|
||||
// ponytail: the builder runs once (identity is stable, see `Inbox`
|
||||
// hashing). If a module ever makes `services()` dynamic and a key
|
||||
// is dropped then recreated, `take()` returns None and this panics
|
||||
// — make `Bar` rebuild the inbox per start instead.
|
||||
let mut rx = inbox.take().expect("inbox receiver is taken once");
|
||||
iced::stream::channel(
|
||||
0,
|
||||
move |mut sender: mpsc::Sender<Wire>| async move {
|
||||
let mut clock = ClockTicker::new();
|
||||
let mut interval = tokio::time::interval(std::time::Duration::from_secs(1));
|
||||
loop {
|
||||
tokio::select! {
|
||||
_ = interval.tick() => {
|
||||
if let Some(payload) = clock.tick() {
|
||||
let wire = Wire::topic(Endpoint::service(key), key, payload);
|
||||
if sender.send(wire).await.is_err() {
|
||||
return;
|
||||
}
|
||||
// We dont need to rx any msgs from modules
|
||||
let mut _rx = inbox.take().expect("inbox receiver is taken once");
|
||||
|
||||
iced::stream::channel(0, move |mut sender: mpsc::Sender<Wire>| async move {
|
||||
let mut clock = ClockTicker::new();
|
||||
let mut interval = tokio::time::interval(std::time::Duration::from_secs(1));
|
||||
loop {
|
||||
tokio::select! {
|
||||
_ = interval.tick() => {
|
||||
if let Some(payload) = clock.tick() {
|
||||
let wire = Wire::topic(Endpoint::service(key), key, payload);
|
||||
if sender.send(wire).await.is_err() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
msg = rx.next() => match msg {
|
||||
Some(wire) => {
|
||||
// A module poking us: switch published kind.
|
||||
if let Some(kind) = wire.downcast::<ClockKind>() {
|
||||
clock.want = *kind;
|
||||
clock.last.clear();
|
||||
}
|
||||
}
|
||||
None => return,
|
||||
},
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/// Latest value for the wanted kind, or `None` if unchanged.
|
||||
pub fn tick(&mut self) -> Option<ClockPayload> {
|
||||
let value = chrono::Local::now()
|
||||
.format(match self.want {
|
||||
ClockKind::Mins => "%H:%M",
|
||||
ClockKind::Seconds => "%H:%M:%S",
|
||||
})
|
||||
.to_string();
|
||||
if self.last.get(&self.want) == Some(&value) {
|
||||
return None;
|
||||
let value = chrono::Local::now();
|
||||
let meow = self.last;
|
||||
if meow.minute() != value.minute() {
|
||||
return Some(ClockPayload {
|
||||
kind: ClockKind::Mins,
|
||||
value: value.format("%H:%M:%S").to_string(),
|
||||
});
|
||||
}
|
||||
self.last.insert(self.want, value.clone());
|
||||
Some(ClockPayload {
|
||||
kind: self.want,
|
||||
value,
|
||||
})
|
||||
if meow.second() != value.second() {
|
||||
return Some(ClockPayload {
|
||||
kind: ClockKind::Seconds,
|
||||
value: value.format("%H:%M:%S").to_string(),
|
||||
});
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ use iced::Subscription;
|
||||
use common::{Inbox, Service, Wire};
|
||||
|
||||
pub mod datetime;
|
||||
pub mod weather;
|
||||
|
||||
/// Conversion from a route key + its inbox to the subscription backing it.
|
||||
///
|
||||
@@ -21,6 +22,7 @@ impl IntoSubscription for Service {
|
||||
fn into_subscription(self, inbox: Inbox) -> Subscription<Wire> {
|
||||
match self.0 {
|
||||
key if key.starts_with(datetime::NAMESPACE) => datetime::ClockTicker::run(inbox),
|
||||
key if key.starts_with(weather::NAMESPACE) => datetime::ClockTicker::run(inbox),
|
||||
_ => Subscription::none(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
pub struct WeatherService {}
|
||||
pub const NAMESPACE: &str = "weather.";
|
||||
Reference in New Issue
Block a user