reactive_graph_behaviour_model_api/
function.rs1use std::ops::Deref;
2use std::sync::Arc;
3
4use dashmap::DashMap;
5use dashmap::ReadOnlyView;
6
7use reactive_graph_reactive_model_api::ReactiveInstance;
8
9use crate::BehaviourFactories;
10use crate::BehaviourFactory;
11use crate::BehaviourTypeId;
12use crate::ComponentBehaviourTypeId;
13use crate::ComponentBehaviourTypeIds;
14use crate::EntityBehaviourTypeId;
15use crate::EntityBehaviourTypeIds;
16use crate::RelationBehaviourTypeId;
17use crate::RelationBehaviourTypeIds;
18
19pub type BehaviourFactoryCreator<ID, T, FnType> = fn(&BehaviourTypeId, FnType) -> Arc<dyn BehaviourFactory<ID, T> + Send + Sync + 'static>;
22
23pub struct BehaviourFunctions<ID: Clone, T: ReactiveInstance<ID>, FnType: Clone>(DashMap<BehaviourTypeId, FnType>, BehaviourFactoryCreator<ID, T, FnType>);
24
25impl<ID: Clone, T: ReactiveInstance<ID>, FnType: Clone> BehaviourFunctions<ID, T, FnType> {
26 pub fn new(factory_creator: BehaviourFactoryCreator<ID, T, FnType>) -> Self {
27 Self(DashMap::new(), factory_creator)
28 }
29
30 pub fn behaviour_from_ty<B: Into<BehaviourTypeId>>(self, ty: B, f: FnType) -> Self {
31 self.0.insert(ty.into(), f);
32 self
33 }
34
35 pub fn component_behaviour_from_ty<B: Into<ComponentBehaviourTypeId>>(self, ty: B, f: FnType) -> Self {
36 self.0.insert(ty.into().behaviour_ty, f);
37 self
38 }
39
40 pub fn entity_behaviour_from_ty<B: Into<EntityBehaviourTypeId>>(self, ty: B, f: FnType) -> Self {
41 self.0.insert(ty.into().behaviour_ty, f);
42 self
43 }
44
45 pub fn relation_behaviour_from_ty<B: Into<RelationBehaviourTypeId>>(self, ty: B, f: FnType) -> Self {
46 self.0.insert(ty.into().behaviour_ty, f);
47 self
48 }
49
50 pub fn get(&self) -> BehaviourFunctionsReadOnlyView<ID, T, FnType> {
51 self.into()
52 }
53}
54
55pub struct BehaviourFunctionsReadOnlyView<ID: Clone, T: ReactiveInstance<ID>, FnType: Clone>(
56 ReadOnlyView<BehaviourTypeId, FnType>,
57 BehaviourFactoryCreator<ID, T, FnType>,
58);
59
60impl<ID: Clone, T: ReactiveInstance<ID>, FnType: Clone> BehaviourFunctionsReadOnlyView<ID, T, FnType> {
61 pub fn to_component_behaviour_tys(&self) -> ComponentBehaviourTypeIds {
62 self.keys().map(ComponentBehaviourTypeId::from).collect()
63 }
64
65 pub fn to_entity_behaviour_tys(&self) -> EntityBehaviourTypeIds {
66 self.keys().map(EntityBehaviourTypeId::from).collect()
67 }
68
69 pub fn to_relation_behaviour_tys(&self) -> RelationBehaviourTypeIds {
70 self.keys().map(RelationBehaviourTypeId::from).collect()
71 }
72
73 pub fn get_factories(&self) -> BehaviourFactories<ID, T> {
74 self.0
75 .iter()
76 .fold(BehaviourFactories::new(), |factories, (ty, f)| factories.factory(self.1(ty, f.clone())))
77 }
78}
79
80impl<ID: Clone, T: ReactiveInstance<ID>, FnType: Clone> Deref for BehaviourFunctionsReadOnlyView<ID, T, FnType> {
81 type Target = ReadOnlyView<BehaviourTypeId, FnType>;
82
83 fn deref(&self) -> &Self::Target {
84 &self.0
85 }
86}
87
88impl<ID: Clone, T: ReactiveInstance<ID>, FnType: Clone> From<&BehaviourFunctions<ID, T, FnType>> for BehaviourFunctionsReadOnlyView<ID, T, FnType> {
89 fn from(bf: &BehaviourFunctions<ID, T, FnType>) -> Self {
90 Self(bf.0.clone().into_read_only(), bf.1)
91 }
92}
93
94#[macro_export]
95macro_rules! entity_behaviour_functions {
96 ($ident: ident, $fn_type: ty, $constructor: expr) => {
97 pub static $ident: std::sync::LazyLock<$crate::BehaviourFunctionsReadOnlyView<Uuid, reactive_graph_reactive_model_impl::ReactiveEntity, $fn_type>> =
98 std::sync::LazyLock::new($constructor);
99 };
100}
101
102#[macro_export]
103macro_rules! relation_behaviour_functions {
104 ($ident: ident, $fn_type: ty, $constructor: expr) => {
105 pub static $ident: std::sync::LazyLock<
106 $crate::BehaviourFunctionsReadOnlyView<reactive_graph_graph::RelationTypeId, reactive_graph_reactive_model_impl::ReactiveRelation, $fn_type>,
107 > = std::sync::LazyLock::new($constructor);
108 };
109}