Refactor pa_volume_interface to not poll inefficiently

This commit is contained in:
Moritz Ruth 2024-06-17 00:27:10 +02:00
parent 4d476c5e42
commit e2f4aac438
Signed by: moritzruth
GPG key ID: C9BBAB79405EE56D
11 changed files with 212 additions and 329 deletions

View file

@ -145,10 +145,14 @@ fn state_matches(target: &Target, state: &PaEntityState) -> bool {
}
async fn manage_knob(path: KnobPath, config: KnobConfig, mut events: broadcast::Receiver<(KnobPath, KnobEvent)>, pa_volume_interface: Arc<PaVolumeInterface>) {
let mut entity_state: Option<Arc<PaEntityState>> = None;
let (initial_state, mut volume_states) = pa_volume_interface.subscribe_to_state();
let mut entity_state: Option<Arc<PaEntityState>> = initial_state
.entities_by_id()
.values()
.find(|entity| state_matches(&config.target, entity))
.map(Arc::clone);
let update_knob_value = {
let config = &config;
let path = path.clone();
@ -198,14 +202,6 @@ async fn manage_knob(path: KnobPath, config: KnobConfig, mut events: broadcast::
}
};
if let Some(state) = initial_state {
entity_state = state
.entities_by_id()
.values()
.find(|entity| state_matches(&config.target, entity))
.map(Arc::clone);
}
loop {
tokio::select! {
Ok(volume_state) = volume_states.recv() => {