1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-07-01 18:51:04 +02:00

refactor: extract selective state update macro

This commit is contained in:
Reinout Meliesie 2024-06-19 21:24:27 +02:00
parent 465d16800a
commit a9cd5f5489
Signed by: zedfrigg
GPG key ID: 3AFCC06481308BC6

View file

@ -60,6 +60,17 @@ impl Client {
} }
fn run(&self) -> Result<()> { fn run(&self) -> Result<()> {
macro_rules! update_state_for_device_change {
($client:ident) => {
$client.state.set(State {
wired: determine_wired_state(&read_lock!($client.devices))?,
wifi: determine_wifi_state(&read_lock!($client.devices))?,
cellular: determine_cellular_state(&read_lock!($client.devices))?,
vpn: $client.state.get_cloned().vpn,
});
};
}
macro_rules! initialise_path_map { macro_rules! initialise_path_map {
( (
$client:expr, $client:expr,
@ -88,8 +99,9 @@ impl Client {
$client:expr, $client:expr,
$property:ident, $property:ident,
$property_changes:ident, $property_changes:ident,
$proxy_type:ident $proxy_type:ident,
$(, |$inner_client:ident, $new_path:ident| $property_watcher:expr)* |$state_client:ident| $state_update:expr
$(, |$property_client:ident, $new_path:ident| $property_watcher:expr)*
) => { ) => {
let client = $client.clone(); let client = $client.clone();
spawn_blocking_result!({ spawn_blocking_result!({
@ -111,7 +123,7 @@ impl Client {
.build()?; .build()?;
new_path_map.insert(new_path.clone(), new_proxy); new_path_map.insert(new_path.clone(), new_proxy);
$({ $({
let $inner_client = &client; let $property_client = &client;
let $new_path = &new_path; let $new_path = &new_path;
$property_watcher; $property_watcher;
})* })*
@ -119,13 +131,10 @@ impl Client {
} }
} }
*write_lock!(client.$property) = new_path_map; *write_lock!(client.$property) = new_path_map;
client.state.set(State { {
// TODO: Investigate if there's a sane way to do only the relevant updates let $state_client = &client;
wired: determine_wired_state(&read_lock!(client.devices))?, $state_update;
wifi: determine_wifi_state(&read_lock!(client.devices))?, }
cellular: determine_cellular_state(&read_lock!(client.devices))?,
vpn: determine_vpn_state(&read_lock!(client.active_connections))?,
});
} }
Ok(()) Ok(())
}); });
@ -168,12 +177,7 @@ impl Client {
); );
initialise_path_map!(self.0, devices, DeviceDbusProxyBlocking, |path| { initialise_path_map!(self.0, devices, DeviceDbusProxyBlocking, |path| {
spawn_property_watcher!(self.0, path, receive_state_changed, devices, |client| { spawn_property_watcher!(self.0, path, receive_state_changed, devices, |client| {
client.state.set(State { update_state_for_device_change!(client);
wired: determine_wired_state(&read_lock!(client.devices))?,
wifi: determine_wifi_state(&read_lock!(client.devices))?,
cellular: determine_cellular_state(&read_lock!(client.devices))?,
vpn: client.state.get_cloned().vpn,
});
}); });
}); });
self.0.state.set(State { self.0.state.set(State {
@ -187,21 +191,27 @@ impl Client {
self.0, self.0,
active_connections, active_connections,
receive_active_connections_changed, receive_active_connections_changed,
ActiveConnectionDbusProxyBlocking ActiveConnectionDbusProxyBlocking,
|client| {
client.state.set(State {
wired: client.state.get_cloned().wired,
wifi: client.state.get_cloned().wifi,
cellular: client.state.get_cloned().cellular,
vpn: determine_vpn_state(&read_lock!(client.active_connections))?,
});
}
); );
spawn_path_list_watcher!( spawn_path_list_watcher!(
self.0, self.0,
devices, devices,
receive_devices_changed, receive_devices_changed,
DeviceDbusProxyBlocking, DeviceDbusProxyBlocking,
|client| {
update_state_for_device_change!(client);
},
|client, path| { |client, path| {
spawn_property_watcher!(client, path, receive_state_changed, devices, |client| { spawn_property_watcher!(client, path, receive_state_changed, devices, |client| {
client.state.set(State { update_state_for_device_change!(client);
wired: determine_wired_state(&read_lock!(client.devices))?,
wifi: determine_wifi_state(&read_lock!(client.devices))?,
cellular: determine_cellular_state(&read_lock!(client.devices))?,
vpn: client.state.get_cloned().vpn,
});
}); });
} }
); );