use super::simulation::SimulationThread;
use crate::wiring::step::SimulationStepError;
use bevy::prelude::{Commands, Event, NonSend};
use std::sync::mpsc;
#[derive(Event, Debug, Clone)]
pub struct SimulationErrorEvent {
pub thread: SimulationThread,
pub error: SimulationStepError,
}
#[derive(bevy::prelude::Resource, Clone)]
pub struct SimulationErrorSender(pub(crate) mpsc::SyncSender<SimulationErrorEvent>);
pub struct SimulationErrorReceiver(pub(crate) mpsc::Receiver<SimulationErrorEvent>);
pub const ERROR_CHANNEL_CAPACITY: usize = 64;
pub(crate) fn error_channel() -> (SimulationErrorSender, SimulationErrorReceiver) {
let (sender, receiver) = mpsc::sync_channel(ERROR_CHANNEL_CAPACITY);
(
SimulationErrorSender(sender),
SimulationErrorReceiver(receiver),
)
}
pub(crate) fn propagate_simulation_errors(
receiver: NonSend<SimulationErrorReceiver>,
mut commands: Commands,
) {
while let Ok(event) = receiver.0.try_recv() {
commands.trigger(event);
}
}