winit/src/platform_impl/web/event_loop/proxy.rs

40 lines
1,022 B
Rust
Raw Normal View History

use std::sync::mpsc::Sender;
2019-06-25 03:15:34 +02:00
use super::runner;
use crate::event::Event;
use crate::event_loop::EventLoopClosed;
use crate::platform_impl::platform::r#async::Channel;
2019-06-25 03:15:34 +02:00
pub struct EventLoopProxy<T: 'static> {
// used to wake the event loop handler, not to actually pass data
runner: Channel<runner::Shared, ()>,
sender: Sender<T>,
2019-06-25 03:15:34 +02:00
}
impl<T: 'static> EventLoopProxy<T> {
pub fn new(runner: runner::Shared, sender: Sender<T>) -> Self {
Self {
runner: Channel::new(runner, |runner, event| {
runner.send_event(Event::UserEvent(event))
})
.unwrap(),
sender,
}
2019-06-25 03:15:34 +02:00
}
pub fn send_event(&self, event: T) -> Result<(), EventLoopClosed<T>> {
self.sender.send(event).unwrap();
self.runner.send(());
2019-06-25 03:15:34 +02:00
Ok(())
}
}
2019-09-24 19:39:13 -04:00
impl<T: 'static> Clone for EventLoopProxy<T> {
2019-09-24 19:39:13 -04:00
fn clone(&self) -> Self {
Self {
runner: self.runner.clone(),
sender: self.sender.clone(),
2019-09-24 19:39:13 -04:00
}
}
}