reactive_graph_behaviour_service_impl/
relation_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;
9
10use reactive_graph_behaviour_model_api::prelude::*;
11use reactive_graph_behaviour_service_api::RelationBehaviourRegistry;
12use reactive_graph_graph::RelationInstanceId;
13use reactive_graph_graph::RelationTypeId;
14use reactive_graph_lifecycle::Lifecycle;
15use reactive_graph_reactive_model_impl::ReactiveRelation;
16use reactive_graph_type_system_api::RelationTypeManager;
17
18#[derive(Component)]
19pub struct RelationBehaviourRegistryImpl {
20    relation_type_manager: Arc<dyn RelationTypeManager + Send + Sync>,
21
22    #[component(default = "DashMap::new")]
23    factories: DashMap<RelationBehaviourTypeId, Arc<dyn BehaviourFactory<RelationInstanceId, ReactiveRelation> + Send + Sync>>,
24}
25
26#[async_trait]
27#[component_alias]
28impl RelationBehaviourRegistry for RelationBehaviourRegistryImpl {
29    fn register(&self, relation_behaviour_ty: RelationBehaviourTypeId, factory: Arc<dyn BehaviourFactory<RelationInstanceId, ReactiveRelation> + Send + Sync>) {
30        debug!(
31            "Registering relation behaviour {} {}",
32            &relation_behaviour_ty.relation_ty, &relation_behaviour_ty.behaviour_ty
33        );
34        if !self.relation_type_manager.has(&relation_behaviour_ty.relation_ty) {
35            warn!(
36                "Relation behaviour {} is registered on a non-existent relation type {}",
37                &relation_behaviour_ty.behaviour_ty, &relation_behaviour_ty.relation_ty
38            )
39        }
40        self.factories.insert(relation_behaviour_ty, factory);
41    }
42
43    fn unregister(&self, relation_behaviour_ty: &RelationBehaviourTypeId) {
44        debug!(
45            "Unregistering relation behaviour {} {}",
46            &relation_behaviour_ty.relation_ty, &relation_behaviour_ty.behaviour_ty
47        );
48        self.factories.remove(relation_behaviour_ty);
49    }
50
51    fn get_all(&self) -> Vec<RelationBehaviourTypeId> {
52        self.factories.iter().map(|f| f.key().clone()).collect()
53    }
54
55    fn get(&self, relation_ty: &RelationTypeId) -> Vec<Arc<dyn BehaviourFactory<RelationInstanceId, ReactiveRelation> + Send + Sync>> {
56        self.factories
57            .iter()
58            .filter(|factory| &factory.key().relation_ty == relation_ty)
59            .map(|factory| factory.value().clone())
60            .collect()
61    }
62
63    fn get_factory_by_behaviour_type(
64        &self,
65        behaviour_ty: &BehaviourTypeId,
66    ) -> Option<Arc<dyn BehaviourFactory<RelationInstanceId, ReactiveRelation> + Send + Sync>> {
67        self.factories
68            .iter()
69            .find(|factory| &factory.key().behaviour_ty == behaviour_ty)
70            .map(|factory| factory.value().clone())
71    }
72
73    fn get_behaviour_types(&self, relation_ty: &RelationTypeId) -> Vec<RelationBehaviourTypeId> {
74        self.factories
75            .iter()
76            .filter(|factory| &factory.key().relation_ty == relation_ty)
77            .map(|factory| factory.key().clone())
78            .collect()
79    }
80
81    fn get_by_behaviour_type(&self, behaviour_ty: &BehaviourTypeId) -> Option<RelationBehaviourTypeId> {
82        self.factories
83            .iter()
84            .find(|factory| &factory.key().behaviour_ty == behaviour_ty)
85            .map(|factory| factory.key().clone())
86    }
87
88    fn count(&self) -> usize {
89        self.factories.len()
90    }
91}
92
93#[async_trait]
94impl Lifecycle for RelationBehaviourRegistryImpl {}