2023-10-16 15:50:22 +02:00
|
|
|
use std::rc::Weak;
|
|
|
|
|
use std::sync::mpsc::{SendError, Sender};
|
2023-09-03 02:26:53 +02:00
|
|
|
|
2023-10-16 15:50:22 +02:00
|
|
|
use super::runner::Execution;
|
2019-06-25 03:15:34 +02:00
|
|
|
use crate::event_loop::EventLoopClosed;
|
2023-10-16 15:50:22 +02:00
|
|
|
use crate::platform_impl::platform::r#async::Waker;
|
2019-06-25 03:15:34 +02:00
|
|
|
|
2022-03-18 14:09:39 +01:00
|
|
|
pub struct EventLoopProxy<T: 'static> {
|
2023-10-16 15:50:22 +02:00
|
|
|
runner: Waker<Weak<Execution>>,
|
2023-09-03 02:26:53 +02:00
|
|
|
sender: Sender<T>,
|
2019-06-25 03:15:34 +02:00
|
|
|
}
|
|
|
|
|
|
2022-03-18 14:09:39 +01:00
|
|
|
impl<T: 'static> EventLoopProxy<T> {
|
2023-10-16 15:50:22 +02:00
|
|
|
pub fn new(runner: Waker<Weak<Execution>>, sender: Sender<T>) -> Self {
|
|
|
|
|
Self { runner, sender }
|
2019-06-25 03:15:34 +02:00
|
|
|
}
|
|
|
|
|
|
2019-12-07 18:22:03 +01:00
|
|
|
pub fn send_event(&self, event: T) -> Result<(), EventLoopClosed<T>> {
|
2023-10-16 15:50:22 +02:00
|
|
|
self.sender
|
|
|
|
|
.send(event)
|
|
|
|
|
.map_err(|SendError(event)| EventLoopClosed(event))?;
|
|
|
|
|
self.runner.wake();
|
2019-06-25 03:15:34 +02:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-24 19:39:13 -04:00
|
|
|
|
2022-03-18 14:09:39 +01:00
|
|
|
impl<T: 'static> Clone for EventLoopProxy<T> {
|
2019-09-24 19:39:13 -04:00
|
|
|
fn clone(&self) -> Self {
|
2022-03-18 14:09:39 +01:00
|
|
|
Self {
|
2019-09-24 19:41:59 -04:00
|
|
|
runner: self.runner.clone(),
|
2023-09-03 02:26:53 +02:00
|
|
|
sender: self.sender.clone(),
|
2019-09-24 19:39:13 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|