Skip to main content

melodium_common/executive/
world.rs

1use crate::{
2    descriptor::Collection,
3    executive::{
4        Context, ContinuousFuture, Input, Level, Log, ModelId, Output, Outputs, TrackFuture, Value,
5    },
6};
7use async_trait::async_trait;
8use core::fmt::Debug;
9use std::{collections::HashMap, sync::Arc};
10use uuid::Uuid;
11
12pub type TrackId = usize;
13pub type TrackCreationCallback = Box<dyn FnOnce(Box<dyn Outputs>) -> Vec<TrackFuture> + Send>;
14pub type DirectCreationCallback = Box<
15    dyn FnOnce(
16            HashMap<String, Box<dyn Output>>,
17            HashMap<String, Box<dyn Input>>,
18        ) -> Vec<TrackFuture>
19        + Send,
20>;
21
22#[async_trait]
23pub trait World: Debug + Send + Sync {
24    fn collection(&self) -> Arc<Collection>;
25    fn add_continuous_task(&self, task: ContinuousFuture);
26    async fn create_track(
27        &self,
28        id: ModelId,
29        source: &str,
30        params: &HashMap<String, Value>,
31        contexts: Vec<Arc<dyn Context>>,
32        parent_track: Option<TrackId>,
33        callback: Option<TrackCreationCallback>,
34    );
35    async fn log(&self, level: Level, label: String, message: String, track_id: Option<TrackId>);
36    async fn inject_log(&self, log: Log) -> Result<(), ()>;
37    async fn inject_debug(&self, run_id: Uuid, data: String) -> Result<(), ()>;
38    /// Resolves once no track is running nor pending anymore (having run at
39    /// least one first), independently of whether the engine will actually
40    /// auto-end. Lets a continuous task that is itself waiting on some event
41    /// only fired in reaction to a track running (e.g. a model waiting to be
42    /// started by a treatment instance that may never run) detect that no
43    /// track will ever run again, instead of waiting forever or relying on
44    /// an arbitrary timeout.
45    async fn wait_no_more_tracks(&self);
46}