use log::{info, trace};
use crate::block::{Block, BlockRet};
use crate::graph::{CancellationToken, GraphRunner};
#[derive(Default)]
pub struct WasmGraph {
blocks: Vec<Box<dyn Block>>,
}
impl WasmGraph {
pub fn new() -> Self {
Self::default()
}
pub async fn run_async(&mut self, rx: async_channel::Receiver<()>) -> crate::Result<()> {
let mut eof = vec![false; self.blocks.len()];
let rx = Box::pin(rx);
loop {
let mut done = true;
let mut need_more = false;
for (n, b) in self.blocks.iter_mut().enumerate() {
let name = b.block_name().to_owned();
trace!("Running graph node {name}");
if eof[n] {
continue;
}
let ret = b.work()?;
trace!("graph node {name} work ended");
match ret {
BlockRet::EOF => {
eof[n] = true;
info!("Block({name}): EOF");
}
BlockRet::Again => done = false,
BlockRet::WaitForStream(s, _) => {
let closed = s.closed();
if b.eof() && closed {
eof[n] = true;
}
}
BlockRet::Pending => {
need_more = true;
done = false;
}
}
}
if done {
info!("Wasm graph: All done");
return Ok(());
}
if need_more {
if let Err(e) = rx.recv().await {
info!("Graph: recv error: {e:?}");
return Err(crate::Error::msg("recv()"));
}
}
}
}
}
impl GraphRunner for WasmGraph {
fn add(&mut self, b: Box<dyn Block + Send>) {
self.blocks.push(b);
}
fn run(&mut self) -> crate::Result<()> {
todo!()
}
fn generate_stats(&self) -> Option<String> {
None
}
fn cancel_token(&self) -> CancellationToken {
todo!()
}
}