firebase_rs_sdk/component/
component.rs1use std::collections::HashMap;
2use std::sync::Arc;
3
4use serde_json::{Map, Value};
5
6use crate::component::types::{
7 ComponentType, InstanceFactory, InstantiationMode, OnInstanceCreatedCallback,
8};
9
10#[derive(Clone)]
11pub struct Component {
12 name: Arc<str>,
13 pub(crate) instance_factory: InstanceFactory,
14 pub(crate) ty: ComponentType,
15 pub(crate) instantiation_mode: InstantiationMode,
16 pub(crate) multiple_instances: bool,
17 pub(crate) service_props: Map<String, Value>,
18 pub(crate) on_instance_created: Option<OnInstanceCreatedCallback>,
19}
20
21impl Component {
22 pub fn new(
23 name: impl Into<String>,
24 instance_factory: InstanceFactory,
25 ty: ComponentType,
26 ) -> Self {
27 Self {
28 name: Arc::from(name.into()),
29 instance_factory,
30 ty,
31 instantiation_mode: InstantiationMode::Lazy,
32 multiple_instances: false,
33 service_props: Map::new(),
34 on_instance_created: None,
35 }
36 }
37
38 pub fn name(&self) -> &str {
39 &self.name
40 }
41
42 pub fn component_type(&self) -> ComponentType {
43 self.ty
44 }
45
46 pub fn instantiation_mode(&self) -> InstantiationMode {
47 self.instantiation_mode
48 }
49
50 pub fn multiple_instances(&self) -> bool {
51 self.multiple_instances
52 }
53
54 pub fn service_props(&self) -> &Map<String, Value> {
55 &self.service_props
56 }
57
58 pub fn on_instance_created(&self) -> Option<&OnInstanceCreatedCallback> {
59 self.on_instance_created.as_ref()
60 }
61
62 pub fn with_instantiation_mode(mut self, mode: InstantiationMode) -> Self {
63 self.instantiation_mode = mode;
64 self
65 }
66
67 pub fn with_multiple_instances(mut self, multiple: bool) -> Self {
68 self.multiple_instances = multiple;
69 self
70 }
71
72 pub fn with_service_props(mut self, props: HashMap<String, Value>) -> Self {
73 self.service_props = props.into_iter().collect();
74 self
75 }
76
77 pub fn with_instance_created_callback<F>(mut self, callback: F) -> Self
78 where
79 F: Fn(
80 &crate::component::container::ComponentContainer,
81 &str,
82 &crate::component::types::DynService,
83 ) + Send
84 + Sync
85 + 'static,
86 {
87 self.on_instance_created = Some(Arc::new(callback));
88 self
89 }
90}