neon-bindings / neon

Rust bindings for writing safe and fast native Node.js modules.
https://www.neon-bindings.com/
Apache License 2.0
8k stars 283 forks source link

How to grab a context from another thread? #974

Closed victorteokw closed 1 year ago

victorteokw commented 1 year ago

Thanks for this great binding package! 👏

I have some rust code to save async callbacks into hashmaps. When some rust code is running, these callbacks are fetched and executed. However, I cannot get a context to execute JS code that passed in.

Is there any way to grab a context when there aren't contexts from the parameters?

kjvalencik commented 1 year ago

It's not possible to get a Context on another thread because it represents access to the single-threaded VM. However, you can use Channel to schedule a callback to be executed on the main thread.

Documentation on this is in the events module.

This example demonstrates using Channel along with the tokio runtime for async handling.

victorteokw commented 1 year ago

Thanks