From 83e09c38cea5f7a7680d1bd51fd55db39e31a611 Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Sun, 5 Jan 2025 09:22:40 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Nuevo=20m=C3=A9todo=20writable()=20?= =?UTF-8?q?para=20componentes=20hijo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pagetop/src/core/component/children.rs | 36 ++++++++++++++++++++------ 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/pagetop/src/core/component/children.rs b/pagetop/src/core/component/children.rs index d9ad8c4a..531f20c6 100644 --- a/pagetop/src/core/component/children.rs +++ b/pagetop/src/core/component/children.rs @@ -2,7 +2,7 @@ use crate::core::component::{ComponentTrait, Context}; use crate::html::{html, Markup}; use crate::{fn_builder, UniqueId}; -use std::sync::{Arc, RwLock}; +use std::sync::{Arc, RwLock, RwLockWriteGuard}; #[derive(Clone)] pub struct ChildComponent(Arc>); @@ -12,6 +12,16 @@ impl ChildComponent { ChildComponent(Arc::new(RwLock::new(component))) } + // ChildComponent GETTERS. + + pub fn id(&self) -> Option { + self.0.read().unwrap().id() + } + + pub fn writable(&self) -> RwLockWriteGuard<'_, dyn ComponentTrait> { + self.0.write().unwrap() + } + // ChildComponent RENDER. pub fn render(&self, cx: &mut Context) -> Markup { @@ -24,7 +34,7 @@ impl ChildComponent { self.0.read().unwrap().type_id() } - fn id(&self) -> String { + fn child_id(&self) -> String { self.0.read().unwrap().id().unwrap_or_default() } } @@ -44,6 +54,16 @@ impl TypedComponent { TypedComponent(Arc::new(RwLock::new(component))) } + // TypedComponent GETTERS. + + pub fn id(&self) -> Option { + self.0.read().unwrap().id() + } + + pub fn writable(&self) -> RwLockWriteGuard<'_, C> { + self.0.write().unwrap() + } + // TypedComponent RENDER. pub fn render(&self, cx: &mut Context) -> Markup { @@ -135,7 +155,7 @@ impl Children { #[inline] fn insert_after_id(&mut self, id: &str, child: ChildComponent) -> &mut Self { - match self.0.iter().position(|c| c.id() == id) { + match self.0.iter().position(|c| c.child_id() == id) { Some(index) => self.0.insert(index + 1, child), _ => self.0.push(child), }; @@ -144,7 +164,7 @@ impl Children { #[inline] fn insert_before_id(&mut self, id: &str, child: ChildComponent) -> &mut Self { - match self.0.iter().position(|c| c.id() == id) { + match self.0.iter().position(|c| c.child_id() == id) { Some(index) => self.0.insert(index, child), _ => self.0.insert(0, child), }; @@ -159,7 +179,7 @@ impl Children { #[inline] fn remove_by_id(&mut self, id: &str) -> &mut Self { - if let Some(index) = self.0.iter().position(|c| c.id() == id) { + if let Some(index) = self.0.iter().position(|c| c.child_id() == id) { self.0.remove(index); } self @@ -168,7 +188,7 @@ impl Children { #[inline] fn replace_by_id(&mut self, id: &str, child: ChildComponent) -> &mut Self { for c in &mut self.0 { - if c.id() == id { + if c.child_id() == id { *c = child; break; } @@ -194,12 +214,12 @@ impl Children { pub fn get_by_id(&self, id: impl Into) -> Option<&ChildComponent> { let id = id.into(); - self.0.iter().find(|c| c.id() == id) + self.0.iter().find(|c| c.child_id() == id) } pub fn iter_by_id(&self, id: impl Into) -> impl Iterator { let id = id.into(); - self.0.iter().filter(move |&c| c.id() == id) + self.0.iter().filter(move |&c| c.child_id() == id) } pub fn iter_by_type_id(&self, type_id: UniqueId) -> impl Iterator {