-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to send an Event outside of the main thread #23
Comments
Recapping discussions on Rust Audio Discord:
The poll events can be done manually for now, but ultimately the ability to submit async tasks seems useful overall: state.spawn(|handle| async move {
let timer = ...;
timer.await;
handle.insert_event(Event::new(TickEvent::new()).direct(entity));
}); Needs some design work though, and unclear how an async executor can be integrated with the main event loop. EDIT: Most likely, for now, we want the async executor to run on a separate thread. This way the user can set up an executor of their choice. For example: // Use a multi-threaded executor. If a single-threaded one is desired (which - it probably is),
// it needs to be started on a separate thread.
// We can also of course provide feature-gated helpers for spinning up some executor.
#[tokio::main]
fn main() {
let app = Application::new(|state, window| {
// Invent a better name for this. The purpose of this "handle" is only to be able to
// send events to the main event loop.
// Internally clones either an EventLoopProxy or an MPSC producer handle.
let handle = state.get_async_handle();
tokio::spawn(async move {
let mut timer = time::interval(Duration::from_secs(5));
loop {
timer.tick().await;
handle.insert_event(Event::new(TickEvent::new()).propagate(Propagation::All));
}
});
});
// Run the actual app synchronously. This is why we need a multi-threaded or separate executor.
app.run();
} |
So my thinking for a solution to this would be to add timers. In the future I think it would be great to have some way to send events from another thread or to use async, but for the time being my suggested stopgap solution would be something like |
Currently, sending an event requires a
&mut State
, which can't be obtained outside of event handlers. This makes it difficult to write any code that requires external triggering. A minimal example would be a timer app - an external time-keeping thread can't trigger a redraw, so the displayed time can't be updated.The workaround I'm currently using is to push a
WindowEvent::Redraw
during theon_draw
handler, which will initiate a manual redraw right after the last one is finished. This isn't ideal though - a lot of the renders are redundant.The text was updated successfully, but these errors were encountered: