diff --git a/pagetop/src/core/component/bundle.rs b/pagetop/src/core/component/bundle.rs index 77f8cadf..06cba0a2 100644 --- a/pagetop/src/core/component/bundle.rs +++ b/pagetop/src/core/component/bundle.rs @@ -1,6 +1,6 @@ use crate::core::component::{ComponentTrait, RenderContext}; -use crate::fn_builder; use crate::html::{html, Markup}; +use crate::{fn_builder, Handle}; use std::sync::{Arc, RwLock}; @@ -14,8 +14,10 @@ pub enum BundleOp { Reset, } +pub type ArcLockComponent = Arc>; + #[derive(Clone, Default)] -pub struct ComponentsBundle(Vec>>); +pub struct ComponentsBundle(Vec); impl ComponentsBundle { pub fn new() -> Self { @@ -92,6 +94,26 @@ impl ComponentsBundle { self } + // ComponentsBundle GETTERS. + + pub fn get_by_id(&self, id: &'static str) -> Option<&ArcLockComponent> { + self.0 + .iter() + .find(|&c| c.read().unwrap().id().as_deref() == Some(id)) + } + + pub fn iter_by_id(&self, id: &'static str) -> impl Iterator { + self.0 + .iter() + .filter(|&c| c.read().unwrap().id().as_deref() == Some(id)) + } + + pub fn iter_by_handle(&self, handle: Handle) -> impl Iterator { + self.0 + .iter() + .filter(move |&c| c.read().unwrap().handle() == handle) + } + // ComponentsBundle RENDER. pub fn render(&self, rcx: &mut RenderContext) -> Markup {