reactive_graph_std_state/
plugin.rs

1use std::ops::Deref;
2
3use reactive_graph_plugin_api::prelude::plugin::*;
4use reactive_graph_plugin_api::prelude::providers::*;
5
6use crate::behaviour::component::state::STATE_BEHAVIOURS;
7use crate::behaviour::component::state::STATE_FACTORIES;
8use crate::behaviour::component::state_debugger::STATE_DEBUGGER_BEHAVIOURS;
9
10export_plugin!({
11    "dependencies": [
12        { "name": "reactive-graph-std-base", "version": ">=0.10.0, <0.11.0" },
13        { "name": "reactive-graph-std-value", "version": ">=0.10.0, <0.11.0" }
14    ]
15});
16
17#[derive(Component)]
18pub struct StatePlugin {
19    component_provider: Arc<dyn TypeProvider<Components> + Send + Sync>,
20
21    #[component(default = "component_provider_registry")]
22    component_provider_registry: Arc<dyn ComponentProviderRegistry + Send + Sync>,
23
24    entity_types_provider: Arc<dyn TypeProvider<EntityTypes> + Send + Sync>,
25
26    #[component(default = "entity_types_provider_registry")]
27    entity_type_provider_registry: Arc<dyn EntityTypeProviderRegistry + Send + Sync>,
28
29    #[component(default = "inject_plugin_context")]
30    context: Arc<dyn PluginContext + Send + Sync>,
31}
32
33#[async_trait]
34#[component_alias]
35impl Plugin for StatePlugin {
36    async fn activate(&self) -> Result<(), PluginActivationError> {
37        self.component_provider_registry.register_provider(self.component_provider.clone()).await;
38        self.entity_type_provider_registry.register_provider(self.entity_types_provider.clone()).await;
39        self.context
40            .get_entity_component_behaviour_registry()
41            .register_all(STATE_FACTORIES.deref())
42            .await;
43        self.context
44            .get_entity_component_behaviour_registry()
45            .register_all(&STATE_DEBUGGER_BEHAVIOURS.get_factories())
46            .await;
47        Ok(())
48    }
49
50    async fn deactivate(&self) -> Result<(), PluginDeactivationError> {
51        self.context
52            .get_entity_component_behaviour_registry()
53            .unregister_all(&STATE_DEBUGGER_BEHAVIOURS.to_component_behaviour_tys())
54            .await;
55        self.context
56            .get_entity_component_behaviour_registry()
57            .unregister_all(&STATE_BEHAVIOURS.deref().into())
58            .await;
59        self.entity_type_provider_registry.unregister_provider(self.entity_types_provider.id()).await;
60        self.component_provider_registry.unregister_provider(self.component_provider.id()).await;
61        Ok(())
62    }
63}