🧑💻 Simplify component vector handling
This commit is contained in:
parent
400a492311
commit
d7acf0c3d1
17 changed files with 154 additions and 221 deletions
|
|
@ -11,8 +11,7 @@ pub use definition::{component_as_mut, component_as_ref, ComponentBase, Componen
|
|||
mod classes;
|
||||
pub use classes::{ComponentClasses, ComponentClassesOp};
|
||||
|
||||
mod arc_mixed;
|
||||
pub use arc_mixed::{AnyComponent, MixedComponents, MixedOp};
|
||||
|
||||
mod arc_typed;
|
||||
pub use arc_typed::{OneComponent, TypedComponents, TypedOp};
|
||||
mod mixed;
|
||||
pub use mixed::MixedComponents;
|
||||
pub use mixed::{AnyComponent, AnyOp};
|
||||
pub use mixed::{TypedComponent, TypedOp};
|
||||
|
|
|
|||
|
|
@ -1,141 +0,0 @@
|
|||
use crate::core::component::{ComponentTrait, Context};
|
||||
use crate::html::{html, Markup};
|
||||
use crate::{fn_builder, TypeId, Weight};
|
||||
|
||||
use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};
|
||||
|
||||
pub struct OneComponent<C: ComponentTrait>(Arc<RwLock<C>>);
|
||||
|
||||
impl<C: ComponentTrait> Clone for OneComponent<C> {
|
||||
fn clone(&self) -> Self {
|
||||
Self(self.0.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl<C: ComponentTrait> OneComponent<C> {
|
||||
pub fn with(component: C) -> Self {
|
||||
OneComponent(Arc::new(RwLock::new(component)))
|
||||
}
|
||||
|
||||
// OneComponent BUILDER.
|
||||
|
||||
pub fn set(&mut self, component: C) {
|
||||
self.0 = Arc::new(RwLock::new(component));
|
||||
}
|
||||
|
||||
// OneComponent GETTERS.
|
||||
|
||||
pub fn get(&self) -> RwLockReadGuard<'_, C> {
|
||||
self.0.read().unwrap()
|
||||
}
|
||||
|
||||
pub fn get_mut(&self) -> RwLockWriteGuard<'_, C> {
|
||||
self.0.write().unwrap()
|
||||
}
|
||||
|
||||
// OneComponent RENDER.
|
||||
|
||||
pub fn render(&self, cx: &mut Context) -> Markup {
|
||||
self.0.write().unwrap().render(cx)
|
||||
}
|
||||
|
||||
// OneComponent HELPERS.
|
||||
|
||||
fn type_id(&self) -> TypeId {
|
||||
self.0.read().unwrap().type_id()
|
||||
}
|
||||
|
||||
fn id(&self) -> String {
|
||||
self.0.read().unwrap().id().unwrap_or_default()
|
||||
}
|
||||
|
||||
fn weight(&self) -> Weight {
|
||||
self.0.read().unwrap().weight()
|
||||
}
|
||||
}
|
||||
|
||||
// *************************************************************************************************
|
||||
|
||||
pub enum TypedOp<C: ComponentTrait> {
|
||||
Add(OneComponent<C>),
|
||||
InsertAfterId(&'static str, OneComponent<C>),
|
||||
InsertBeforeId(&'static str, OneComponent<C>),
|
||||
Prepend(OneComponent<C>),
|
||||
RemoveById(&'static str),
|
||||
ReplaceById(&'static str, OneComponent<C>),
|
||||
Reset,
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct TypedComponents<C: ComponentTrait>(Vec<OneComponent<C>>);
|
||||
|
||||
impl<C: ComponentTrait + Default> TypedComponents<C> {
|
||||
pub fn new(one: OneComponent<C>) -> Self {
|
||||
TypedComponents::default().with_value(TypedOp::Add(one))
|
||||
}
|
||||
|
||||
// TypedComponents BUILDER.
|
||||
|
||||
#[fn_builder]
|
||||
pub fn alter_value(&mut self, op: TypedOp<C>) -> &mut Self {
|
||||
match op {
|
||||
TypedOp::Add(one) => self.0.push(one),
|
||||
TypedOp::InsertAfterId(id, one) => match self.0.iter().position(|c| c.id() == id) {
|
||||
Some(index) => self.0.insert(index + 1, one),
|
||||
_ => self.0.push(one),
|
||||
},
|
||||
TypedOp::InsertBeforeId(id, one) => match self.0.iter().position(|c| c.id() == id) {
|
||||
Some(index) => self.0.insert(index, one),
|
||||
_ => self.0.insert(0, one),
|
||||
},
|
||||
TypedOp::Prepend(one) => self.0.insert(0, one),
|
||||
TypedOp::RemoveById(id) => {
|
||||
if let Some(index) = self.0.iter().position(|c| c.id() == id) {
|
||||
self.0.remove(index);
|
||||
}
|
||||
}
|
||||
TypedOp::ReplaceById(id, one) => {
|
||||
for c in self.0.iter_mut() {
|
||||
if c.id() == id {
|
||||
*c = one;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
TypedOp::Reset => self.0.clear(),
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
// TypedComponents GETTERS.
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.0.is_empty()
|
||||
}
|
||||
|
||||
pub fn get_by_id(&self, id: impl Into<String>) -> Option<&OneComponent<C>> {
|
||||
let id = id.into();
|
||||
self.0.iter().find(|&c| c.id() == id)
|
||||
}
|
||||
|
||||
pub fn iter_by_id(&self, id: impl Into<String>) -> impl Iterator<Item = &OneComponent<C>> {
|
||||
let id = id.into();
|
||||
self.0.iter().filter(move |&c| c.id() == id)
|
||||
}
|
||||
|
||||
pub fn iter_by_type_id(&self, type_id: TypeId) -> impl Iterator<Item = &OneComponent<C>> {
|
||||
self.0.iter().filter(move |&c| c.type_id() == type_id)
|
||||
}
|
||||
|
||||
// TypedComponents 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() {
|
||||
(c.render(cx))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
use crate::base::component::add_base_assets;
|
||||
use crate::core::component::MixedOp;
|
||||
use crate::core::component::AnyOp;
|
||||
use crate::core::theme::all::{theme_by_single_name, THEME_DEFAULT};
|
||||
use crate::core::theme::{ComponentsInRegions, ThemeRef};
|
||||
use crate::html::{html, Assets, HeadScript, HeadStyles, JavaScript, Markup, StyleSheet};
|
||||
|
|
@ -95,7 +95,7 @@ impl Context {
|
|||
self
|
||||
}
|
||||
|
||||
pub fn alter_regions(&mut self, region: &'static str, op: MixedOp) -> &mut Self {
|
||||
pub fn alter_regions(&mut self, region: &'static str, op: AnyOp) -> &mut Self {
|
||||
self.regions.alter_components(region, op);
|
||||
self
|
||||
}
|
||||
|
|
|
|||
|
|
@ -51,7 +51,43 @@ impl AnyComponent {
|
|||
|
||||
// *************************************************************************************************
|
||||
|
||||
pub enum MixedOp {
|
||||
pub struct TypedComponent<C: ComponentTrait>(Arc<RwLock<C>>);
|
||||
|
||||
impl<C: ComponentTrait> Clone for TypedComponent<C> {
|
||||
fn clone(&self) -> Self {
|
||||
Self(self.0.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl<C: ComponentTrait> TypedComponent<C> {
|
||||
pub fn with(component: C) -> Self {
|
||||
TypedComponent(Arc::new(RwLock::new(component)))
|
||||
}
|
||||
|
||||
// TypedComponent GETTERS.
|
||||
|
||||
pub fn get(&self) -> RwLockReadGuard<'_, C> {
|
||||
self.0.read().unwrap()
|
||||
}
|
||||
|
||||
pub fn get_mut(&self) -> RwLockWriteGuard<'_, C> {
|
||||
self.0.write().unwrap()
|
||||
}
|
||||
|
||||
fn to_any(&self) -> AnyComponent {
|
||||
AnyComponent(self.0.clone())
|
||||
}
|
||||
|
||||
// TypedComponent RENDER.
|
||||
|
||||
pub fn render(&self, cx: &mut Context) -> Markup {
|
||||
self.0.write().unwrap().render(cx)
|
||||
}
|
||||
}
|
||||
|
||||
// *************************************************************************************************
|
||||
|
||||
pub enum AnyOp {
|
||||
Add(AnyComponent),
|
||||
InsertAfterId(&'static str, AnyComponent),
|
||||
InsertBeforeId(&'static str, AnyComponent),
|
||||
|
|
@ -61,6 +97,16 @@ pub enum MixedOp {
|
|||
Reset,
|
||||
}
|
||||
|
||||
pub enum TypedOp<C: ComponentTrait> {
|
||||
Add(TypedComponent<C>),
|
||||
InsertAfterId(&'static str, TypedComponent<C>),
|
||||
InsertBeforeId(&'static str, TypedComponent<C>),
|
||||
Prepend(TypedComponent<C>),
|
||||
RemoveById(&'static str),
|
||||
ReplaceById(&'static str, TypedComponent<C>),
|
||||
Reset,
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct MixedComponents(Vec<AnyComponent>);
|
||||
|
||||
|
|
@ -70,7 +116,7 @@ impl MixedComponents {
|
|||
}
|
||||
|
||||
pub fn with(any: AnyComponent) -> Self {
|
||||
MixedComponents::default().with_value(MixedOp::Add(any))
|
||||
MixedComponents::default().with_value(AnyOp::Add(any))
|
||||
}
|
||||
|
||||
pub(crate) fn merge(mixes: &[Option<&MixedComponents>]) -> Self {
|
||||
|
|
@ -84,24 +130,24 @@ impl MixedComponents {
|
|||
// MixedComponents BUILDER.
|
||||
|
||||
#[fn_builder]
|
||||
pub fn alter_value(&mut self, op: MixedOp) -> &mut Self {
|
||||
pub fn alter_value(&mut self, op: AnyOp) -> &mut Self {
|
||||
match op {
|
||||
MixedOp::Add(any) => self.0.push(any),
|
||||
MixedOp::InsertAfterId(id, any) => match self.0.iter().position(|c| c.id() == id) {
|
||||
AnyOp::Add(any) => self.0.push(any),
|
||||
AnyOp::InsertAfterId(id, any) => match self.0.iter().position(|c| c.id() == id) {
|
||||
Some(index) => self.0.insert(index + 1, any),
|
||||
_ => self.0.push(any),
|
||||
},
|
||||
MixedOp::InsertBeforeId(id, any) => match self.0.iter().position(|c| c.id() == id) {
|
||||
AnyOp::InsertBeforeId(id, any) => match self.0.iter().position(|c| c.id() == id) {
|
||||
Some(index) => self.0.insert(index, any),
|
||||
_ => self.0.insert(0, any),
|
||||
},
|
||||
MixedOp::Prepend(any) => self.0.insert(0, any),
|
||||
MixedOp::RemoveById(id) => {
|
||||
AnyOp::Prepend(any) => self.0.insert(0, any),
|
||||
AnyOp::RemoveById(id) => {
|
||||
if let Some(index) = self.0.iter().position(|c| c.id() == id) {
|
||||
self.0.remove(index);
|
||||
}
|
||||
}
|
||||
MixedOp::ReplaceById(id, any) => {
|
||||
AnyOp::ReplaceById(id, any) => {
|
||||
for c in self.0.iter_mut() {
|
||||
if c.id() == id {
|
||||
*c = any;
|
||||
|
|
@ -109,11 +155,40 @@ impl MixedComponents {
|
|||
}
|
||||
}
|
||||
}
|
||||
MixedOp::Reset => self.0.clear(),
|
||||
AnyOp::Reset => self.0.clear(),
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
#[fn_builder]
|
||||
#[rustfmt::skip]
|
||||
pub fn alter_typed<C: ComponentTrait + Default>(&mut self, op: TypedOp<C>) -> &mut Self {
|
||||
match op {
|
||||
TypedOp::Add(typed) => {
|
||||
self.alter_value(AnyOp::Add(typed.to_any()))
|
||||
}
|
||||
TypedOp::InsertAfterId(id, typed) => {
|
||||
self.alter_value(AnyOp::InsertAfterId(id, typed.to_any()))
|
||||
}
|
||||
TypedOp::InsertBeforeId(id, typed) => {
|
||||
self.alter_value(AnyOp::InsertBeforeId(id, typed.to_any()))
|
||||
}
|
||||
TypedOp::Prepend(typed) => {
|
||||
self.alter_value(AnyOp::Prepend(typed.to_any()))
|
||||
}
|
||||
TypedOp::RemoveById(id) => {
|
||||
self.alter_value(AnyOp::RemoveById(id))
|
||||
}
|
||||
TypedOp::ReplaceById(id, typed) => {
|
||||
self.alter_value(AnyOp::ReplaceById(id, typed.to_any()))
|
||||
}
|
||||
TypedOp::Reset => {
|
||||
self.alter_value(AnyOp::Reset)
|
||||
}
|
||||
};
|
||||
self
|
||||
}
|
||||
|
||||
// MixedComponents GETTERS.
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
use crate::core::component::{AnyComponent, MixedComponents, MixedOp};
|
||||
use crate::core::component::{AnyComponent, AnyOp, MixedComponents};
|
||||
use crate::core::theme::ThemeRef;
|
||||
use crate::{fn_builder, AutoDefault, LazyStatic, TypeId};
|
||||
|
||||
|
|
@ -16,11 +16,11 @@ pub struct ComponentsInRegions(HashMap<&'static str, MixedComponents>);
|
|||
|
||||
impl ComponentsInRegions {
|
||||
pub fn new(region: &'static str, any: AnyComponent) -> Self {
|
||||
ComponentsInRegions::default().with_components(region, MixedOp::Add(any))
|
||||
ComponentsInRegions::default().with_components(region, AnyOp::Add(any))
|
||||
}
|
||||
|
||||
#[fn_builder]
|
||||
pub fn alter_components(&mut self, region: &'static str, op: MixedOp) -> &mut Self {
|
||||
pub fn alter_components(&mut self, region: &'static str, op: AnyOp) -> &mut Self {
|
||||
if let Some(region) = self.0.get_mut(region) {
|
||||
region.alter_value(op);
|
||||
} else {
|
||||
|
|
@ -52,18 +52,18 @@ impl InRegion {
|
|||
COMMON_REGIONS
|
||||
.write()
|
||||
.unwrap()
|
||||
.alter_components("content", MixedOp::Add(any));
|
||||
.alter_components("content", AnyOp::Add(any));
|
||||
}
|
||||
InRegion::Named(name) => {
|
||||
COMMON_REGIONS
|
||||
.write()
|
||||
.unwrap()
|
||||
.alter_components(name, MixedOp::Add(any));
|
||||
.alter_components(name, AnyOp::Add(any));
|
||||
}
|
||||
InRegion::OfTheme(region, theme) => {
|
||||
let mut regions = THEME_REGIONS.write().unwrap();
|
||||
if let Some(r) = regions.get_mut(&theme.type_id()) {
|
||||
r.alter_components(region, MixedOp::Add(any));
|
||||
r.alter_components(region, AnyOp::Add(any));
|
||||
} else {
|
||||
regions.insert(theme.type_id(), ComponentsInRegions::new(region, any));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue