use libhaystack::val::Value;
use crate::{
base::{
Status,
input::InputProps,
link::BaseLink,
output::{BaseOutput, Output},
},
tokio_impl::WriterImpl,
};
use super::input::InputImpl;
pub type LinkImpl = BaseLink<WriterImpl>;
pub type OutputImpl = BaseOutput<LinkImpl>;
impl Output for OutputImpl {
type Writer = <InputImpl as InputProps>::Writer;
fn add_link(&mut self, link: BaseLink<Self::Writer>) {
self.links.push(link);
}
fn set(&mut self, value: Value) {
let status = self.effective_status;
for link in &mut self.links {
if let Some(tx) = &link.tx {
tx.send_if_modified(|current| {
if current.0 != value || current.1 != status {
current.0 = value.clone();
current.1 = status;
true
} else {
false
}
});
}
}
self.value = value;
}
fn emit_status(&mut self, status: Status) {
self.effective_status = status;
let value = self.value.clone();
for link in &mut self.links {
if let Some(tx) = &link.tx {
tx.send_if_modified(|current| {
if current.0 != value || current.1 != status {
current.0 = value.clone();
current.1 = status;
true
} else {
false
}
});
}
}
}
}