neon-bindings/neon

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

Closed this issue · 2 comments

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

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

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 😄