use std::path::Path;
use serde_json::Value;
use crate::state::{Link, Said};
use crate::ui::Channel;
pub fn tick(link: &Link, root: &Path) {
let standing = link.standing();
for channel in &standing.channels {
down(link, root, channel, &crate::verbs::workspaces());
}
let Some((channel, aim)) = standing.aimed() else {
return;
};
aimed(
link,
root,
&channel,
&crate::verbs::conversations(aim.address.clone()),
);
if standing.tuning {
aimed(
link,
root,
&channel,
&crate::verbs::roles(aim.address.clone()),
);
}
let Some(conversation) = standing.conversation else {
return;
};
aimed(
link,
root,
&channel,
&crate::verbs::transcript(aim.address, conversation),
);
}
fn down(link: &Link, root: &Path, channel: &Channel, envelope: &Value) {
match crate::seat::dial(root, &channel.name).and_then(|open| open.ask(envelope)) {
Ok(stream) => file(link, channel, stream),
Err(why) => link.heard(channel, Said::Unreachable(why)),
}
}
fn aimed(link: &Link, root: &Path, channel: &Channel, envelope: &Value) {
match crate::seat::route(root, envelope).and_then(|(open, carried)| open.ask(&carried)) {
Ok(stream) => file(link, channel, stream),
Err(why) => link.heard(channel, Said::Unreachable(why)),
}
}
fn file(link: &Link, channel: &Channel, stream: Vec<Value>) {
for frame in stream {
link.heard(channel, Said::Frame(frame));
}
}
#[cfg(test)]
mod tests;