node-bindgen icon indicating copy to clipboard operation
node-bindgen copied to clipboard

Return functions

Open Phara0h opened this issue 4 years ago • 4 comments

Any way to do this

addon.foo((string)=>{
  string = "slkdfjsf";
  return string;
})

or this

addon.foo((func)=>{
  func("slkdfjsf")
})

Phara0h avatar May 27 '20 23:05 Phara0h

not yet, is something you need?

sehz avatar May 28 '20 00:05 sehz

Yes this would be needed for porting over a http framework im working on.

Phara0h avatar Jun 02 '20 21:06 Phara0h

This is what im looking to write.

request.get('/test/:foo', (req, res)=>{
  
  res({
     status:200,
    body: 'hello'
  })
// or this

res.send('hello');
})

Phara0h avatar Jun 02 '20 21:06 Phara0h

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.

sehz avatar Jun 02 '20 21:06 sehz