Add component getter

This commit is contained in:
Manuel Cillero 2023-10-08 21:14:03 +02:00
parent 33dff8f085
commit 0a03e293a2
2 changed files with 18 additions and 2 deletions

View file

@ -2,7 +2,7 @@ use crate::core::component::{ComponentTrait, Context};
use crate::html::{html, Markup}; use crate::html::{html, Markup};
use crate::{new_handle, Handle, Weight}; use crate::{new_handle, Handle, Weight};
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock, RwLockReadGuard};
new_handle!(COMPONENT_NULL for Crate); new_handle!(COMPONENT_NULL for Crate);
@ -37,10 +37,18 @@ impl ArcComponent {
ArcComponent(Arc::new(RwLock::new(component))) ArcComponent(Arc::new(RwLock::new(component)))
} }
// ArcComponent BUILDER.
pub fn set(&mut self, component: impl ComponentTrait) { pub fn set(&mut self, component: impl ComponentTrait) {
self.0 = Arc::new(RwLock::new(component)); self.0 = Arc::new(RwLock::new(component));
} }
// ArcComponent GETTERS.
pub fn get(&self) -> RwLockReadGuard<'_, dyn ComponentTrait> {
self.0.read().unwrap()
}
pub(crate) fn handle(&self) -> Handle { pub(crate) fn handle(&self) -> Handle {
self.0.read().unwrap().handle() self.0.read().unwrap().handle()
} }

View file

@ -2,7 +2,7 @@ use crate::core::component::{ComponentTrait, Context};
use crate::html::{html, Markup}; use crate::html::{html, Markup};
use crate::{Handle, Weight}; use crate::{Handle, Weight};
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock, RwLockReadGuard};
#[derive(Default)] #[derive(Default)]
pub struct TypedComponent<T: ComponentTrait + Default>(Arc<RwLock<T>>); pub struct TypedComponent<T: ComponentTrait + Default>(Arc<RwLock<T>>);
@ -22,10 +22,18 @@ impl<T: ComponentTrait + Default> TypedComponent<T> {
TypedComponent(Arc::new(RwLock::new(component))) TypedComponent(Arc::new(RwLock::new(component)))
} }
// TypedComponent BUILDER.
pub fn set(&mut self, component: T) { pub fn set(&mut self, component: T) {
self.0 = Arc::new(RwLock::new(component)); self.0 = Arc::new(RwLock::new(component));
} }
// TypedComponent GETTERS.
pub fn get(&self) -> RwLockReadGuard<'_, T> {
self.0.read().unwrap()
}
pub(crate) fn handle(&self) -> Handle { pub(crate) fn handle(&self) -> Handle {
self.0.read().unwrap().handle() self.0.read().unwrap().handle()
} }