🔥 Refactor TypeId/Any use, drop own Handle
This commit is contained in:
parent
8402b7946e
commit
169e562488
59 changed files with 137 additions and 289 deletions
|
|
@ -1,5 +1,5 @@
|
|||
mod definition;
|
||||
pub use definition::{action_ref, ActionBase, ActionTrait};
|
||||
pub use definition::{action_ref, ActionTrait};
|
||||
|
||||
mod list;
|
||||
pub use list::Action;
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
use crate::core::action::{Action, ActionsList};
|
||||
use crate::{Handle, LazyStatic};
|
||||
use crate::{LazyStatic, TypeId};
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::RwLock;
|
||||
|
||||
pub type KeyAction = (Handle, Option<Handle>, Option<String>);
|
||||
pub type KeyAction = (TypeId, Option<TypeId>, Option<String>);
|
||||
|
||||
// Registered actions.
|
||||
static ACTIONS: LazyStatic<RwLock<HashMap<KeyAction, ActionsList>>> =
|
||||
|
|
@ -13,8 +13,8 @@ static ACTIONS: LazyStatic<RwLock<HashMap<KeyAction, ActionsList>>> =
|
|||
pub fn add_action(action: Action) {
|
||||
let mut actions = ACTIONS.write().unwrap();
|
||||
let key_action = (
|
||||
action.handle(),
|
||||
action.referer_handle(),
|
||||
action.type_id(),
|
||||
action.referer_type_id(),
|
||||
action.referer_id(),
|
||||
);
|
||||
if let Some(list) = actions.get_mut(&key_action) {
|
||||
|
|
|
|||
|
|
@ -1,13 +1,8 @@
|
|||
use crate::{Handle, ImplementHandle, Weight};
|
||||
use crate::core::AnyBase;
|
||||
use crate::{TypeId, Weight};
|
||||
|
||||
use std::any::Any;
|
||||
|
||||
pub trait ActionBase: Any {
|
||||
fn as_ref_any(&self) -> &dyn Any;
|
||||
}
|
||||
|
||||
pub trait ActionTrait: ActionBase + ImplementHandle + Send + Sync {
|
||||
fn referer_handle(&self) -> Option<Handle> {
|
||||
pub trait ActionTrait: AnyBase + Send + Sync {
|
||||
fn referer_type_id(&self) -> Option<TypeId> {
|
||||
None
|
||||
}
|
||||
|
||||
|
|
@ -20,12 +15,6 @@ pub trait ActionTrait: ActionBase + ImplementHandle + Send + Sync {
|
|||
}
|
||||
}
|
||||
|
||||
impl<C: ActionTrait> ActionBase for C {
|
||||
fn as_ref_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
pub fn action_ref<A: 'static>(action: &dyn ActionTrait) -> &A {
|
||||
action.as_ref_any().downcast_ref::<A>().unwrap()
|
||||
action.as_any_ref().downcast_ref::<A>().unwrap()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use crate::core::component::{ComponentTrait, Context};
|
||||
use crate::html::{html, Markup};
|
||||
use crate::{fn_with, Handle, Weight};
|
||||
use crate::{fn_with, TypeId, Weight};
|
||||
|
||||
use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};
|
||||
|
||||
|
|
@ -36,8 +36,8 @@ impl ArcAnyComponent {
|
|||
|
||||
// ArcAnyComponent HELPERS.
|
||||
|
||||
fn handle(&self) -> Handle {
|
||||
self.0.read().unwrap().handle()
|
||||
fn type_id(&self) -> TypeId {
|
||||
self.0.read().unwrap().type_id()
|
||||
}
|
||||
|
||||
fn id(&self) -> String {
|
||||
|
|
@ -126,8 +126,8 @@ impl AnyComponents {
|
|||
self.0.iter().filter(move |&c| c.id() == id)
|
||||
}
|
||||
|
||||
pub fn iter_by_handle(&self, handle: Handle) -> impl Iterator<Item = &ArcAnyComponent> {
|
||||
self.0.iter().filter(move |&c| c.handle() == handle)
|
||||
pub fn iter_by_type_id(&self, type_id: TypeId) -> impl Iterator<Item = &ArcAnyComponent> {
|
||||
self.0.iter().filter(move |&c| c.type_id() == type_id)
|
||||
}
|
||||
|
||||
// AnyComponents RENDER.
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use crate::core::component::{ComponentTrait, Context};
|
||||
use crate::html::{html, Markup};
|
||||
use crate::{fn_with, Handle, Weight};
|
||||
use crate::{fn_with, TypeId, Weight};
|
||||
|
||||
use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};
|
||||
|
||||
|
|
@ -41,8 +41,8 @@ impl<C: ComponentTrait> ArcTypedComponent<C> {
|
|||
|
||||
// ArcTypedComponent HELPERS.
|
||||
|
||||
fn handle(&self) -> Handle {
|
||||
self.0.read().unwrap().handle()
|
||||
fn type_id(&self) -> TypeId {
|
||||
self.0.read().unwrap().type_id()
|
||||
}
|
||||
|
||||
fn id(&self) -> String {
|
||||
|
|
@ -123,8 +123,8 @@ impl<C: ComponentTrait + Default> TypedComponents<C> {
|
|||
self.0.iter().filter(move |&c| c.id() == id)
|
||||
}
|
||||
|
||||
pub fn iter_by_handle(&self, handle: Handle) -> impl Iterator<Item = &ArcTypedComponent<C>> {
|
||||
self.0.iter().filter(move |&c| c.handle() == handle)
|
||||
pub fn iter_by_type_id(&self, type_id: TypeId) -> impl Iterator<Item = &ArcTypedComponent<C>> {
|
||||
self.0.iter().filter(move |&c| c.type_id() == type_id)
|
||||
}
|
||||
|
||||
// TypedComponents RENDER.
|
||||
|
|
|
|||
|
|
@ -1,19 +1,14 @@
|
|||
use crate::base::action;
|
||||
use crate::core::component::Context;
|
||||
use crate::core::AnyBase;
|
||||
use crate::html::{html, Markup, PrepareMarkup};
|
||||
use crate::{util, ImplementHandle, Weight};
|
||||
use crate::{util, Weight};
|
||||
|
||||
use std::any::Any;
|
||||
|
||||
pub trait ComponentBase: Any {
|
||||
pub trait ComponentBase {
|
||||
fn render(&mut self, cx: &mut Context) -> Markup;
|
||||
|
||||
fn as_ref_any(&self) -> &dyn Any;
|
||||
|
||||
fn as_mut_any(&mut self) -> &mut dyn Any;
|
||||
}
|
||||
|
||||
pub trait ComponentTrait: ComponentBase + ImplementHandle + Send + Sync {
|
||||
pub trait ComponentTrait: AnyBase + ComponentBase + Send + Sync {
|
||||
fn new() -> Self
|
||||
where
|
||||
Self: Sized;
|
||||
|
|
@ -87,20 +82,12 @@ impl<C: ComponentTrait> ComponentBase for C {
|
|||
html! {}
|
||||
}
|
||||
}
|
||||
|
||||
fn as_ref_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
|
||||
fn as_mut_any(&mut self) -> &mut dyn Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
pub fn component_as_ref<C: ComponentTrait>(component: &dyn ComponentTrait) -> Option<&C> {
|
||||
component.as_ref_any().downcast_ref::<C>()
|
||||
component.as_any_ref().downcast_ref::<C>()
|
||||
}
|
||||
|
||||
pub fn component_as_mut<C: ComponentTrait>(component: &mut dyn ComponentTrait) -> Option<&mut C> {
|
||||
component.as_mut_any().downcast_mut::<C>()
|
||||
component.as_any_mut().downcast_mut::<C>()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
mod definition;
|
||||
pub use definition::{PackageBase, PackageRef, PackageTrait};
|
||||
pub use definition::{PackageRef, PackageTrait};
|
||||
|
||||
pub(crate) mod all;
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ pub fn register_packages(app: PackageRef) {
|
|||
|
||||
fn add_to_dropped(list: &mut Vec<PackageRef>, package: PackageRef) {
|
||||
for d in package.drop_packages().iter() {
|
||||
if !list.iter().any(|p| p.handle() == d.handle()) {
|
||||
if !list.iter().any(|p| p.type_id() == d.type_id()) {
|
||||
list.push(*d);
|
||||
trace::debug!("Package \"{}\" dropped", d.single_name());
|
||||
}
|
||||
|
|
@ -54,12 +54,12 @@ fn add_to_dropped(list: &mut Vec<PackageRef>, package: PackageRef) {
|
|||
}
|
||||
|
||||
fn add_to_enabled(list: &mut Vec<PackageRef>, package: PackageRef) {
|
||||
if !list.iter().any(|p| p.handle() == package.handle()) {
|
||||
if !list.iter().any(|p| p.type_id() == package.type_id()) {
|
||||
if DROPPED_PACKAGES
|
||||
.read()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.any(|p| p.handle() == package.handle())
|
||||
.any(|p| p.type_id() == package.type_id())
|
||||
{
|
||||
panic!(
|
||||
"Trying to enable \"{}\" package which is dropped",
|
||||
|
|
@ -78,7 +78,7 @@ fn add_to_enabled(list: &mut Vec<PackageRef>, package: PackageRef) {
|
|||
let mut registered_themes = THEMES.write().unwrap();
|
||||
if !registered_themes
|
||||
.iter()
|
||||
.any(|t| t.handle() == theme.handle())
|
||||
.any(|t| t.type_id() == theme.type_id())
|
||||
{
|
||||
registered_themes.push(theme);
|
||||
trace::debug!("Enabling \"{}\" theme", theme.single_name());
|
||||
|
|
|
|||
|
|
@ -1,19 +1,16 @@
|
|||
use crate::core::action::Action;
|
||||
use crate::core::theme::ThemeRef;
|
||||
use crate::core::AnyBase;
|
||||
use crate::locale::L10n;
|
||||
use crate::{actions, service, util, ImplementHandle};
|
||||
use crate::{actions, service};
|
||||
|
||||
#[cfg(feature = "database")]
|
||||
use crate::{db::MigrationItem, migrations};
|
||||
|
||||
pub type PackageRef = &'static dyn PackageTrait;
|
||||
|
||||
pub trait PackageBase {
|
||||
fn single_name(&self) -> &'static str;
|
||||
}
|
||||
|
||||
/// Los paquetes deben implementar este *trait*.
|
||||
pub trait PackageTrait: ImplementHandle + PackageBase + Send + Sync {
|
||||
pub trait PackageTrait: AnyBase + Send + Sync {
|
||||
fn name(&self) -> L10n {
|
||||
L10n::n(self.single_name())
|
||||
}
|
||||
|
|
@ -41,7 +38,6 @@ pub trait PackageTrait: ImplementHandle + PackageBase + Send + Sync {
|
|||
fn init(&self) {}
|
||||
|
||||
#[cfg(feature = "database")]
|
||||
#[allow(unused_variables)]
|
||||
fn migrations(&self) -> Vec<MigrationItem> {
|
||||
migrations![]
|
||||
}
|
||||
|
|
@ -49,9 +45,3 @@ pub trait PackageTrait: ImplementHandle + PackageBase + Send + Sync {
|
|||
#[allow(unused_variables)]
|
||||
fn configure_service(&self, scfg: &mut service::web::ServiceConfig) {}
|
||||
}
|
||||
|
||||
impl<M: ?Sized + PackageTrait> PackageBase for M {
|
||||
fn single_name(&self) -> &'static str {
|
||||
util::single_type_name::<Self>()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -118,10 +118,11 @@ pub trait ThemeTrait: PackageTrait + Send + Sync {
|
|||
/*
|
||||
Cómo usarlo:
|
||||
|
||||
match component.handle() {
|
||||
BLOCK_COMPONENT => {
|
||||
let block = component_as_mut::<Block>(component);
|
||||
block.alter_title("New title");
|
||||
match component.type_id() {
|
||||
t if t == TypeId::of::<Block>() => {
|
||||
if let Some(b) = component_as_mut::<Block>(component) {
|
||||
b.alter_title("New title");
|
||||
}
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
|
|
@ -138,10 +139,11 @@ pub trait ThemeTrait: PackageTrait + Send + Sync {
|
|||
/*
|
||||
Cómo usarlo:
|
||||
|
||||
match component.handle() {
|
||||
BLOCK_COMPONENT => {
|
||||
let block = component_as_mut::<Block>(component);
|
||||
block.alter_title("New title");
|
||||
match component.type_id() {
|
||||
t if t == TypeId::of::<Block>() => {
|
||||
if let Some(b) = component_as_mut::<Block>(component) {
|
||||
b.alter_title("New title");
|
||||
}
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
|
|
@ -159,13 +161,9 @@ pub trait ThemeTrait: PackageTrait + Send + Sync {
|
|||
/*
|
||||
Cómo usarlo:
|
||||
|
||||
match component.handle() {
|
||||
BLOCK_COMPONENT => {
|
||||
let block = component_as_ref::<Block>(component);
|
||||
match block.template() {
|
||||
"default" => Some(block_default(block)),
|
||||
_ => None,
|
||||
}
|
||||
match component.type_id() {
|
||||
t if t == TypeId::of::<Block>() => {
|
||||
Some(block_default(block))
|
||||
},
|
||||
_ => None,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
use crate::core::component::{AnyComponents, ArcAnyComponent, ArcAnyOp};
|
||||
use crate::core::theme::ThemeRef;
|
||||
use crate::{Handle, LazyStatic, SmartDefault};
|
||||
use crate::{LazyStatic, SmartDefault, TypeId};
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::RwLock;
|
||||
|
||||
static THEME_REGIONS: LazyStatic<RwLock<HashMap<Handle, ComponentsInRegions>>> =
|
||||
static THEME_REGIONS: LazyStatic<RwLock<HashMap<TypeId, ComponentsInRegions>>> =
|
||||
LazyStatic::new(|| RwLock::new(HashMap::new()));
|
||||
|
||||
static COMMON_REGIONS: LazyStatic<RwLock<ComponentsInRegions>> =
|
||||
|
|
@ -31,7 +31,7 @@ impl ComponentsInRegions {
|
|||
|
||||
pub fn get_components(&self, theme: ThemeRef, region: &str) -> AnyComponents {
|
||||
let common = COMMON_REGIONS.read().unwrap();
|
||||
if let Some(r) = THEME_REGIONS.read().unwrap().get(&theme.handle()) {
|
||||
if let Some(r) = THEME_REGIONS.read().unwrap().get(&theme.type_id()) {
|
||||
AnyComponents::merge(&[common.0.get(region), self.0.get(region), r.0.get(region)])
|
||||
} else {
|
||||
AnyComponents::merge(&[common.0.get(region), self.0.get(region)])
|
||||
|
|
@ -51,10 +51,10 @@ pub fn add_component_in(region: Region, arc: ArcAnyComponent) {
|
|||
}
|
||||
Region::OfTheme(theme, region) => {
|
||||
let mut regions = THEME_REGIONS.write().unwrap();
|
||||
if let Some(r) = regions.get_mut(&theme.handle()) {
|
||||
if let Some(r) = regions.get_mut(&theme.type_id()) {
|
||||
r.add_component_in(region, arc);
|
||||
} else {
|
||||
regions.insert(theme.handle(), ComponentsInRegions::new(region, arc));
|
||||
regions.insert(theme.type_id(), ComponentsInRegions::new(region, arc));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue