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

How to callback as event handler multiple times from multiple threads? #1009

Closed
sonewman opened this issue Nov 19, 2023 · 2 comments
Closed

Comments

@sonewman
Copy link

Hi, Forgive me if I’ve missed something, but I can’t seem to work out how it would be possible call a js callback from rust multiple times from multiple threads. Picture a scenario such as calling an event handler for a stream of events.

Obviously I understand that the is runtime itself has single threaded isolation for the most part so and so the use of cx.channel() to synchronise an async event from another thread makes sense.

However I can’t seem to work out how (assuming it’s possible) to clone argument handles in a separate thread because cx is obviously not Sync.

The only way I can think of doing what I’m trying to do (without using unsafe pointers - which might be my only other option, or making the callback global - which is not ideal or manageable) would be keep attaching a new listener from js each time the callback is called, but this seems inefficient as I would also need to manage a queue of events which the cx channel would otherwise be able to handle.

Please let me know if maybe I’ve missed something in the api docs or if you need more context to clarify anything

@kjvalencik
Copy link
Member

Use an Arc to clone (Arc<Root<JsFunction>>) and use Root::to_inner to extract (instead of into_inner).

@sonewman
Copy link
Author

sonewman commented Nov 21, 2023

Ooo, ok. Thanks, I will give that a try 😅

It seems kind of obvious now, not sure why i didn't think of it before. But thank you so much for responding and helping me, this works perfectly 😄

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

No branches or pull requests

2 participants