reactive_graph_std_array/
plugin.rs

1use reactive_graph_plugin_api::EntityBehaviourRegistry;
2use reactive_graph_plugin_api::prelude::plugin::*;
3use reactive_graph_plugin_api::prelude::providers::*;
4
5use crate::behaviour::entity::contains::ArrayContainsFactory;
6use crate::behaviour::entity::get_by_index::ArrayGetByIndexFactory;
7use crate::behaviour::entity::length::ArrayLengthFactory;
8use crate::behaviour::entity::pop::ArrayPopFactory;
9use crate::behaviour::entity::push::ArrayPushFactory;
10use crate::behaviour::entity::reverse::ArrayReverseFactory;
11use reactive_graph_std_array_model::BEHAVIOUR_ARRAY_CONTAINS;
12use reactive_graph_std_array_model::BEHAVIOUR_ARRAY_GET_BY_INDEX;
13use reactive_graph_std_array_model::BEHAVIOUR_ARRAY_LENGTH;
14use reactive_graph_std_array_model::BEHAVIOUR_ARRAY_POP;
15use reactive_graph_std_array_model::BEHAVIOUR_ARRAY_PUSH;
16use reactive_graph_std_array_model::BEHAVIOUR_ARRAY_REVERSE;
17use reactive_graph_std_array_model::ENTITY_BEHAVIOUR_ARRAY_CONTAINS;
18use reactive_graph_std_array_model::ENTITY_BEHAVIOUR_ARRAY_GET_BY_INDEX;
19use reactive_graph_std_array_model::ENTITY_BEHAVIOUR_ARRAY_LENGTH;
20use reactive_graph_std_array_model::ENTITY_BEHAVIOUR_ARRAY_POP;
21use reactive_graph_std_array_model::ENTITY_BEHAVIOUR_ARRAY_PUSH;
22use reactive_graph_std_array_model::ENTITY_BEHAVIOUR_ARRAY_REVERSE;
23
24export_plugin!({
25    "dependencies": [
26        { "name": "reactive-graph-std-base", "version": ">=0.10.0, <0.11.0" },
27        { "name": "reactive-graph-std-result", "version": ">=0.10.0, <0.11.0" },
28        { "name": "reactive-graph-std-trigger", "version": ">=0.10.0, <0.11.0" }
29    ]
30});
31
32#[injectable]
33pub trait ArrayPlugin: Plugin + Send + Sync {}
34
35#[derive(Component)]
36pub struct ArrayPluginImpl {
37    entity_types_provider: Arc<dyn TypeProvider<EntityTypes> + Send + Sync>,
38
39    #[component(default = "entity_types_provider_registry")]
40    entity_type_provider_registry: Arc<dyn EntityTypeProviderRegistry + Send + Sync>,
41
42    #[component(default = "entity_behaviour_registry")]
43    entity_behaviour_registry: Arc<dyn EntityBehaviourRegistry + Send + Sync>,
44}
45
46#[async_trait]
47#[component_alias]
48impl Plugin for ArrayPluginImpl {
49    async fn activate(&self) -> Result<(), PluginActivationError> {
50        self.entity_type_provider_registry.register_provider(self.entity_types_provider.clone()).await;
51
52        // Array Contains
53        let factory = Arc::new(ArrayContainsFactory::new(BEHAVIOUR_ARRAY_CONTAINS.clone()));
54        self.entity_behaviour_registry.register(ENTITY_BEHAVIOUR_ARRAY_CONTAINS.clone(), factory).await;
55
56        // Array Get By Index
57        let factory = Arc::new(ArrayGetByIndexFactory::new(BEHAVIOUR_ARRAY_GET_BY_INDEX.clone()));
58        self.entity_behaviour_registry
59            .register(ENTITY_BEHAVIOUR_ARRAY_GET_BY_INDEX.clone(), factory)
60            .await;
61
62        // Array Length
63        let factory = Arc::new(ArrayLengthFactory::new(BEHAVIOUR_ARRAY_LENGTH.clone()));
64        self.entity_behaviour_registry.register(ENTITY_BEHAVIOUR_ARRAY_LENGTH.clone(), factory).await;
65
66        // Array Pop
67        let factory = Arc::new(ArrayPopFactory::new(BEHAVIOUR_ARRAY_POP.clone()));
68        self.entity_behaviour_registry.register(ENTITY_BEHAVIOUR_ARRAY_POP.clone(), factory).await;
69
70        // Array Push
71        let factory = Arc::new(ArrayPushFactory::new(BEHAVIOUR_ARRAY_PUSH.clone()));
72        self.entity_behaviour_registry.register(ENTITY_BEHAVIOUR_ARRAY_PUSH.clone(), factory).await;
73
74        // Array Reverse
75        let factory = Arc::new(ArrayReverseFactory::new(BEHAVIOUR_ARRAY_REVERSE.clone()));
76        self.entity_behaviour_registry.register(ENTITY_BEHAVIOUR_ARRAY_REVERSE.clone(), factory).await;
77
78        Ok(())
79    }
80
81    async fn deactivate(&self) -> Result<(), PluginDeactivationError> {
82        self.entity_behaviour_registry.unregister(&ENTITY_BEHAVIOUR_ARRAY_CONTAINS).await;
83        self.entity_behaviour_registry.unregister(&ENTITY_BEHAVIOUR_ARRAY_GET_BY_INDEX).await;
84        self.entity_behaviour_registry.unregister(&ENTITY_BEHAVIOUR_ARRAY_LENGTH).await;
85        self.entity_behaviour_registry.unregister(&ENTITY_BEHAVIOUR_ARRAY_POP).await;
86        self.entity_behaviour_registry.unregister(&ENTITY_BEHAVIOUR_ARRAY_PUSH).await;
87        self.entity_behaviour_registry.unregister(&ENTITY_BEHAVIOUR_ARRAY_REVERSE).await;
88
89        self.entity_type_provider_registry.unregister_provider(self.entity_types_provider.id()).await;
90        Ok(())
91    }
92}