feat(alias): add it
This commit is contained in:
@@ -1,9 +1,10 @@
|
|||||||
use super::data::{PulseReceiver, PulseSender};
|
use super::data::PulseSender;
|
||||||
use super::field::Field;
|
use super::field::Field;
|
||||||
use super::spatial::Spatial;
|
use super::spatial::Spatial;
|
||||||
use crate::core::client::Client;
|
use crate::core::client::Client;
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use libstardustxr::scenegraph::ScenegraphError;
|
use libstardustxr::scenegraph::ScenegraphError;
|
||||||
|
use rccell::{RcCell, WeakCell};
|
||||||
use std::rc::{Rc, Weak};
|
use std::rc::{Rc, Weak};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::{collections::HashMap, vec::Vec};
|
use std::{collections::HashMap, vec::Vec};
|
||||||
@@ -24,6 +25,7 @@ pub struct Node<'a> {
|
|||||||
local_methods: HashMap<String, Method, BuildHasherDefault<FxHasher>>,
|
local_methods: HashMap<String, Method, BuildHasherDefault<FxHasher>>,
|
||||||
destroyable: bool,
|
destroyable: bool,
|
||||||
|
|
||||||
|
alias: Option<Alias<'a>>,
|
||||||
pub spatial: Option<Rc<Spatial>>,
|
pub spatial: Option<Rc<Spatial>>,
|
||||||
pub field: Option<Rc<Field>>,
|
pub field: Option<Rc<Field>>,
|
||||||
pub pulse_sender: Option<Arc<PulseSender>>,
|
pub pulse_sender: Option<Arc<PulseSender>>,
|
||||||
@@ -56,6 +58,7 @@ impl<'a> Node<'a> {
|
|||||||
local_methods: Default::default(),
|
local_methods: Default::default(),
|
||||||
destroyable,
|
destroyable,
|
||||||
|
|
||||||
|
alias: None,
|
||||||
spatial: None,
|
spatial: None,
|
||||||
field: None,
|
field: None,
|
||||||
pulse_sender: None,
|
pulse_sender: None,
|
||||||
@@ -89,11 +92,24 @@ impl<'a> Node<'a> {
|
|||||||
method: &str,
|
method: &str,
|
||||||
data: &[u8],
|
data: &[u8],
|
||||||
) -> Result<(), ScenegraphError> {
|
) -> Result<(), ScenegraphError> {
|
||||||
let signal = self
|
if let Some(alias) = self.alias.as_ref() {
|
||||||
.local_signals
|
if !alias.signals.contains(&method.to_string()) {
|
||||||
.get(method)
|
return Err(ScenegraphError::SignalNotFound);
|
||||||
.ok_or(ScenegraphError::SignalNotFound)?;
|
}
|
||||||
signal(self, calling_client, data).map_err(|error| ScenegraphError::SignalError { error })
|
alias
|
||||||
|
.original
|
||||||
|
.upgrade()
|
||||||
|
.ok_or_else(|| ScenegraphError::BrokenAlias)?
|
||||||
|
.borrow()
|
||||||
|
.send_local_signal(calling_client, method, data)
|
||||||
|
} else {
|
||||||
|
let signal = self
|
||||||
|
.local_signals
|
||||||
|
.get(method)
|
||||||
|
.ok_or(ScenegraphError::SignalNotFound)?;
|
||||||
|
signal(self, calling_client, data)
|
||||||
|
.map_err(|error| ScenegraphError::SignalError { error })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
pub fn execute_local_method(
|
pub fn execute_local_method(
|
||||||
&self,
|
&self,
|
||||||
@@ -101,11 +117,24 @@ impl<'a> Node<'a> {
|
|||||||
method: &str,
|
method: &str,
|
||||||
data: &[u8],
|
data: &[u8],
|
||||||
) -> Result<Vec<u8>, ScenegraphError> {
|
) -> Result<Vec<u8>, ScenegraphError> {
|
||||||
let method = self
|
if let Some(alias) = self.alias.as_ref() {
|
||||||
.local_methods
|
if !alias.methods.contains(&method.to_string()) {
|
||||||
.get(method)
|
return Err(ScenegraphError::MethodNotFound);
|
||||||
.ok_or(ScenegraphError::MethodNotFound)?;
|
}
|
||||||
method(self, calling_client, data).map_err(|error| ScenegraphError::MethodError { error })
|
alias
|
||||||
|
.original
|
||||||
|
.upgrade()
|
||||||
|
.ok_or_else(|| ScenegraphError::BrokenAlias)?
|
||||||
|
.borrow()
|
||||||
|
.execute_local_method(calling_client, method, data)
|
||||||
|
} else {
|
||||||
|
let method = self
|
||||||
|
.local_methods
|
||||||
|
.get(method)
|
||||||
|
.ok_or(ScenegraphError::MethodNotFound)?;
|
||||||
|
method(self, calling_client, data)
|
||||||
|
.map_err(|error| ScenegraphError::MethodError { error })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
pub fn send_remote_signal(&self, method: &str, data: &[u8]) -> Result<()> {
|
pub fn send_remote_signal(&self, method: &str, data: &[u8]) -> Result<()> {
|
||||||
self.get_client()
|
self.get_client()
|
||||||
@@ -127,3 +156,24 @@ impl<'a> Node<'a> {
|
|||||||
// .map_err(|_| anyhow!("Unable to write in messenger"))
|
// .map_err(|_| anyhow!("Unable to write in messenger"))
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct Alias<'a> {
|
||||||
|
original: WeakCell<Node<'a>>,
|
||||||
|
|
||||||
|
signals: Vec<String>,
|
||||||
|
methods: Vec<String>,
|
||||||
|
}
|
||||||
|
impl<'a> Alias<'a> {
|
||||||
|
pub fn add_to(
|
||||||
|
node: &RcCell<Node<'a>>,
|
||||||
|
original: &RcCell<Node<'a>>,
|
||||||
|
signals: Vec<String>,
|
||||||
|
methods: Vec<String>,
|
||||||
|
) {
|
||||||
|
node.borrow_mut().alias = Some(Alias {
|
||||||
|
original: original.downgrade(),
|
||||||
|
signals,
|
||||||
|
methods,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user