use koda_core::engine::{EngineEvent, EngineSink};
pub enum UiEvent {
Engine(EngineEvent),
}
pub struct CliSink {
ui_tx: tokio::sync::mpsc::UnboundedSender<UiEvent>,
}
impl CliSink {
pub fn channel(ui_tx: tokio::sync::mpsc::UnboundedSender<UiEvent>) -> Self {
Self { ui_tx }
}
}
impl EngineSink for CliSink {
fn emit(&self, event: EngineEvent) {
if let Err(e) = self.ui_tx.send(UiEvent::Engine(event)) {
tracing::warn!("UI channel closed, event lost: {e}");
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn emit_forwards_to_channel() {
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
let sink = CliSink::channel(tx);
sink.emit(EngineEvent::SpinnerStop);
let msg = rx.try_recv().expect("should receive event");
assert!(matches!(msg, UiEvent::Engine(EngineEvent::SpinnerStop)));
}
#[test]
fn emit_does_not_panic_on_closed_channel() {
let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
let sink = CliSink::channel(tx);
drop(rx); sink.emit(EngineEvent::SpinnerStop);
}
}