nodejs 0.2.1

Embedding Node.js in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
use nodejs::neon::context::TaskContext;
use nodejs::neon::result::NeonResult;

pub fn sync_node<T: Send + 'static>(
    f: impl FnOnce(TaskContext) -> NeonResult<T> + Send + 'static,
) -> Option<T> {
    let nodejs_channel = nodejs::channel();
    let (tx, rx) = std::sync::mpsc::sync_channel::<T>(0);
    nodejs_channel.send(move |cx| {
        let val = f(cx)?;
        tx.send(val).unwrap();
        Ok(())
    });
    rx.recv().ok()
}