use std::rc::Rc;
use crate::events::PipelineEvent;
use crate::progress::{LiveProgress, ProgressReporter};
use super::PipelineContext;
const LIVE_PROGRESS_INTERVAL_MS: u64 = 100;
pub(super) fn build_node_reporter<F: Fn() -> u64 + Copy>(
ctx: &PipelineContext<F>,
node_id: &str,
) -> ProgressReporter {
let (live, output) = (ctx.reporter.clone(), ctx.reporter.clone());
let (live_id, output_id) = (node_id.to_string(), node_id.to_string());
let clock = Rc::clone(&ctx.live_clock);
ProgressReporter::with_live(
move |progress, message| live.emit(node_progress(&live_id, progress, message)),
move |line| {
output.emit(PipelineEvent::CommandOutput {
node_id: output_id.clone(),
line: line.to_string(),
});
},
)
.throttled(move || clock(), LIVE_PROGRESS_INTERVAL_MS)
}
fn node_progress(node_id: &str, progress: LiveProgress, message: &str) -> PipelineEvent {
let (done, total) = progress.done_and_total();
PipelineEvent::NodeProgress {
node_id: node_id.to_string(),
done,
total,
message: message.to_string(),
}
}