#How to use Channel for two-way communication

3 messages · Page 1 of 1 (latest)

zenith remnant
#

I want the front end to use the returned Channel to stop the download, but there is no send method on the Channel.

#[tauri::command]
pub async fn download_multi(
    options: DownloadOptions,
    tx: Channel<Event>,
) -> Result<Channel<()>, Error> {
    // Downloading ...
    let abort_handle = handle.abort_handle();
    let stop_channel: Channel<()> = Channel::new(move |_| {
        println!("stop download");
        abort_handle.abort();
        Ok(())
    });
    Ok(stop_channel)
}
declare class Channel<T = unknown> {
    #private;
    /** The callback id returned from {@linkcode transformCallback} */
    id: number;
    constructor(onmessage?: (response: T) => void);
    private cleanupCallback;
    set onmessage(handler: (response: T) => void);
    get onmessage(): (response: T) => void;
    [SERIALIZE_TO_IPC_FN](): string;
    toJSON(): string;
}
stiff marsh
#

Channels should be created on the front-end and sent to the back-end as part of the invoke.