♻️ Major code restructuring
This commit is contained in:
parent
a96e203bb3
commit
fa66d628a0
221 changed files with 228 additions and 315 deletions
34
src/core/action/all.rs
Normal file
34
src/core/action/all.rs
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
use crate::core::action::{Action, ActionsList};
|
||||
use crate::{Handle, LazyStatic};
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::RwLock;
|
||||
|
||||
pub type KeyAction = (Handle, Option<Handle>, Option<String>);
|
||||
|
||||
// Registered actions.
|
||||
static ACTIONS: LazyStatic<RwLock<HashMap<KeyAction, ActionsList>>> =
|
||||
LazyStatic::new(|| RwLock::new(HashMap::new()));
|
||||
|
||||
pub fn add_action(action: Action) {
|
||||
let mut actions = ACTIONS.write().unwrap();
|
||||
let key_action = (
|
||||
action.handle(),
|
||||
action.referer_handle(),
|
||||
action.referer_id(),
|
||||
);
|
||||
if let Some(list) = actions.get_mut(&key_action) {
|
||||
list.add(action);
|
||||
} else {
|
||||
actions.insert(key_action, ActionsList::new(action));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn dispatch_actions<B, F>(key_action: KeyAction, f: F)
|
||||
where
|
||||
F: FnMut(&Action) -> B,
|
||||
{
|
||||
if let Some(list) = ACTIONS.read().unwrap().get(&key_action) {
|
||||
list.iter_map(f)
|
||||
}
|
||||
}
|
||||
31
src/core/action/definition.rs
Normal file
31
src/core/action/definition.rs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
use crate::{Handle, ImplementHandle, 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> {
|
||||
None
|
||||
}
|
||||
|
||||
fn referer_id(&self) -> Option<String> {
|
||||
None
|
||||
}
|
||||
|
||||
fn weight(&self) -> Weight {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
45
src/core/action/list.rs
Normal file
45
src/core/action/list.rs
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
use crate::core::action::ActionTrait;
|
||||
use crate::SmartDefault;
|
||||
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
pub type Action = Box<dyn ActionTrait>;
|
||||
|
||||
#[derive(SmartDefault)]
|
||||
pub struct ActionsList(Arc<RwLock<Vec<Action>>>);
|
||||
|
||||
impl ActionsList {
|
||||
pub fn new(action: Action) -> Self {
|
||||
let mut list = ActionsList::default();
|
||||
list.add(action);
|
||||
list
|
||||
}
|
||||
|
||||
pub fn add(&mut self, action: Action) {
|
||||
let mut list = self.0.write().unwrap();
|
||||
list.push(action);
|
||||
list.sort_by_key(|a| a.weight());
|
||||
}
|
||||
|
||||
pub fn iter_map<B, F>(&self, f: F)
|
||||
where
|
||||
Self: Sized,
|
||||
F: FnMut(&Action) -> B,
|
||||
{
|
||||
let _: Vec<_> = self.0.read().unwrap().iter().map(f).collect();
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! actions {
|
||||
() => {
|
||||
Vec::<Action>::new()
|
||||
};
|
||||
( $($action:expr),+ $(,)? ) => {{
|
||||
let mut v = Vec::<Action>::new();
|
||||
$(
|
||||
v.push(Box::new($action));
|
||||
)*
|
||||
v
|
||||
}};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue