🔥 Drop Default, always refer existing components

This commit is contained in:
Manuel Cillero 2023-11-16 18:13:36 +01:00
parent ced06b0b62
commit 3be7b23d0a
2 changed files with 84 additions and 94 deletions

View file

@ -1,29 +1,12 @@
use crate::core::component::{ComponentTrait, Context}; use crate::core::component::{ComponentTrait, Context};
use crate::html::{html, Markup}; use crate::html::{html, Markup};
use crate::{fn_builder, impl_handle, Handle, Weight}; use crate::{fn_builder, Handle, Weight};
use std::sync::{Arc, RwLock, RwLockReadGuard}; use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};
#[derive(Default)]
struct ComponentNull;
impl_handle!(COMPONENT_NULL for ComponentNull);
impl ComponentTrait for ComponentNull {
fn new() -> Self {
ComponentNull::default()
}
}
#[derive(Clone)] #[derive(Clone)]
pub struct ArcAnyComponent(Arc<RwLock<dyn ComponentTrait>>); pub struct ArcAnyComponent(Arc<RwLock<dyn ComponentTrait>>);
impl Default for ArcAnyComponent {
fn default() -> Self {
ArcAnyComponent(Arc::new(RwLock::new(ComponentNull)))
}
}
impl ArcAnyComponent { impl ArcAnyComponent {
pub fn new(component: impl ComponentTrait) -> Self { pub fn new(component: impl ComponentTrait) -> Self {
ArcAnyComponent(Arc::new(RwLock::new(component))) ArcAnyComponent(Arc::new(RwLock::new(component)))
@ -41,16 +24,8 @@ impl ArcAnyComponent {
self.0.read().unwrap() self.0.read().unwrap()
} }
pub(crate) fn handle(&self) -> Handle { pub fn get_mut(&self) -> RwLockWriteGuard<'_, dyn ComponentTrait> {
self.0.read().unwrap().handle() self.0.write().unwrap()
}
pub(crate) fn id(&self) -> Option<String> {
self.0.read().unwrap().id()
}
pub(crate) fn weight(&self) -> Weight {
self.0.read().unwrap().weight()
} }
// ArcAnyComponent RENDER. // ArcAnyComponent RENDER.
@ -58,6 +33,20 @@ impl ArcAnyComponent {
pub fn render(&self, cx: &mut Context) -> Markup { pub fn render(&self, cx: &mut Context) -> Markup {
self.0.write().unwrap().render(cx) self.0.write().unwrap().render(cx)
} }
// ArcAnyComponent HELPERS.
fn handle(&self) -> Handle {
self.0.read().unwrap().handle()
}
fn id(&self) -> String {
self.0.read().unwrap().id().unwrap_or_default()
}
fn weight(&self) -> Weight {
self.0.read().unwrap().weight()
}
} }
// ************************************************************************************************* // *************************************************************************************************
@ -94,27 +83,23 @@ impl AnyComponents {
pub fn alter_value(&mut self, op: ArcAnyOp) -> &mut Self { pub fn alter_value(&mut self, op: ArcAnyOp) -> &mut Self {
match op { match op {
ArcAnyOp::Add(arc) => self.0.push(arc), ArcAnyOp::Add(arc) => self.0.push(arc),
ArcAnyOp::AddAfterId(id, arc) => { ArcAnyOp::AddAfterId(id, arc) => match self.0.iter().position(|c| c.id() == id) {
match self.0.iter().position(|c| c.id().as_deref() == Some(id)) { Some(index) => self.0.insert(index + 1, arc),
Some(index) => self.0.insert(index + 1, arc), _ => self.0.push(arc),
_ => self.0.push(arc), },
} ArcAnyOp::AddBeforeId(id, arc) => match self.0.iter().position(|c| c.id() == id) {
} Some(index) => self.0.insert(index, arc),
ArcAnyOp::AddBeforeId(id, arc) => { _ => self.0.insert(0, arc),
match self.0.iter().position(|c| c.id().as_deref() == Some(id)) { },
Some(index) => self.0.insert(index, arc),
_ => self.0.insert(0, arc),
}
}
ArcAnyOp::AddFirst(arc) => self.0.insert(0, arc), ArcAnyOp::AddFirst(arc) => self.0.insert(0, arc),
ArcAnyOp::RemoveById(id) => { ArcAnyOp::RemoveById(id) => {
if let Some(index) = self.0.iter().position(|c| c.id().as_deref() == Some(id)) { if let Some(index) = self.0.iter().position(|c| c.id() == id) {
self.0.remove(index); self.0.remove(index);
} }
} }
ArcAnyOp::ReplaceById(id, arc) => { ArcAnyOp::ReplaceById(id, arc) => {
for c in self.0.iter_mut() { for c in self.0.iter_mut() {
if c.id().as_deref() == Some(id) { if c.id() == id {
*c = arc; *c = arc;
break; break;
} }
@ -131,12 +116,14 @@ impl AnyComponents {
self.0.is_empty() self.0.is_empty()
} }
pub fn get_by_id(&self, id: &'static str) -> Option<&ArcAnyComponent> { pub fn get_by_id(&self, id: impl Into<String>) -> Option<&ArcAnyComponent> {
self.0.iter().find(|&c| c.id().as_deref() == Some(id)) let id = id.into();
self.0.iter().find(|c| c.id() == id)
} }
pub fn iter_by_id(&self, id: &'static str) -> impl Iterator<Item = &ArcAnyComponent> { pub fn iter_by_id(&self, id: impl Into<String>) -> impl Iterator<Item = &ArcAnyComponent> {
self.0.iter().filter(|&c| c.id().as_deref() == Some(id)) let id = id.into();
self.0.iter().filter(move |&c| c.id() == id)
} }
pub fn iter_by_handle(&self, handle: Handle) -> impl Iterator<Item = &ArcAnyComponent> { pub fn iter_by_handle(&self, handle: Handle) -> impl Iterator<Item = &ArcAnyComponent> {

View file

@ -2,44 +2,35 @@ use crate::core::component::{ComponentTrait, Context};
use crate::html::{html, Markup}; use crate::html::{html, Markup};
use crate::{fn_builder, Handle, Weight}; use crate::{fn_builder, Handle, Weight};
use std::sync::{Arc, RwLock, RwLockReadGuard}; use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};
#[derive(Default)] pub struct ArcTypedComponent<C: ComponentTrait>(Arc<RwLock<C>>);
pub struct ArcTypedComponent<T: ComponentTrait>(Arc<RwLock<T>>);
impl<T: ComponentTrait> Clone for ArcTypedComponent<T> { impl<C: ComponentTrait> Clone for ArcTypedComponent<C> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self(self.0.clone()) Self(self.0.clone())
} }
} }
impl<T: ComponentTrait> ArcTypedComponent<T> { impl<C: ComponentTrait> ArcTypedComponent<C> {
pub fn new(component: T) -> Self { pub fn new(component: C) -> Self {
ArcTypedComponent(Arc::new(RwLock::new(component))) ArcTypedComponent(Arc::new(RwLock::new(component)))
} }
// ArcTypedComponent BUILDER. // ArcTypedComponent BUILDER.
pub fn set(&mut self, component: T) { pub fn set(&mut self, component: C) {
self.0 = Arc::new(RwLock::new(component)); self.0 = Arc::new(RwLock::new(component));
} }
// ArcTypedComponent GETTERS. // ArcTypedComponent GETTERS.
pub fn get(&self) -> RwLockReadGuard<'_, T> { pub fn get(&self) -> RwLockReadGuard<'_, C> {
self.0.read().unwrap() self.0.read().unwrap()
} }
pub(crate) fn handle(&self) -> Handle { pub fn get_mut(&self) -> RwLockWriteGuard<'_, C> {
self.0.read().unwrap().handle() self.0.write().unwrap()
}
pub(crate) fn id(&self) -> Option<String> {
self.0.read().unwrap().id()
}
pub(crate) fn weight(&self) -> Weight {
self.0.read().unwrap().weight()
} }
// ArcTypedComponent RENDER. // ArcTypedComponent RENDER.
@ -47,55 +38,65 @@ impl<T: ComponentTrait> ArcTypedComponent<T> {
pub fn render(&self, cx: &mut Context) -> Markup { pub fn render(&self, cx: &mut Context) -> Markup {
self.0.write().unwrap().render(cx) self.0.write().unwrap().render(cx)
} }
// ArcTypedComponent HELPERS.
fn handle(&self) -> Handle {
self.0.read().unwrap().handle()
}
fn id(&self) -> String {
self.0.read().unwrap().id().unwrap_or_default()
}
fn weight(&self) -> Weight {
self.0.read().unwrap().weight()
}
} }
// ************************************************************************************************* // *************************************************************************************************
pub enum ArcTypedOp<T: ComponentTrait + Default> { pub enum ArcTypedOp<C: ComponentTrait> {
Add(ArcTypedComponent<T>), Add(ArcTypedComponent<C>),
AddAfterId(&'static str, ArcTypedComponent<T>), AddAfterId(&'static str, ArcTypedComponent<C>),
AddBeforeId(&'static str, ArcTypedComponent<T>), AddBeforeId(&'static str, ArcTypedComponent<C>),
AddFirst(ArcTypedComponent<T>), AddFirst(ArcTypedComponent<C>),
RemoveById(&'static str), RemoveById(&'static str),
ReplaceById(&'static str, ArcTypedComponent<T>), ReplaceById(&'static str, ArcTypedComponent<C>),
Reset, Reset,
} }
#[derive(Clone, Default)] #[derive(Clone, Default)]
pub struct TypedComponents<T: ComponentTrait + Default>(Vec<ArcTypedComponent<T>>); pub struct TypedComponents<C: ComponentTrait>(Vec<ArcTypedComponent<C>>);
impl<T: ComponentTrait + Default> TypedComponents<T> { impl<C: ComponentTrait + Default> TypedComponents<C> {
pub fn new(arc: ArcTypedComponent<T>) -> Self { pub fn new(arc: ArcTypedComponent<C>) -> Self {
TypedComponents::default().with_value(ArcTypedOp::Add(arc)) TypedComponents::default().with_value(ArcTypedOp::Add(arc))
} }
// TypedComponents BUILDER. // TypedComponents BUILDER.
#[fn_builder] #[fn_builder]
pub fn alter_value(&mut self, op: ArcTypedOp<T>) -> &mut Self { pub fn alter_value(&mut self, op: ArcTypedOp<C>) -> &mut Self {
match op { match op {
ArcTypedOp::Add(one) => self.0.push(one), ArcTypedOp::Add(one) => self.0.push(one),
ArcTypedOp::AddAfterId(id, one) => { ArcTypedOp::AddAfterId(id, one) => match self.0.iter().position(|c| c.id() == id) {
match self.0.iter().position(|c| c.id().as_deref() == Some(id)) { Some(index) => self.0.insert(index + 1, one),
Some(index) => self.0.insert(index + 1, one), _ => self.0.push(one),
_ => self.0.push(one), },
} ArcTypedOp::AddBeforeId(id, one) => match self.0.iter().position(|c| c.id() == id) {
} Some(index) => self.0.insert(index, one),
ArcTypedOp::AddBeforeId(id, one) => { _ => self.0.insert(0, one),
match self.0.iter().position(|c| c.id().as_deref() == Some(id)) { },
Some(index) => self.0.insert(index, one),
_ => self.0.insert(0, one),
}
}
ArcTypedOp::AddFirst(one) => self.0.insert(0, one), ArcTypedOp::AddFirst(one) => self.0.insert(0, one),
ArcTypedOp::RemoveById(id) => { ArcTypedOp::RemoveById(id) => {
if let Some(index) = self.0.iter().position(|c| c.id().as_deref() == Some(id)) { if let Some(index) = self.0.iter().position(|c| c.id() == id) {
self.0.remove(index); self.0.remove(index);
} }
} }
ArcTypedOp::ReplaceById(id, one) => { ArcTypedOp::ReplaceById(id, one) => {
for c in self.0.iter_mut() { for c in self.0.iter_mut() {
if c.id().as_deref() == Some(id) { if c.id() == id {
*c = one; *c = one;
break; break;
} }
@ -112,15 +113,17 @@ impl<T: ComponentTrait + Default> TypedComponents<T> {
self.0.is_empty() self.0.is_empty()
} }
pub fn get_by_id(&self, id: &'static str) -> Option<&ArcTypedComponent<T>> { pub fn get_by_id(&self, id: impl Into<String>) -> Option<&ArcTypedComponent<C>> {
self.0.iter().find(|&c| c.id().as_deref() == Some(id)) let id = id.into();
self.0.iter().find(|&c| c.id() == id)
} }
pub fn iter_by_id(&self, id: &'static str) -> impl Iterator<Item = &ArcTypedComponent<T>> { pub fn iter_by_id(&self, id: impl Into<String>) -> impl Iterator<Item = &ArcTypedComponent<C>> {
self.0.iter().filter(|&c| c.id().as_deref() == Some(id)) let id = id.into();
self.0.iter().filter(move |&c| c.id() == id)
} }
pub fn iter_by_handle(&self, handle: Handle) -> impl Iterator<Item = &ArcTypedComponent<T>> { pub fn iter_by_handle(&self, handle: Handle) -> impl Iterator<Item = &ArcTypedComponent<C>> {
self.0.iter().filter(move |&c| c.handle() == handle) self.0.iter().filter(move |&c| c.handle() == handle)
} }