From 1f371fe4970b42d040eb6b8bfca20dcfcadea671 Mon Sep 17 00:00:00 2001 From: Nova Date: Thu, 13 Oct 2022 00:29:50 -0400 Subject: [PATCH] fix(node): get client returns option --- src/nodes/drawable/model.rs | 9 ++++++++- src/nodes/drawable/text.rs | 10 ++++++---- src/nodes/items/mod.rs | 32 ++++++++++++++++++-------------- src/nodes/mod.rs | 34 ++++++++++++++++++++-------------- 4 files changed, 52 insertions(+), 33 deletions(-) diff --git a/src/nodes/drawable/model.rs b/src/nodes/drawable/model.rs index e904678..784be8f 100644 --- a/src/nodes/drawable/model.rs +++ b/src/nodes/drawable/model.rs @@ -62,7 +62,14 @@ impl Model { let _ = model_arc.pending_model_path.set( model_arc .resource_id - .get_file(&node.get_client().base_resource_prefixes.lock().clone()) + .get_file( + &node + .get_client() + .ok_or_else(|| anyhow!("Client not found"))? + .base_resource_prefixes + .lock() + .clone(), + ) .ok_or_else(|| anyhow!("Resource not found"))?, ); let _ = node.model.set(model_arc.clone()); diff --git a/src/nodes/drawable/text.rs b/src/nodes/drawable/text.rs index 27c295b..d86bc1b 100644 --- a/src/nodes/drawable/text.rs +++ b/src/nodes/drawable/text.rs @@ -5,7 +5,7 @@ use crate::{ Node, }, }; -use anyhow::{ensure, Result}; +use anyhow::{anyhow, ensure, Result}; use glam::{vec3, Mat4, Vec2}; use mint::Vector2; use once_cell::sync::OnceCell; @@ -63,11 +63,13 @@ impl Text { "Internal: Node already has text attached!" ); + let client = node + .get_client() + .ok_or_else(|| anyhow!("Client not found"))?; let text = TEXT_REGISTRY.add(Text { space: node.spatial.get().unwrap().clone(), - font_path: font_resource_id.and_then(|res| { - res.get_file(&node.get_client().base_resource_prefixes.lock().clone()) - }), + font_path: font_resource_id + .and_then(|res| res.get_file(&client.base_resource_prefixes.lock().clone())), style: OnceCell::new(), data: Mutex::new(TextData { diff --git a/src/nodes/items/mod.rs b/src/nodes/items/mod.rs index 72fa299..19600ff 100644 --- a/src/nodes/items/mod.rs +++ b/src/nodes/items/mod.rs @@ -211,14 +211,16 @@ impl ItemUI { fn handle_create_item(&self, item: &Item) { let node = self.node.upgrade().unwrap(); - let (alias_node, _) = - item.make_alias(&node.get_client(), &(node.get_path().to_string() + "/item")); - self.aliases.add(Arc::downgrade(&alias_node)); + if let Some(client) = node.get_client() { + let (alias_node, _) = + item.make_alias(&client, &(node.get_path().to_string() + "/item")); + self.aliases.add(Arc::downgrade(&alias_node)); - let _ = node.send_remote_signal( - "create", - &item.specialization.serialize_start_data(&item.uid), - ); + let _ = node.send_remote_signal( + "create", + &item.specialization.serialize_start_data(&item.uid), + ); + } } fn handle_destroy_item(&self, item: &Item) { self.send_state("destroy", item.uid.as_str()); @@ -231,13 +233,15 @@ impl ItemUI { } fn handle_create_acceptor(&self, acceptor: &ItemAcceptor) { let node = self.node.upgrade().unwrap(); - let aliases = acceptor.make_aliases( - &node.get_client(), - &format!("/item/{}/acceptor", self.type_info.type_name), - ); - aliases - .iter() - .for_each(|alias| self.aliases.add(Arc::downgrade(alias))); + if let Some(client) = node.get_client() { + let aliases = acceptor.make_aliases( + &client, + &format!("/item/{}/acceptor", self.type_info.type_name), + ); + aliases + .iter() + .for_each(|alias| self.aliases.add(Arc::downgrade(alias))); + } } fn handle_destroy_acceptor(&self, _acceptor: &ItemAcceptor) {} } diff --git a/src/nodes/mod.rs b/src/nodes/mod.rs index 39cdafe..fc02681 100644 --- a/src/nodes/mod.rs +++ b/src/nodes/mod.rs @@ -77,8 +77,8 @@ pub struct Node { } impl Node { - pub fn get_client(&self) -> Arc { - self.client.upgrade().unwrap() + pub fn get_client(&self) -> Option> { + self.client.upgrade() } // pub fn get_name(&self) -> &str { // &self.path[self.trailing_slash_pos + 1..] @@ -123,10 +123,12 @@ impl Node { node } pub fn add_to_scenegraph(self) -> Arc { - self.get_client().scenegraph.add_node(self) + self.get_client().unwrap().scenegraph.add_node(self) } pub fn destroy(&self) { - let _ = self.get_client().scenegraph.remove_node(self.get_path()); + let _ = self + .get_client() + .map(|c| c.scenegraph.remove_node(self.get_path())); } pub fn destroy_flex(node: &Node, _calling_client: Arc, _data: &[u8]) -> Result<()> { @@ -203,24 +205,28 @@ impl Node { .unwrap() .send_remote_signal(method, data); }); - let client = self.get_client(); let path = self.path.clone(); let method = method.to_string(); let data = data.to_vec(); - - if let Some(messenger) = client.messenger.as_ref() { - messenger.send_remote_signal(path.as_str(), method.as_str(), data.as_slice()); + if let Some(client) = self.get_client() { + if let Some(messenger) = client.messenger.as_ref() { + messenger.send_remote_signal(path.as_str(), method.as_str(), data.as_slice()); + } } Ok(()) } pub async fn execute_remote_method(&self, method: &str, data: Vec) -> Result> { - match self.get_client().messenger.as_ref() { - None => Err(anyhow!("Messenger does not exist for this node's client")), - Some(messenger) => { - messenger - .execute_remote_method(self.path.as_str(), method, &data) - .await + if let Some(client) = self.get_client() { + match client.messenger.as_ref() { + None => Err(anyhow!("Messenger does not exist for this node's client")), + Some(messenger) => { + messenger + .execute_remote_method(self.path.as_str(), method, &data) + .await + } } + } else { + Err(anyhow!("Client does not exist somehow?")) } } }