use crate::runtime;
use crate::runtime::BlockDescription;
use crate::runtime::BlockId;
use crate::runtime::Error;
use crate::runtime::Flowgraph;
use crate::runtime::FlowgraphDescription;
use crate::runtime::FlowgraphHandle;
use crate::runtime::FlowgraphTask;
use crate::runtime::Pmt;
use crate::runtime::Result;
pub struct RunningFlowgraph {
handle: FlowgraphHandle,
task: FlowgraphTask,
}
impl RunningFlowgraph {
pub(crate) fn new(handle: FlowgraphHandle, task: FlowgraphTask) -> Self {
Self { handle, task }
}
pub fn handle(&self) -> FlowgraphHandle {
self.handle.clone()
}
pub fn block(&self, block_id: impl Into<BlockId>) -> runtime::FlowgraphBlockHandle {
self.handle.block(block_id)
}
pub fn split(self) -> (FlowgraphTask, FlowgraphHandle) {
(self.task, self.handle)
}
pub async fn wait_async(self) -> Result<Flowgraph, Error> {
self.task.await
}
#[cfg(not(target_arch = "wasm32"))]
pub fn wait(self) -> Result<Flowgraph, Error> {
async_io::block_on(self.wait_async())
}
pub async fn post(
&self,
block_id: impl Into<BlockId>,
port_id: impl Into<crate::runtime::PortId>,
data: Pmt,
) -> Result<(), Error> {
self.handle.post(block_id, port_id, data).await
}
pub async fn call(
&self,
block_id: impl Into<BlockId>,
port_id: impl Into<crate::runtime::PortId>,
data: Pmt,
) -> Result<Pmt, Error> {
self.handle.call(block_id, port_id, data).await
}
pub async fn describe(&self) -> Result<FlowgraphDescription, Error> {
self.handle.describe().await
}
pub async fn describe_block(
&self,
block_id: impl Into<BlockId>,
) -> Result<BlockDescription, Error> {
self.handle.describe_block(block_id).await
}
pub async fn stop(&self) -> Result<(), Error> {
self.handle.stop().await
}
pub async fn stop_and_wait(self) -> Result<Flowgraph, Error> {
self.handle.stop().await?;
self.wait_async().await
}
}