Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions 12 generate/input/callbacks.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@
"type": "int",
"noResults": 1,
"success": 0,
"error": -1
"error": -1,
"throttle": 100
}
},
"git_checkout_perfdata_cb": {
Expand Down Expand Up @@ -207,7 +208,8 @@
"type": "int",
"noResults": 1,
"success": 0,
"error": -1
"error": -1,
"throttle": 100
}
},
"git_diff_hunk_cb": {
Expand Down Expand Up @@ -560,7 +562,8 @@
"type": "int",
"noResults":0,
"success": 0,
"error": -1
"error": -1,
"throttle": 100
}
},
"git_stash_cb": {
Expand Down Expand Up @@ -670,7 +673,8 @@
"type": "int",
"noResults": 0,
"success": 0,
"error": -1
"error": -1,
"throttle": 100
}
},
"git_transport_cb": {
Expand Down
32 changes: 32 additions & 0 deletions 32 generate/templates/manual/include/async_baton.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include <uv.h>
#include <nan.h>

#include "lock_master.h"
#include "functions/sleep_for_ms.h"

// Base class for Batons used for callbacks (for example,
// JS functions passed as callback parameters,
// or field properties of configuration objects whose values are callbacks)
Expand All @@ -13,4 +16,33 @@ struct AsyncBaton {
bool done;
};

template<typename ResultT>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

struct AsyncBatonWithResult : public AsyncBaton {
ResultT result;
ResultT defaultResult; // result returned if the callback doesn't return anything valid

AsyncBatonWithResult(const ResultT &defaultResult)
: defaultResult(defaultResult) {
}

ResultT ExecuteAsync(uv_async_cb asyncCallback) {
result = 0;
req.data = this;
done = false;

uv_async_init(uv_default_loop(), &req, asyncCallback);
{
LockMaster::TemporaryUnlock temporaryUnlock;

uv_async_send(&req);

while(!done) {
sleep_for_ms(1);
}
}

return result;
}
};

#endif
55 changes: 49 additions & 6 deletions 55 generate/templates/manual/include/callback_wrapper.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,60 @@
#ifndef CALLBACK_WRAPPER_H
#define CALLBACK_WRAPPER_H

#include <v8.h>
#include <node.h>

#include "nan.h"
#include <nan.h>
#include <uv.h>

using namespace v8;
using namespace node;

struct CallbackWrapper {
class CallbackWrapper {
Nan::Callback* jsCallback;
void * payload;

// throttling data, used for callbacks that need to be throttled
int throttle; // in milliseconds - if > 0, calls to the JS callback will be throttled
uint64_t lastCallTime;

public:
CallbackWrapper() {
jsCallback = NULL;
lastCallTime = 0;
throttle = 0;
}

~CallbackWrapper() {
SetCallback(NULL);
}

bool HasCallback() {
return jsCallback != NULL;
}

Nan::Callback* GetCallback() {
return jsCallback;
}

void SetCallback(Nan::Callback* callback, int throttle = 0) {
if(jsCallback) {
delete jsCallback;
}
jsCallback = callback;
this->throttle = throttle;
}

bool WillBeThrottled() {
if(!throttle) {
return false;
}
// throttle if needed
uint64_t now = uv_hrtime();
if(lastCallTime > 0 && now < lastCallTime + throttle * 1000000) {
// throttled
return true;
} else {
lastCallTime = now;
return false;
}
}
};

#endif
2 changes: 2 additions & 0 deletions 2 generate/templates/manual/include/lock_master.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef LOCK_MASTER_H
#define LOCK_MASTER_H

#include <git2.h>

class LockMasterImpl;

class LockMaster {
Expand Down
28 changes: 7 additions & 21 deletions 28 generate/templates/partials/callback_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,14 @@
{{ arg.cType }} {{ arg.name}}{% if not arg.lastArg %},{% endif %}
{% endeach %}
) {
{{ cppFunctionName }}_{{ cbFunction.name|titleCase }}Baton* baton = new {{ cppFunctionName }}_{{ cbFunction.name|titleCase }}Baton();
{{ cppFunctionName }}_{{ cbFunction.name|titleCase }}Baton* baton =
new {{ cppFunctionName }}_{{ cbFunction.name|titleCase }}Baton({{ cbFunction.return.noResults }});

{% each cbFunction.args|argsInfo as arg %}
baton->{{ arg.name }} = {{ arg.name }};
{% endeach %}

baton->result = 0;
baton->req.data = baton;
baton->done = false;

uv_async_init(uv_default_loop(), &baton->req, (uv_async_cb) {{ cppFunctionName }}_{{ cbFunction.name }}_async);
{
LockMaster::TemporaryUnlock temporaryUnlock;

uv_async_send(&baton->req);

while(!baton->done) {
sleep_for_ms(1);
}
}

return baton->result;
return baton->ExecuteAsync((uv_async_cb) {{ cppFunctionName }}_{{ cbFunction.name }}_async);
}

void {{ cppClassName }}::{{ cppFunctionName }}_{{ cbFunction.name }}_async(uv_async_t* req, int status) {
Expand Down Expand Up @@ -93,12 +79,12 @@ void {{ cppClassName }}::{{ cppFunctionName }}_{{ cbFunction.name }}_async(uv_as
baton->result = (int)result->ToNumber()->Value();
}
else {
baton->result = {{ cbFunction.return.noResults }};
baton->result = baton->defaultResult;
}
{% endif %}
}
else {
baton->result = {{ cbFunction.return.noResults }};
baton->result = baton->defaultResult;
}
{% endeach %}

Expand Down Expand Up @@ -127,12 +113,12 @@ void {{ cppClassName }}::{{ cppFunctionName }}_{{ cbFunction.name }}_promiseComp
baton->result = (int)result->ToNumber()->Value();
}
else {
baton->result = {{ cbFunction.return.noResults }};
baton->result = baton->defaultResult;
}
{% endif %}
}
else {
baton->result = {{ cbFunction.return.noResults }};
baton->result = baton->defaultResult;
}
{% endeach %}
}
Expand Down
76 changes: 46 additions & 30 deletions 76 generate/templates/partials/field_accessors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
info.GetReturnValue().Set(Nan::New(wrapper->{{ field.name }}));

{% elsif field.isCallbackFunction %}
if (wrapper->{{field.name}} != NULL) {
info.GetReturnValue().Set(wrapper->{{ field.name }}->GetFunction());
if (wrapper->{{field.name}}.HasCallback()) {
info.GetReturnValue().Set(wrapper->{{ field.name }}.GetCallback()->GetFunction());
} else {
info.GetReturnValue().SetUndefined();
}
Expand All @@ -31,6 +31,7 @@
}

NAN_SETTER({{ cppClassName }}::Set{{ field.cppFunctionName }}) {
Nan::HandleScope scope;

{{ cppClassName }} *wrapper = Nan::ObjectWrap::Unwrap<{{ cppClassName }}>(info.This());

Expand All @@ -47,16 +48,35 @@
wrapper->raw->{{ field.name }} = {% if not field.cType | isPointer %}*{% endif %}{% if field.cppClassName == 'GitStrarray' %}StrArrayConverter::Convert({{ field.name }}->ToObject()){% else %}Nan::ObjectWrap::Unwrap<{{ field.cppClassName }}>({{ field.name }}->ToObject())->GetValue(){% endif %};

{% elsif field.isCallbackFunction %}
if (wrapper->{{ field.name }} != NULL) {
delete wrapper->{{ field.name }};
}
Nan::Callback *callback = NULL;
int throttle = {%if field.return.throttle %}{{ field.return.throttle }}{%else%}0{%endif%};

if (value->IsFunction()) {
callback = new Nan::Callback(value.As<Function>());
} else if (value->IsObject()) {
Local<Object> object = value.As<Object>();
Local<String> callbackKey;
Nan::MaybeLocal<Value> maybeObjectCallback = Nan::Get(object, Nan::New("callback").ToLocalChecked());
if (!maybeObjectCallback.IsEmpty()) {
Local<Value> objectCallback = maybeObjectCallback.ToLocalChecked();
if (objectCallback->IsFunction()) {
callback = new Nan::Callback(objectCallback.As<Function>());
Nan::MaybeLocal<Value> maybeObjectThrottle = Nan::Get(object, Nan::New("throttle").ToLocalChecked());
if(!maybeObjectThrottle.IsEmpty()) {
Local<Value> objectThrottle = maybeObjectThrottle.ToLocalChecked();
if (objectThrottle->IsNumber()) {
throttle = (int)objectThrottle.As<Number>()->Value();
}
}
}
}
}
if (callback) {
if (!wrapper->raw->{{ field.name }}) {
wrapper->raw->{{ field.name }} = ({{ field.cType }}){{ field.name }}_cppCallback;
}

wrapper->{{ field.name }} = new Nan::Callback(value.As<Function>());
wrapper->{{ field.name }}.SetCallback(callback, throttle);
}

{% elsif field.payloadFor %}
Expand All @@ -82,46 +102,42 @@
}

{% if field.isCallbackFunction %}
{{ cppClassName }}* {{ cppClassName }}::{{ field.name }}_getInstanceFromBaton({{ field.name|titleCase }}Baton* baton) {
return static_cast<{{ cppClassName }}*>(baton->{% each field.args|argsInfo as arg %}
{% if arg.payload == true %}{{arg.name}}{% elsif arg.lastArg %}{{arg.name}}{% endif %}
{% endeach %});
}

{{ field.return.type }} {{ cppClassName }}::{{ field.name }}_cppCallback (
{% each field.args|argsInfo as arg %}
{{ arg.cType }} {{ arg.name}}{% if not arg.lastArg %},{% endif %}
{% endeach %}
) {
{{ field.name|titleCase }}Baton* baton = new {{ field.name|titleCase }}Baton();
{{ field.name|titleCase }}Baton* baton =
new {{ field.name|titleCase }}Baton({{ field.return.noResults }});

{% each field.args|argsInfo as arg %}
baton->{{ arg.name }} = {{ arg.name }};
{% endeach %}

baton->result = 0;
baton->req.data = baton;
baton->done = false;

uv_async_init(uv_default_loop(), &baton->req, (uv_async_cb) {{ field.name }}_async);
{
LockMaster::TemporaryUnlock temporaryUnlock;

uv_async_send(&baton->req);
{{ cppClassName }}* instance = {{ field.name }}_getInstanceFromBaton(baton);

while(!baton->done) {
sleep_for_ms(1);
}
if (instance->{{ field.name }}.WillBeThrottled()) {
return baton->defaultResult;
}

return baton->result;
return baton->ExecuteAsync((uv_async_cb) {{ field.name }}_async);
}

void {{ cppClassName }}::{{ field.name }}_async(uv_async_t* req, int status) {
Nan::HandleScope scope;

{{ field.name|titleCase }}Baton* baton = static_cast<{{ field.name|titleCase }}Baton*>(req->data);
{{ cppClassName }}* instance = static_cast<{{ cppClassName }}*>(baton->{% each field.args|argsInfo as arg %}
{% if arg.payload == true %}{{arg.name}}{% elsif arg.lastArg %}{{arg.name}}{% endif %}
{% endeach %});
{{ cppClassName }}* instance = {{ field.name }}_getInstanceFromBaton(baton);

if (instance->{{ field.name }}->IsEmpty()) {
if (instance->{{ field.name }}.GetCallback()->IsEmpty()) {
{% if field.return.type == "int" %}
baton->result = {{ field.return.noResults }}; // no results acquired
baton->result = baton->defaultResult; // no results acquired
{% endif %}

baton->done = true;
Expand Down Expand Up @@ -163,7 +179,7 @@
};

Nan::TryCatch tryCatch;
Local<v8::Value> result = instance->{{ field.name }}->Call({{ field.args|jsArgsCount }}, argv);
Local<v8::Value> result = instance->{{ field.name }}.GetCallback()->Call({{ field.args|jsArgsCount }}, argv);

uv_close((uv_handle_t*) &baton->req, NULL);

Expand All @@ -187,12 +203,12 @@
baton->result = (int)result->ToNumber()->Value();
}
else {
baton->result = {{ field.return.noResults }};
baton->result = baton->defaultResult;
}
{% endif %}
}
else {
baton->result = {{ field.return.noResults }};
baton->result = baton->defaultResult;
}
{% endeach %}
baton->done = true;
Expand Down Expand Up @@ -220,12 +236,12 @@
baton->result = (int)result->ToNumber()->Value();
}
else{
baton->result = {{ field.return.noResults }};
baton->result = baton->defaultResult;
}
{% endif %}
}
else {
baton->result = {{ field.return.noResults }};
baton->result = baton->defaultResult;
}
{% endeach %}
}
Expand Down
1 change: 0 additions & 1 deletion 1 generate/templates/templates/class_content.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ extern "C" {
#include "../include/lock_master.h"
#include "../include/functions/copy.h"
#include "../include/{{ filename }}.h"
#include "../include/functions/sleep_for_ms.h"

{% each dependencies as dependency %}
#include "{{ dependency }}"
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.