engine_mel/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![doc = include_str!("../README.md")]
3
4use melodium_core::common::executive::{Output, ResultStatus};
5use melodium_macro::{mel_model, mel_package};
6
7/// Provides interactions with Mélodium engine.
8///
9/// `ready` source is triggered at startup when engine is ready to process.
10#[mel_model(
11    source ready () (trigger Block<void>)
12    continuous (continuous)
13)]
14#[derive(Debug)]
15pub struct Engine {
16    model: std::sync::Weak<EngineModel>,
17}
18
19impl Engine {
20    fn new(model: std::sync::Weak<EngineModel>) -> Self {
21        Self { model }
22    }
23
24    async fn continuous(&self) {
25        let model = self.model.upgrade().unwrap();
26
27        model
28            .new_ready(
29                None,
30                Some(Box::new(|mut outputs| {
31                    let trigger = outputs.get("trigger");
32
33                    vec![Box::new(Box::pin(Self::ready(trigger)))]
34                })),
35            )
36            .await;
37    }
38
39    async fn ready(trigger: Box<dyn Output>) -> ResultStatus {
40        let _ = trigger.send_one_void(()).await;
41        trigger.close().await;
42        ResultStatus::Ok
43    }
44}
45
46mel_package!();