Skip to content

Ability to call Javascript methods from Rust threads #197

@jugglingcats

Description

@jugglingcats

As discussed on Slack: https://rust-bindings.slack.com/archives/neon/p1489508576491688.

The basic idea is that a Rust addon can create a continuous background thread that notifies Javascript when things happen (event style).

Psuedo Javascript:

addin.run(function() { 
  console.log("got the event!")
}

Psuedo Rust:

fn run(call: Call) -> JsResult<JsString> {
    let scope = call.scope;
    let f = call.arguments.require(scope, 0)?.check::<JsFunction>()?;

    thread::spawn(|| {
        while (true) {
            let args: Vec<Handle<JsNumber>> = vec![];
            match f.call(scope, JsNull::new(), args) {
                _ => { println!("Call to javascript done!"); }
            }
            thread::sleep(1000);
        }
    });
    Ok(JsString::new(scope, "hello node").unwrap())
}

If this was achievable it would be a lot nicer than the C equivalent, an example of which is here: https://gist.github.com/matzoe/11082417.

I imagine there would need to be some kind of wrapper around the call, so maybe not completely transparent as shown above.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions