Skip to main content

mittens_engine/engine/ecs/component/
http_client.rs

1use super::Component;
2use crate::engine::ecs::component::ce_helpers::*;
3use crate::engine::ecs::{ComponentId, IntentValue, SignalEmitter};
4
5#[derive(Debug, Clone, Default)]
6pub struct HttpClientComponent {
7    pub enabled: bool,
8    pub timeout_ms: Option<u64>,
9    component: Option<ComponentId>,
10}
11
12impl HttpClientComponent {
13    pub fn new() -> Self {
14        Self {
15            enabled: true,
16            timeout_ms: None,
17            component: None,
18        }
19    }
20
21    pub fn with_enabled(mut self, enabled: bool) -> Self {
22        self.enabled = enabled;
23        self
24    }
25
26    pub fn with_timeout_ms(mut self, timeout_ms: u64) -> Self {
27        self.timeout_ms = Some(timeout_ms);
28        self
29    }
30}
31
32impl Component for HttpClientComponent {
33    fn name(&self) -> &'static str {
34        "http_client"
35    }
36
37    fn set_id(&mut self, component: ComponentId) {
38        self.component = Some(component);
39    }
40
41    fn as_any(&self) -> &dyn std::any::Any {
42        self
43    }
44
45    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
46        self
47    }
48
49    fn init(&mut self, emit: &mut dyn SignalEmitter, component: ComponentId) {
50        emit.push_intent_now(
51            component,
52            IntentValue::RegisterHttpClient {
53                component_ids: vec![component],
54            },
55        );
56    }
57
58    fn to_mms_ast(
59        &self,
60        _world: &crate::engine::ecs::World,
61    ) -> crate::scripting::ast::ComponentExpression {
62        let mut ce = ce("HttpClient");
63        if !self.enabled {
64            ce = ce.with_call("enabled", vec![b(false)]);
65        }
66        if let Some(timeout_ms) = self.timeout_ms {
67            ce = ce.with_call("timeout_ms", vec![num(timeout_ms as f64)]);
68        }
69        ce
70    }
71}