Wrap HttpResponseWrapper::onWritable

This commit is contained in:
Alex Hultman 2019-01-15 08:10:57 +01:00
parent 6a96e07c3c
commit d132ae2685
1 changed files with 18 additions and 1 deletions

View File

@ -13,7 +13,22 @@ struct HttpResponseWrapper {
// res.onData(JS function)
// res.onAborted
// res.onWritable
/* Takes function of bool(int), returns this */
template <bool SSL>
static void res_onWritable(const FunctionCallbackInfo<Value> &args) {
/* This thing perfectly fits in with unique_function, and will Reset on destructor */
UniquePersistent<Function> p(isolate, Local<Function>::Cast(args[0]));
getHttpResponse<SSL>(args)->onWritable([p = std::move(p)](int offset) {
HandleScope hs(isolate);
Local<Value> argv[] = {Integer::NewFromUnsigned(isolate, offset)};
return Local<Function>::New(isolate, p)->Call(isolate->GetCurrentContext()->Global(), 1, argv)->BooleanValue();
});
args.GetReturnValue().Set(args.Holder());
}
/* Takes string or arraybuffer, returns this */
template <bool SSL>
@ -85,6 +100,8 @@ struct HttpResponseWrapper {
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "write"), FunctionTemplate::New(isolate, res_write<SSL>));
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "writeHeader"), FunctionTemplate::New(isolate, res_writeHeader<SSL>));
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "onWritable"), FunctionTemplate::New(isolate, res_onWritable<SSL>));
/* Create our template */
Local<Object> resObjectLocal = resTemplateLocal->GetFunction()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
resTemplate[SSL].Reset(isolate, resObjectLocal);