Merge pull request #47 from Ottatop/udev_fix

Reschedule ConnectForAllOutputs callbacks until stream exists
This commit is contained in:
Ottatop 2023-08-07 18:28:25 -05:00 committed by GitHub
commit a552c3abda
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 20 deletions

View file

@ -895,26 +895,32 @@ impl State<UdevData> {
// Run any connected callbacks
{
let clone = output.clone();
self.loop_handle.insert_idle(move |data| {
let stream = data
.state
.api_state
.stream
.as_ref()
.expect("Stream doesn't exist");
let mut stream = stream.lock().expect("Couldn't lock stream");
for callback_id in data.state.output_callback_ids.iter() {
crate::api::send_to_client(
&mut stream,
&OutgoingMsg::CallCallback {
callback_id: *callback_id,
args: Some(Args::ConnectForAllOutputs {
output_name: clone.name(),
}),
},
)
.expect("Send to client failed");
}
self.loop_handle.insert_idle(|data| {
crate::state::schedule(
data,
|dt| dt.state.api_state.stream.is_some(),
move |dt| {
let stream = dt
.state
.api_state
.stream
.as_ref()
.expect("Stream doesn't exist");
let mut stream = stream.lock().expect("Couldn't lock stream");
for callback_id in dt.state.output_callback_ids.iter() {
crate::api::send_to_client(
&mut stream,
&OutgoingMsg::CallCallback {
callback_id: *callback_id,
args: Some(Args::ConnectForAllOutputs {
output_name: clone.name(),
}),
},
)
.expect("Send to client failed");
}
},
)
});
}

View file

@ -738,6 +738,22 @@ pub fn schedule_on_commit<F, B: Backend>(
on_commit(data);
}
// Schedule something to be done when `condition` returns true.
pub fn schedule<F1, F2, B: Backend>(data: &mut CalloopData<B>, condition: F1, run: F2)
where
F1: Fn(&mut CalloopData<B>) -> bool + 'static,
F2: FnOnce(&mut CalloopData<B>) + 'static,
{
if !condition(data) {
data.state.loop_handle.insert_idle(|data| {
schedule(data, condition, run);
});
return;
}
run(data);
}
impl<B: Backend> State<B> {
pub fn init(
backend_data: B,