diff --git a/src/nodes/input/mod.rs b/src/nodes/input/mod.rs index 829950d..170c896 100644 --- a/src/nodes/input/mod.rs +++ b/src/nodes/input/mod.rs @@ -191,24 +191,25 @@ impl InputHandler { let method = Arc::downgrade(&distance_link.method); let handler = Arc::downgrade(&distance_link.handler); - tokio::spawn(async move { - let data = node.execute_remote_method("input", data).await; - if frame == FRAME.load(Ordering::Relaxed) { - if let Ok(data) = data { - let capture = flexbuffers::Reader::get_root(data.as_slice()) - .and_then(|data| data.get_bool()) - .unwrap_or(false); + if let Ok(data) = node.execute_remote_method("input", data) { + tokio::spawn(async move { + if let Ok(data) = data.await { + if frame == FRAME.load(Ordering::Relaxed) { + let capture = flexbuffers::Reader::get_root(data.as_slice()) + .and_then(|data| data.get_bool()) + .unwrap_or(false); - if let Some(method) = method.upgrade() { - if let Some(handler) = handler.upgrade() { - if capture { - method.captures.add_raw(&handler); + if let Some(method) = method.upgrade() { + if let Some(handler) = handler.upgrade() { + if capture { + method.captures.add_raw(&handler); + } } } } } - } - }); + }); + } } } impl Drop for InputHandler { diff --git a/src/nodes/mod.rs b/src/nodes/mod.rs index 23077f7..aa1630e 100644 --- a/src/nodes/mod.rs +++ b/src/nodes/mod.rs @@ -15,6 +15,7 @@ use once_cell::sync::OnceCell; use parking_lot::Mutex; use stardust_xr::messenger::MessageSenderHandle; use stardust_xr::scenegraph::ScenegraphError; +use std::future::Future; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Weak}; use std::vec::Vec; @@ -235,15 +236,18 @@ impl Node { .transpose()?; Ok(()) } - pub async fn execute_remote_method(&self, method: &str, data: Vec) -> Result> { + pub fn execute_remote_method( + &self, + method: &str, + data: Vec, + ) -> Result>>> { let message_sender_handle = self .message_sender_handle .as_ref() .ok_or(eyre!("Messenger does not exist for this node"))?; - message_sender_handle - .method(self.path.as_str(), method, &data)? - .await - .map_err(|e| eyre!(e)) + let future = message_sender_handle.method(self.path.as_str(), method, &data)?; + + Ok(async { future.await.map_err(|e| eyre!(e)) }) } }