🚧 Revamp ComponentRef to ComponentArc

This commit is contained in:
Manuel Cillero 2023-08-13 10:42:39 +02:00
parent df0b2eeb71
commit 68b54fbce5
10 changed files with 105 additions and 79 deletions

View file

@ -12,7 +12,7 @@ pub struct Block {
id : IdentifierValue,
classes : Classes,
title : AttributeValue,
content : PackComponents,
stuff : PackComponents,
template : String,
}
@ -94,12 +94,17 @@ impl Block {
}
pub fn with_component(mut self, component: impl ComponentTrait) -> Self {
self.content.alter(PackOp::Add, ComponentRef::to(component));
self.stuff.alter(PackOp::Add, ComponentArc::new(component));
self
}
pub fn with_component_arc(mut self, arc: ComponentArc) -> Self {
self.stuff.alter(PackOp::Add, arc);
self
}
pub fn alter_components(&mut self, op: PackOp, component: impl ComponentTrait) -> &mut Self {
self.content.alter(op, ComponentRef::to(component));
self.stuff.alter(op, ComponentArc::new(component));
self
}
@ -120,7 +125,7 @@ impl Block {
}
pub fn components(&self) -> &PackComponents {
&self.content
&self.stuff
}
pub fn template(&self) -> &str {

View file

@ -121,12 +121,17 @@ impl Form {
}
pub fn with_element(mut self, element: impl ComponentTrait) -> Self {
self.elements.alter(PackOp::Add, ComponentRef::to(element));
self.elements.alter(PackOp::Add, ComponentArc::new(element));
self
}
pub fn with_element_arc(mut self, arc: ComponentArc) -> Self {
self.elements.alter(PackOp::Add, arc);
self
}
pub fn alter_elements(&mut self, op: PackOp, element: impl ComponentTrait) -> &mut Self {
self.elements.alter(op, ComponentRef::to(element));
self.elements.alter(op, ComponentArc::new(element));
self
}

View file

@ -44,7 +44,7 @@ pub struct Column {
id : IdentifierValue,
classes : Classes,
size : ColumnSize,
content : PackComponents,
stuff : PackComponents,
template : String,
}
@ -136,12 +136,17 @@ impl Column {
}
pub fn with_component(mut self, component: impl ComponentTrait) -> Self {
self.content.alter(PackOp::Add, ComponentRef::to(component));
self.stuff.alter(PackOp::Add, ComponentArc::new(component));
self
}
pub fn with_component_arc(mut self, arc: ComponentArc) -> Self {
self.stuff.alter(PackOp::Add, arc);
self
}
pub fn alter_components(&mut self, op: PackOp, component: impl ComponentTrait) -> &mut Self {
self.content.alter(op, ComponentRef::to(component));
self.stuff.alter(op, ComponentArc::new(component));
self
}
@ -162,7 +167,7 @@ impl Column {
}
pub fn components(&self) -> &PackComponents {
&self.content
&self.stuff
}
pub fn template(&self) -> &str {

View file

@ -83,12 +83,12 @@ impl Row {
}
pub fn with_column(mut self, column: grid::Column) -> Self {
self.columns.alter(PackOp::Add, ComponentRef::to(column));
self.columns.alter(PackOp::Add, ComponentArc::new(column));
self
}
pub fn alter_columns(&mut self, op: PackOp, column: grid::Column) -> &mut Self {
self.columns.alter(op, ComponentRef::to(column));
self.columns.alter(op, ComponentArc::new(column));
self
}

View file

@ -20,7 +20,7 @@ pub struct Paragraph {
renderable: Renderable,
id : IdentifierValue,
classes : Classes,
content : PackComponents,
stuff : PackComponents,
display : ParagraphDisplay,
template : String,
}
@ -90,12 +90,17 @@ impl Paragraph {
}
pub fn with_component(mut self, component: impl ComponentTrait) -> Self {
self.content.alter(PackOp::Add, ComponentRef::to(component));
self.stuff.alter(PackOp::Add, ComponentArc::new(component));
self
}
pub fn with_component_arc(mut self, arc: ComponentArc) -> Self {
self.stuff.alter(PackOp::Add, arc);
self
}
pub fn alter_components(&mut self, op: PackOp, component: impl ComponentTrait) -> &mut Self {
self.content.alter(op, ComponentRef::to(component));
self.stuff.alter(op, ComponentArc::new(component));
self
}
@ -130,7 +135,7 @@ impl Paragraph {
}
pub fn components(&self) -> &PackComponents {
&self.content
&self.stuff
}
pub fn display(&self) -> &ParagraphDisplay {

View file

@ -1,18 +1,21 @@
mod context;
pub use context::{Context, ContextOp};
pub type ContextualPath = fn(cx: &Context) -> &str;
pub type FnContextualPath = fn(cx: &Context) -> &str;
mod definition;
pub use definition::{component_mut, component_ref, ComponentBase, ComponentTrait};
mod arc;
pub use arc::ComponentArc;
mod one;
pub use one::OneComponent;
mod pack;
pub use pack::{ComponentRef, PackComponents, PackOp};
pub use pack::{PackComponents, PackOp};
mod renderable;
pub use renderable::{IsRenderable, Renderable};
pub use renderable::{FnIsRenderable, Renderable};
pub mod html;
pub mod l10n;
@ -23,7 +26,7 @@ macro_rules! actions_for_component {
$crate::paste! {
use $crate::prelude::*;
pub type [<Action $Component>] = fn(component: &$Component, cx: &mut Context);
pub type [<FnAction $Component>] = fn(component: &$Component, cx: &mut Context);
// *************************************************************************************
// ACTION BEFORE PREPARE COMPONENT
@ -32,7 +35,7 @@ macro_rules! actions_for_component {
$crate::new_handle!([<ACTION_BEFORE_PREPARE_ $Component:upper>] for Action);
pub struct [<BeforePrepare $Component>] {
action: Option<[<Action $Component>]>,
action: Option<[<FnAction $Component>]>,
weight: Weight,
}
@ -55,7 +58,7 @@ macro_rules! actions_for_component {
impl [<BeforePrepare $Component>] {
#[allow(dead_code)]
pub fn with_action(mut self, action: [<Action $Component>]) -> Self {
pub fn with_action(mut self, action: [<FnAction $Component>]) -> Self {
self.action = Some(action);
self
}
@ -91,7 +94,7 @@ macro_rules! actions_for_component {
$crate::new_handle!([<ACTION_AFTER_PREPARE_ $Component:upper>] for Action);
pub struct [<AfterPrepare $Component>] {
action: Option<[<Action $Component>]>,
action: Option<[<FnAction $Component>]>,
weight: Weight,
}
@ -114,7 +117,7 @@ macro_rules! actions_for_component {
impl [<AfterPrepare $Component>] {
#[allow(dead_code)]
pub fn with_action(mut self, action: [<Action $Component>]) -> Self {
pub fn with_action(mut self, action: [<FnAction $Component>]) -> Self {
self.action = Some(action);
self
}

View file

@ -0,0 +1,30 @@
use crate::core::component::{ComponentTrait, Context};
use crate::html::Markup;
use crate::{Handle, Weight};
use std::sync::{Arc, RwLock};
#[derive(Clone)]
pub struct ComponentArc(Arc<RwLock<dyn ComponentTrait>>);
impl ComponentArc {
pub fn new(component: impl ComponentTrait) -> Self {
ComponentArc(Arc::new(RwLock::new(component)))
}
pub(crate) fn handle(&self) -> Handle {
self.0.read().unwrap().handle()
}
pub(crate) fn id(&self) -> Option<String> {
self.0.read().unwrap().id()
}
pub(crate) fn weight(&self) -> Weight {
self.0.read().unwrap().weight()
}
pub(crate) fn prepare(&self, cx: &mut Context) -> Markup {
self.0.write().unwrap().prepare(cx)
}
}

View file

@ -1,33 +1,6 @@
use crate::core::component::{ComponentTrait, Context};
use crate::core::component::{ComponentArc, Context};
use crate::html::{html, Markup};
use crate::{Handle, Weight};
use std::sync::{Arc, RwLock};
#[derive(Clone)]
pub struct ComponentRef(Arc<RwLock<dyn ComponentTrait>>);
impl ComponentRef {
pub fn to(component: impl ComponentTrait) -> Self {
ComponentRef(Arc::new(RwLock::new(component)))
}
fn handle(&self) -> Handle {
self.0.read().unwrap().handle()
}
fn id(&self) -> Option<String> {
self.0.read().unwrap().id()
}
fn weight(&self) -> Weight {
self.0.read().unwrap().weight()
}
fn prepare(&self, cx: &mut Context) -> Markup {
self.0.write().unwrap().prepare(cx)
}
}
use crate::Handle;
pub enum PackOp {
Add,
@ -40,16 +13,16 @@ pub enum PackOp {
}
#[derive(Clone, Default)]
pub struct PackComponents(Vec<ComponentRef>);
pub struct PackComponents(Vec<ComponentArc>);
impl PackComponents {
pub fn new() -> Self {
PackComponents::default()
}
pub fn with(cref: ComponentRef) -> Self {
pub fn with(arc: ComponentArc) -> Self {
let mut pack = PackComponents::new();
pack.alter(PackOp::Add, cref);
pack.alter(PackOp::Add, arc);
pack
}
@ -63,22 +36,22 @@ impl PackComponents {
// PackComponents BUILDER.
pub fn alter(&mut self, op: PackOp, cref: ComponentRef) -> &mut Self {
pub fn alter(&mut self, op: PackOp, arc: ComponentArc) -> &mut Self {
match op {
PackOp::Add => self.0.push(cref),
PackOp::Add => self.0.push(arc),
PackOp::AddAfterId(id) => {
match self.0.iter().position(|c| c.id().as_deref() == Some(id)) {
Some(index) => self.0.insert(index + 1, cref),
_ => self.0.push(cref),
Some(index) => self.0.insert(index + 1, arc),
_ => self.0.push(arc),
}
}
PackOp::AddBeforeId(id) => {
match self.0.iter().position(|c| c.id().as_deref() == Some(id)) {
Some(index) => self.0.insert(index, cref),
_ => self.0.insert(0, cref),
Some(index) => self.0.insert(index, arc),
_ => self.0.insert(0, arc),
}
}
PackOp::AddFirst => self.0.insert(0, cref),
PackOp::AddFirst => self.0.insert(0, arc),
PackOp::RemoveById(id) => {
if let Some(index) = self.0.iter().position(|c| c.id().as_deref() == Some(id)) {
self.0.remove(index);
@ -87,7 +60,7 @@ impl PackComponents {
PackOp::ReplaceById(id) => {
for c in self.0.iter_mut() {
if c.id().as_deref() == Some(id) {
*c = cref;
*c = arc;
break;
}
}
@ -99,15 +72,15 @@ impl PackComponents {
// PackComponents GETTERS.
pub fn get_by_id(&self, id: &'static str) -> Option<&ComponentRef> {
pub fn get_by_id(&self, id: &'static str) -> Option<&ComponentArc> {
self.0.iter().find(|&c| c.id().as_deref() == Some(id))
}
pub fn iter_by_id(&self, id: &'static str) -> impl Iterator<Item = &ComponentRef> {
pub fn iter_by_id(&self, id: &'static str) -> impl Iterator<Item = &ComponentArc> {
self.0.iter().filter(|&c| c.id().as_deref() == Some(id))
}
pub fn iter_by_handle(&self, handle: Handle) -> impl Iterator<Item = &ComponentRef> {
pub fn iter_by_handle(&self, handle: Handle) -> impl Iterator<Item = &ComponentArc> {
self.0.iter().filter(move |&c| c.handle() == handle)
}

View file

@ -1,4 +1,4 @@
use crate::core::component::{ComponentRef, PackComponents, PackOp};
use crate::core::component::{ComponentArc, PackComponents, PackOp};
use crate::core::theme::ThemeRef;
use crate::{Handle, LazyStatic};
@ -19,17 +19,17 @@ impl ComponentsRegions {
ComponentsRegions::default()
}
pub fn with(region: &'static str, cref: ComponentRef) -> Self {
pub fn with(region: &'static str, arc: ComponentArc) -> Self {
let mut regions = ComponentsRegions::new();
regions.add_in(region, cref);
regions.add_in(region, arc);
regions
}
pub fn add_in(&mut self, region: &'static str, cref: ComponentRef) {
pub fn add_in(&mut self, region: &'static str, arc: ComponentArc) {
if let Some(region) = self.0.get_mut(region) {
region.alter(PackOp::Add, cref);
region.alter(PackOp::Add, arc);
} else {
self.0.insert(region, PackComponents::with(cref));
self.0.insert(region, PackComponents::with(arc));
}
}
@ -48,17 +48,17 @@ pub enum Region {
OfTheme(ThemeRef, &'static str),
}
pub fn add_component_in(region: Region, cref: ComponentRef) {
pub fn add_component_in(region: Region, arc: ComponentArc) {
match region {
Region::Named(name) => {
COMMON_REGIONS.write().unwrap().add_in(name, cref);
COMMON_REGIONS.write().unwrap().add_in(name, arc);
}
Region::OfTheme(theme, region) => {
let mut regions = THEME_REGIONS.write().unwrap();
if let Some(hm) = regions.get_mut(&theme.handle()) {
hm.add_in(region, cref);
hm.add_in(region, arc);
} else {
regions.insert(theme.handle(), ComponentsRegions::with(region, cref));
regions.insert(theme.handle(), ComponentsRegions::with(region, arc));
}
}
}

View file

@ -2,7 +2,7 @@ mod action;
pub use action::*;
use crate::core::component::l10n::L10n;
use crate::core::component::{ComponentRef, ComponentTrait, Context, ContextOp, OneComponent};
use crate::core::component::{ComponentArc, ComponentTrait, Context, ContextOp, OneComponent};
use crate::core::theme::ComponentsRegions;
use crate::html::{html, Classes, ClassesOp, Favicon, Markup, DOCTYPE};
use crate::response::fatal_error::FatalError;
@ -90,7 +90,7 @@ impl Page {
#[fn_builder]
pub fn alter_in(&mut self, region: &'static str, component: impl ComponentTrait) -> &mut Self {
self.regions.add_in(region, ComponentRef::to(component));
self.regions.add_in(region, ComponentArc::new(component));
self
}