gled 2.32.0

gled is an application for creating animations and effects on artnet or dmx installations
//! The desktop app's link to the engine in the same process: the same
//! messages a remote interface gets, without a network in between.

use gled_core::engine::{ClientConnection, ClientInput, Outbox};
use gled_protocol::message::{ClientMessage, ServerMessage};
use gled_ui::link::EngineLink;

#[derive(Debug)]
pub struct InProcessLink {
    connection: ClientConnection,
    outbox: Outbox,
}

impl InProcessLink {
    pub fn new(connection: ClientConnection) -> Self {
        Self {
            connection,
            outbox: Outbox::default(),
        }
    }
}

impl EngineLink for InProcessLink {
    fn send(&mut self, message: ClientMessage) {
        match message {
            ClientMessage::Input(ClientInput::Action { sequence, action }) => {
                self.connection.send_numbered_action(sequence, action);
            }
            ClientMessage::Input(input) => self.connection.send(input),
            ClientMessage::Subscribe(subscriptions) => self.outbox.subscribe(subscriptions),
            ClientMessage::Hello { .. } | ClientMessage::Ping(_) => {}
        }
    }

    fn receive(&mut self) -> Vec<ServerMessage> {
        let mut messages = match self.connection.latest_state() {
            Some(state) => self.outbox.messages(state),
            None => Vec::new(),
        };
        while let Some(event) = self.connection.next_event() {
            messages.push(ServerMessage::Event(event));
        }
        messages
    }
}