use crate::runtime::BlockDescription;
use crate::runtime::BlockId;
use crate::runtime::Error;
use crate::runtime::FlowgraphBlockHandle;
use crate::runtime::FlowgraphDescription;
use crate::runtime::FlowgraphHandle;
use crate::runtime::FlowgraphId;
use crate::runtime::FlowgraphTask;
use crate::runtime::Pmt;
use crate::runtime::PortId;
use crate::runtime::PortIndex;
use crate::runtime::PortName;
use crate::runtime::Result;
use crate::runtime::TerminatedFlowgraph;
#[cfg(not(target_arch = "wasm32"))]
use crate::runtime::block_on;
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 id(&self) -> FlowgraphId {
self.handle.id()
}
pub fn block(&self, block_id: impl Into<BlockId>) -> FlowgraphBlockHandle {
self.handle.block(block_id)
}
pub fn split(self) -> (FlowgraphTask, FlowgraphHandle) {
(self.task, self.handle)
}
pub async fn wait_async(self) -> Result<TerminatedFlowgraph, Error> {
self.task.await
}
#[cfg(not(target_arch = "wasm32"))]
pub fn wait(self) -> Result<TerminatedFlowgraph, Error> {
block_on(self.wait_async())
}
pub fn message_input_id(
&self,
block_id: impl Into<BlockId>,
name: impl Into<PortName>,
) -> Result<PortIndex, Error> {
self.handle.message_input_id(block_id, name)
}
pub async fn post(
&self,
block_id: impl Into<BlockId>,
port_id: impl Into<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<PortId>,
data: Pmt,
) -> Result<Pmt, Error> {
self.handle.call(block_id, port_id, data).await
}
pub fn describe(&self) -> Result<FlowgraphDescription, Error> {
self.handle.describe()
}
pub fn describe_block(&self, block_id: impl Into<BlockId>) -> Result<BlockDescription, Error> {
self.handle.describe_block(block_id)
}
pub async fn stop(&self) -> Result<(), Error> {
self.handle.stop().await
}
pub async fn stop_and_wait(self) -> Result<TerminatedFlowgraph, Error> {
match self.handle.stop().await {
Ok(()) | Err(Error::FlowgraphTerminated) => self.wait_async().await,
Err(e) => Err(e),
}
}
}