From ece78b2b4adbbb33eba4d6b426e73a3643255e6a Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Mon, 15 Apr 2024 21:42:53 +0200 Subject: [PATCH] =?UTF-8?q?=E2=8F=AA=20Revert=20using=20weight=20to=20orde?= =?UTF-8?q?r=20components?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/component/mixed.rs | 90 ++++++------------------------------- 1 file changed, 13 insertions(+), 77 deletions(-) diff --git a/src/core/component/mixed.rs b/src/core/component/mixed.rs index 41f5a6d8..b367417f 100644 --- a/src/core/component/mixed.rs +++ b/src/core/component/mixed.rs @@ -1,121 +1,59 @@ use crate::core::component::{ComponentTrait, Context}; use crate::html::{html, Markup}; -use crate::{fn_builder, TypeId, Weight}; +use crate::{fn_builder, TypeId}; -use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard}; +use std::sync::{Arc, RwLock}; #[derive(Clone)] -pub struct AnyComponent { - weight: Weight, - component: Arc>, -} +pub struct AnyComponent(Arc>); impl AnyComponent { pub fn with(component: impl ComponentTrait) -> Self { - AnyComponent { - weight: Weight::default(), - component: Arc::new(RwLock::new(component)), - } - } - - // AnyComponent BUILDER. - - #[fn_builder] - pub fn alter_weight(&mut self, value: Weight) -> &mut Self { - self.weight = value; - self - } - - // AnyComponent GETTERS. - - pub fn weight(&self) -> Weight { - self.weight - } - - pub fn get(&self) -> RwLockReadGuard<'_, dyn ComponentTrait> { - self.component.read().unwrap() - } - - pub fn get_mut(&self) -> RwLockWriteGuard<'_, dyn ComponentTrait> { - self.component.write().unwrap() + AnyComponent(Arc::new(RwLock::new(component))) } // AnyComponent RENDER. pub fn render(&self, cx: &mut Context) -> Markup { - self.component.write().unwrap().render(cx) + self.0.write().unwrap().render(cx) } // AnyComponent HELPERS. fn type_id(&self) -> TypeId { - self.component.read().unwrap().type_id() + self.0.read().unwrap().type_id() } fn id(&self) -> String { - self.component.read().unwrap().id().unwrap_or_default() + self.0.read().unwrap().id().unwrap_or_default() } } // ************************************************************************************************* -pub struct TypedComponent { - weight: Weight, - component: Arc>, -} +pub struct TypedComponent(Arc>); impl Clone for TypedComponent { fn clone(&self) -> Self { - Self { - weight: self.weight.clone(), - component: self.component.clone(), - } + Self(self.0.clone()) } } impl TypedComponent { pub fn with(component: C) -> Self { - TypedComponent { - weight: Weight::default(), - component: Arc::new(RwLock::new(component)), - } - } - - // TypedComponent BUILDER. - - #[fn_builder] - pub fn alter_weight(&mut self, value: Weight) -> &mut Self { - self.weight = value; - self - } - - // TypedComponent GETTERS. - - pub fn weight(&self) -> Weight { - self.weight - } - - pub fn get(&self) -> RwLockReadGuard<'_, C> { - self.component.read().unwrap() - } - - pub fn get_mut(&self) -> RwLockWriteGuard<'_, C> { - self.component.write().unwrap() + TypedComponent(Arc::new(RwLock::new(component))) } // TypedComponent RENDER. pub fn render(&self, cx: &mut Context) -> Markup { - self.component.write().unwrap().render(cx) + self.0.write().unwrap().render(cx) } // TypedComponent HELPERS. fn to_any(&self) -> AnyComponent { - AnyComponent { - weight: self.weight.clone(), - component: self.component.clone(), - } + AnyComponent(self.0.clone()) } } @@ -266,10 +204,8 @@ impl MixedComponents { // MixedComponents RENDER. pub fn render(&self, cx: &mut Context) -> Markup { - let mut components = self.0.clone(); - components.sort_by_key(|c| c.weight()); html! { - @for c in components.iter() { + @for c in self.0.iter() { (c.render(cx)) } }