Access thread (specifically a Sender object) from different functions

Ok, so to begin with I'm relatively new to rust / learning it currently. But with a lot of experience with other languages. So whilst in other languages I would probably use a global variable for this, in rust this would require an unsafe marker and I want to try to avoid this. So I wanted to ask what would be the solution the rust way.

I am working on a little Tauri-App, that plays audio (main objective is to learn more rust and tauri) via a rodio::Sink. To do that, a button opens a file selector dialog and triggers a function that opens a Sink in a separate Thread and plays the audio from the file (separate thread so that Tauri GUI doesn't freeze up)

#[tauri::command]
fn select_file(path: &str) {
    let s_path = path.to_string();
    thread::spawn(move || {
        let (_stream, stream_handle) = OutputStream::try_default().unwrap();
        let sink = rodio::Sink::try_new(&stream_handle).unwrap();
        utils::play(&s_path.clone(),&sink);
        sink.sleep_until_end();
    });
}

To note here: the utils::play() is my function that plays the audio
btw. up to that point everything works.

Now to my main Question:
In the utils::play function I will put a Receiver that, if sent a certain value, pauses or resumes the audio playback. The Receiver could be put in that thread at the spawn point, but how can I store the Sender to access it from a separate tauri command function.
In other languages I would've probably used a global variable, but I'm not sure if this is really the right way to do this in rust, and if declaring a public channel (or just public sender is such a good option).
So is it acceptable in your eyes to use a public Sender / Channel or is there some sort of way to do this smarter, that I'm just completely missing.
Thanks in advance

In general (I don't know Tauri), it is good and common to use a channel to send an action/request from one thread to another, and this is much better than using a global.

I don't know tauri, but it would be worth looking into if it has some utility for managing state. I think this might be what you need State Management | Tauri

yep, exactly what I was missing. Thanks a lot

I know that, but I needed to share the channel between to functions (the state thing mentioned in the other reply was what I meant)

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.