Skip to content
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

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

Open
Zercerium opened this issue Aug 25, 2023 · 1 comment · May be fixed by #190
Open

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

Zercerium opened this issue Aug 25, 2023 · 1 comment · May be fixed by #190

Comments

@Zercerium
Copy link
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!

@JonasKruckenberg
Copy link
Contributor

JonasKruckenberg commented Sep 10, 2023

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)

@JonasKruckenberg JonasKruckenberg linked a pull request Sep 10, 2023 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants