Skip to content

Any plans to expand the scope to the tauri event system? #180

@Zercerium

Description

@Zercerium
Contributor

imo it would be nice to get also the event system typed. Any plans or ideas for that?

At the moment a possible solution is to use specta to create at least the typescript types automatically. But you can still misspell the event identifiers. I also rly like the idea of the .wit files.

Thanks for your great work!

Activity

JonasKruckenberg

JonasKruckenberg commented on Sep 10, 2023

@JonasKruckenberg
Member

Yeah so the way I plan to bring this to tauri-bindgen is through something completely different: streams.
This is essentially the same as in GRPC if you're familiar with that, if not here's the gist:

interface log {
   func start_logging() -> stream<string>
}

which would translate to something like this for the host

trait Log {
   type StartLoggingStream = Pin<Box<dyn Stream<Item = String> + Send  + 'static>>;

   fn start_logging(&self) -> Result<Self:: StartLoggingStream>
}

Similar to how the tonic crate handles this.

On the guest side this would map to AsyncIterator for JS and SomeSpecificStreamType<String> for Rust probably.

So no events in the classical tauri sense (imo superior though bc it forces cleaner code) but I hope it can fulfill all the use cases.

Edit: If you want a similar behavior to events right now under this stream model you'd use an tokio::sync::mpsc channel like so:

struct LogData {
   rx: mpsc::Receiver<String>
}

impl Log for LogData {
   type StartLoggingStream = Pin<Box<tokio_stream::wrappers::ReceiverStream<String>>>;

   fn start_logging(&self) -> Result<Self:: StartLoggingStream> {
      let rx = todo!(); // haven't figured out how to do this yet

      let stream = tokio_stream::wrappers::ReceiverStream::new(rx);

      Ok(Box::pin(stream))
   }
}

And now you can use the mpsc::Sender handles across the codebase like you'd call win.emit() (but with more safety)

linked a pull request that will close this issue on Sep 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @JonasKruckenberg@Zercerium

      Issue actions

        Any plans to expand the scope to the tauri event system? · Issue #180 · tauri-apps/tauri-bindgen