reactive_graph_behaviour_service_impl/
entity_component_behaviour_registry_impl.rs

1use std::sync::Arc;
2
3use async_trait::async_trait;
4use dashmap::DashMap;
5use log::debug;
6use log::warn;
7use springtime_di::Component;
8use springtime_di::component_alias;
9use uuid::Uuid;
10
11use reactive_graph_behaviour_model_api::prelude::*;
12use reactive_graph_behaviour_service_api::EntityComponentBehaviourRegistry;
13use reactive_graph_graph::ComponentTypeId;
14use reactive_graph_lifecycle::Lifecycle;
15use reactive_graph_reactive_model_impl::ReactiveEntity;
16use reactive_graph_type_system_api::ComponentManager;
17
18#[derive(Component)]
19pub struct EntityComponentBehaviourRegistryImpl {
20    component_manager: Arc<dyn ComponentManager + Send + Sync>,
21
22    #[component(default = "DashMap::new")]
23    factories: DashMap<ComponentBehaviourTypeId, Arc<dyn BehaviourFactory<Uuid, ReactiveEntity> + Send + Sync>>,
24}
25
26#[async_trait]
27#[component_alias]
28impl EntityComponentBehaviourRegistry for EntityComponentBehaviourRegistryImpl {
29    fn register(&self, component_behaviour_ty: ComponentBehaviourTypeId, factory: Arc<dyn BehaviourFactory<Uuid, ReactiveEntity> + Send + Sync>) {
30        debug!(
31            "Registering entity component behaviour {} {}",
32            &component_behaviour_ty.component_ty, &component_behaviour_ty.behaviour_ty
33        );
34        if !self.component_manager.has(&component_behaviour_ty.component_ty) {
35            warn!(
36                "Entity component behaviour {} is registered on a non-existent component {}",
37                &component_behaviour_ty.behaviour_ty, &component_behaviour_ty.component_ty
38            )
39        }
40        self.factories.insert(component_behaviour_ty, factory);
41    }
42
43    fn unregister(&self, component_behaviour_ty: &ComponentBehaviourTypeId) {
44        debug!(
45            "Unregistering entity component behaviour {} {}",
46            &component_behaviour_ty.component_ty, &component_behaviour_ty.behaviour_ty
47        );
48        self.factories.remove(component_behaviour_ty);
49    }
50
51    fn get_all(&self) -> Vec<ComponentBehaviourTypeId> {
52        self.factories.iter().map(|f| f.key().clone()).collect()
53    }
54
55    fn get(&self, component_ty: &ComponentTypeId) -> Vec<Arc<dyn BehaviourFactory<Uuid, ReactiveEntity> + Send + Sync>> {
56        self.factories
57            .iter()
58            .filter(|factory| &factory.key().component_ty == component_ty)
59            .map(|factory| factory.value().clone())
60            .collect()
61    }
62
63    fn get_behaviour_types(&self, component_ty: &ComponentTypeId) -> Vec<ComponentBehaviourTypeId> {
64        self.factories
65            .iter()
66            .filter(|factory| &factory.key().component_ty == component_ty)
67            .map(|factory| factory.key().clone())
68            .collect()
69    }
70
71    fn get_by_behaviour_type(&self, behaviour_ty: &BehaviourTypeId) -> Option<ComponentBehaviourTypeId> {
72        self.factories
73            .iter()
74            .find(|factory| &factory.key().behaviour_ty == behaviour_ty)
75            .map(|factory| factory.key().clone())
76    }
77
78    fn count(&self) -> usize {
79        self.factories.len()
80    }
81}
82
83#[async_trait]
84impl Lifecycle for EntityComponentBehaviourRegistryImpl {}