From 00ca4564be2420413a802375b25906d10ee2b87d Mon Sep 17 00:00:00 2001 From: Ottatop Date: Mon, 7 Aug 2023 12:58:06 -0500 Subject: [PATCH] Reschedule ConnectForAllOutputs callbacks until stream exists --- src/backend/udev.rs | 46 +++++++++++++++++++++++++-------------------- src/state.rs | 16 ++++++++++++++++ 2 files changed, 42 insertions(+), 20 deletions(-) diff --git a/src/backend/udev.rs b/src/backend/udev.rs index 94ddf48..99885e2 100644 --- a/src/backend/udev.rs +++ b/src/backend/udev.rs @@ -895,26 +895,32 @@ impl State { // 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"); + } + }, + ) }); } diff --git a/src/state.rs b/src/state.rs index c6a5697..616dc99 100644 --- a/src/state.rs +++ b/src/state.rs @@ -738,6 +738,22 @@ pub fn schedule_on_commit( on_commit(data); } +// Schedule something to be done when `condition` returns true. +pub fn schedule(data: &mut CalloopData, condition: F1, run: F2) +where + F1: Fn(&mut CalloopData) -> bool + 'static, + F2: FnOnce(&mut CalloopData) + 'static, +{ + if !condition(data) { + data.state.loop_handle.insert_idle(|data| { + schedule(data, condition, run); + }); + return; + } + + run(data); +} + impl State { pub fn init( backend_data: B,