r3bl-org/r3bl-open-core

[tui] Clean up the calls to `tokio::spawn!(sender.send(...))`

Closed this issue · 0 comments

Current behavior

In all the examples in the r3bl_tui crate in the tui folder, the same type of calls are made to spawn a tokio task to send an app signal over an async mpsc channel.

Here's an example:

https://github.com/r3bl-org/r3bl-open-core/blob/main/tui/examples/demo/ex_app_no_layout/app_main.rs#L56

// Send a signal to the main thread to render.
let main_thread_channel_sender_clone = main_thread_channel_sender.clone();
// Note: make sure to wrap the call to `send` in a `tokio::spawn()` so
// that it doesn't block the calling thread. More info:
// <https://tokio.rs/tokio/tutorial/channels>.
tokio::spawn(async move {
    let _ = main_thread_channel_sender_clone
    .send(TerminalWindowMainThreadSignal::ApplyAction(AppSignal::Add))
    .await;
});

Almost exactly this code is repeated many times in the codebase.

The only thing that might change is the ApplyAction argument that is passed in. And there are simple ways to use a generic w/ a trait bound to a re-usable function that takes care of spawning this task and sending the signal to the sender.

Desired behavior

Create a declarative macro that makes this super easy to send a signal to an app. The 2 things to pass to this macro are:

  1. The &mut global_data reference which can be used to get a clone of the sender
  2. The signal that needs to be sent to the main thread (which will be dispatched to the app).