refactor(battery): use channel subscription
This commit is contained in:
parent
ff6e9e3483
commit
8d9bb40b1b
5 changed files with 127 additions and 174 deletions
|
|
@ -3,9 +3,12 @@
|
|||
//! This code was generated by `zbus-xmlgen` `2.0.1` from DBus introspection data.
|
||||
//! Source: `Interface '/org/freedesktop/UPower/devices/DisplayDevice' from service 'org.freedesktop.UPower' on system bus`.
|
||||
|
||||
use cosmic::iced::{self, subscription};
|
||||
use cosmic::iced::{
|
||||
self,
|
||||
futures::{SinkExt, StreamExt},
|
||||
subscription,
|
||||
};
|
||||
|
||||
use futures::StreamExt;
|
||||
use std::{fmt::Debug, hash::Hash};
|
||||
use zbus::dbus_proxy;
|
||||
|
||||
|
|
@ -152,8 +155,14 @@ trait Device {
|
|||
|
||||
pub fn device_subscription<I: 'static + Hash + Copy + Send + Sync + Debug>(
|
||||
id: I,
|
||||
) -> iced::Subscription<(I, DeviceDbusEvent)> {
|
||||
subscription::unfold(id, State::Ready, move |state| start_listening_loop(id, state))
|
||||
) -> iced::Subscription<DeviceDbusEvent> {
|
||||
subscription::channel(id, 50, move |mut output| async move {
|
||||
let mut state = State::Ready;
|
||||
|
||||
loop {
|
||||
state = start_listening(state, &mut output).await;
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
|
@ -174,45 +183,32 @@ async fn display_device() -> zbus::Result<DeviceProxy<'static>> {
|
|||
.await
|
||||
}
|
||||
|
||||
async fn start_listening_loop<I: Copy + Debug>(
|
||||
id: I,
|
||||
mut state: State,
|
||||
) -> ((I, DeviceDbusEvent), State) {
|
||||
loop {
|
||||
let (update, new_state) = start_listening(id, state).await;
|
||||
state = new_state;
|
||||
if let Some(update) = update {
|
||||
return (update, state);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn start_listening<I: Copy>(id: I, state: State) -> (Option<(I, DeviceDbusEvent)>, State) {
|
||||
async fn start_listening(
|
||||
state: State,
|
||||
output: &mut futures::channel::mpsc::Sender<DeviceDbusEvent>,
|
||||
) -> State {
|
||||
match state {
|
||||
State::Ready => {
|
||||
if let Ok(device) = display_device().await {
|
||||
return (
|
||||
Some((
|
||||
id,
|
||||
DeviceDbusEvent::Update {
|
||||
icon_name: device
|
||||
.cached_icon_name()
|
||||
.unwrap_or_default()
|
||||
.unwrap_or_default(),
|
||||
percent: device
|
||||
.cached_percentage()
|
||||
.unwrap_or_default()
|
||||
.unwrap_or_default(),
|
||||
time_to_empty: device
|
||||
.cached_time_to_empty()
|
||||
.unwrap_or_default()
|
||||
.unwrap_or_default(),
|
||||
},
|
||||
)),
|
||||
State::Waiting(device),
|
||||
);
|
||||
_ = output
|
||||
.send(DeviceDbusEvent::Update {
|
||||
icon_name: device
|
||||
.cached_icon_name()
|
||||
.unwrap_or_default()
|
||||
.unwrap_or_default(),
|
||||
percent: device
|
||||
.cached_percentage()
|
||||
.unwrap_or_default()
|
||||
.unwrap_or_default(),
|
||||
time_to_empty: device
|
||||
.cached_time_to_empty()
|
||||
.unwrap_or_default()
|
||||
.unwrap_or_default(),
|
||||
})
|
||||
.await;
|
||||
return State::Waiting(device);
|
||||
}
|
||||
(None, State::Finished)
|
||||
State::Finished
|
||||
}
|
||||
State::Waiting(device) => {
|
||||
let mut stream = futures::stream_select!(
|
||||
|
|
@ -221,10 +217,9 @@ async fn start_listening<I: Copy>(id: I, state: State) -> (Option<(I, DeviceDbus
|
|||
device.receive_time_to_empty_changed().await.map(|_| ()),
|
||||
);
|
||||
match stream.next().await {
|
||||
Some(_) => (
|
||||
Some((
|
||||
id,
|
||||
DeviceDbusEvent::Update {
|
||||
Some(_) => {
|
||||
_ = output
|
||||
.send(DeviceDbusEvent::Update {
|
||||
icon_name: device
|
||||
.cached_icon_name()
|
||||
.unwrap_or_default()
|
||||
|
|
@ -237,11 +232,12 @@ async fn start_listening<I: Copy>(id: I, state: State) -> (Option<(I, DeviceDbus
|
|||
.cached_time_to_empty()
|
||||
.unwrap_or_default()
|
||||
.unwrap_or_default(),
|
||||
},
|
||||
)),
|
||||
State::Waiting(device),
|
||||
),
|
||||
None => (None, State::Finished),
|
||||
})
|
||||
.await;
|
||||
|
||||
State::Waiting(device)
|
||||
}
|
||||
None => State::Finished,
|
||||
}
|
||||
}
|
||||
State::Finished => iced::futures::future::pending().await,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue