reactive_graph_plugin_delegates/
entity_component_behaviour_registry_impl.rs

1use async_trait::async_trait;
2use std::sync::Arc;
3
4use uuid::Uuid;
5
6use reactive_graph_behaviour_model_api::prelude::*;
7
8use reactive_graph_reactive_model_impl::ReactiveEntity;
9
10pub struct EntityComponentBehaviourRegistryDelegate {
11    entity_component_behaviour_manager: Arc<dyn reactive_graph_behaviour_service_api::EntityComponentBehaviourManager + Send + Sync>,
12    entity_component_behaviour_registry: Arc<dyn reactive_graph_behaviour_service_api::EntityComponentBehaviourRegistry + Send + Sync>,
13    reactive_entity_manager: Arc<dyn reactive_graph_reactive_service_api::ReactiveEntityManager + Send + Sync>,
14}
15
16impl EntityComponentBehaviourRegistryDelegate {
17    pub fn new(
18        entity_component_behaviour_manager: Arc<dyn reactive_graph_behaviour_service_api::EntityComponentBehaviourManager + Send + Sync>,
19        entity_component_behaviour_registry: Arc<dyn reactive_graph_behaviour_service_api::EntityComponentBehaviourRegistry + Send + Sync>,
20        reactive_entity_manager: Arc<dyn reactive_graph_reactive_service_api::ReactiveEntityManager + Send + Sync>,
21    ) -> Self {
22        Self {
23            entity_component_behaviour_manager,
24            entity_component_behaviour_registry,
25            reactive_entity_manager,
26        }
27    }
28}
29
30#[async_trait]
31impl reactive_graph_plugin_api::EntityComponentBehaviourRegistry for EntityComponentBehaviourRegistryDelegate {
32    async fn register(&self, component_behaviour_ty: ComponentBehaviourTypeId, factory: Arc<dyn BehaviourFactory<Uuid, ReactiveEntity> + Send + Sync>) {
33        self.entity_component_behaviour_registry.register(component_behaviour_ty.clone(), factory);
34        self.reactive_entity_manager.add_behaviour_to_all_entity_components(&component_behaviour_ty);
35    }
36
37    async fn unregister(&self, component_behaviour_ty: &ComponentBehaviourTypeId) {
38        self.entity_component_behaviour_registry.unregister(component_behaviour_ty);
39        self.entity_component_behaviour_manager
40            .remove_behaviours_by_behaviour(&component_behaviour_ty.behaviour_ty);
41    }
42}