reactive_graph_plugin_api/behaviours/relations/
relation_behaviour_registry.rs

1use std::ops::Deref;
2use std::sync::Arc;
3
4use async_trait::async_trait;
5use tokio_stream::StreamExt;
6
7use reactive_graph_behaviour_model_api::prelude::*;
8
9use reactive_graph_graph::RelationInstanceId;
10use reactive_graph_reactive_model_impl::ReactiveRelation;
11
12#[async_trait]
13pub trait RelationBehaviourRegistry: Send + Sync {
14    /// Registers a factory for creating relation behaviours.
15    /// If a relation instance is of the relation type then the given behaviour is applied.
16    /// The behaviour will be created using the given RelationBehaviourCreator.
17    #[allow(unused_variables)]
18    async fn register(
19        &self,
20        relation_behaviour_ty: RelationBehaviourTypeId,
21        factory: Arc<dyn BehaviourFactory<RelationInstanceId, ReactiveRelation> + Send + Sync>,
22    );
23
24    async fn register_all(&self, factories: &BehaviourFactories<RelationInstanceId, ReactiveRelation>) {
25        let mut factories = tokio_stream::iter(factories.deref().clone());
26        while let Some((ty, factory)) = factories.next().await {
27            self.register(RelationBehaviourTypeId::from(&ty), factory).await
28        }
29    }
30
31    /// Unregisters a factory for creating relation behaviours.
32    #[allow(unused_variables)]
33    async fn unregister(&self, relation_behaviour_ty: &RelationBehaviourTypeId);
34
35    /// Unregisters the behaviour factories for the given relation behaviour types.
36    async fn unregister_all(&self, tys: &RelationBehaviourTypeIds) {
37        let mut tys = tokio_stream::iter(tys.iter());
38        while let Some(ty) = tys.next().await {
39            self.unregister(&ty).await
40        }
41    }
42}