node-bindgen
node-bindgen copied to clipboard
Return functions
Any way to do this
addon.foo((string)=>{
string = "slkdfjsf";
return string;
})
or this
addon.foo((func)=>{
func("slkdfjsf")
})
not yet, is something you need?
Yes this would be needed for porting over a http framework im working on.
This is what im looking to write.
request.get('/test/:foo', (req, res)=>{
res({
status:200,
body: 'hello'
})
// or this
res.send('hello');
})
Hmm, in this case rust fn would something like this:
struct Response {
....
}
struct Request {
....
}
impl Request {
#[node_bindgen]
async fn get<F,T,R>(path: String, handler: F)
where F: FnOnce<Req,Response> -> T,
T: Future<Output = Response> + 'static + Send
{
let req = // create req
let resp = // create response
let response = handler(req,resp).await; // wait JS to return invoke response
...do something with response, probably return to client
}
}
Tricky part is how to map Response which is actually function call from JS point of view.
It would be be easier instead of callback, pass async fn.