fix(input): reduce latency by several frames

This commit is contained in:
Nova
2023-01-14 11:03:47 -05:00
parent 795780130c
commit 457ff55904
2 changed files with 23 additions and 18 deletions

View File

@@ -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 {

View File

@@ -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<u8>) -> Result<Vec<u8>> {
pub fn execute_remote_method(
&self,
method: &str,
data: Vec<u8>,
) -> Result<impl Future<Output = Result<Vec<u8>>>> {
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)) })
}
}