FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Revert "Merge pull request #932 from srajko/callback-throttle" by johnhaley81 · Pull Request #943 · nodegit/nodegit · GitHub

Repository navigation

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

Filter by extension

Filter by extension .cc  (4) .h  (5) .js  (1) .json  (1) All 4 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
12 changes: 4 additions & 8 deletions generate/input/callbacks.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@
"type": "int",
"noResults": 1,
"success": 0,
"error": -1,
"throttle": 100
"error": -1
}
},
"git_checkout_perfdata_cb": {
Expand Down Expand Up @@ -208,8 +207,7 @@
"type": "int",
"noResults": 1,
"success": 0,
"error": -1,
"throttle": 100
"error": -1
}
},
"git_diff_hunk_cb": {
Expand Down Expand Up @@ -562,8 +560,7 @@
"type": "int",
"noResults":0,
"success": 0,
"error": -1,
"throttle": 100
"error": -1
}
},
"git_stash_cb": {
Expand Down Expand Up @@ -673,8 +670,7 @@
"type": "int",
"noResults": 0,
"success": 0,
"error": -1,
"throttle": 100
"error": -1
}
},
"git_transport_cb": {
Expand Down
32 changes: 0 additions & 32 deletions generate/templates/manual/include/async_baton.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
#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 @@ -16,33 +13,4 @@ struct AsyncBaton {
bool done;
};

template<typename ResultT>
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: 6 additions & 49 deletions generate/templates/manual/include/callback_wrapper.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,60 +1,17 @@
#ifndef CALLBACK_WRAPPER_H
#define CALLBACK_WRAPPER_H

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

#include "nan.h"

using namespace v8;
using namespace node;

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

// 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;
}
}
void * payload;
};

#endif
2 changes: 0 additions & 2 deletions generate/templates/manual/include/lock_master.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef LOCK_MASTER_H
#define LOCK_MASTER_H

#include <git2.h>

class LockMasterImpl;

class LockMaster {
Expand Down
28 changes: 21 additions & 7 deletions generate/templates/partials/callback_helpers.cc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,28 @@
{{ arg.cType }} {{ arg.name}}{% if not arg.lastArg %},{% endif %}
{% endeach %}
) {
{{ cppFunctionName }}_{{ cbFunction.name|titleCase }}Baton* baton =
new {{ cppFunctionName }}_{{ cbFunction.name|titleCase }}Baton({{ cbFunction.return.noResults }});
{{ cppFunctionName }}_{{ cbFunction.name|titleCase }}Baton* baton = new {{ cppFunctionName }}_{{ cbFunction.name|titleCase }}Baton();

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

return baton->ExecuteAsync((uv_async_cb) {{ cppFunctionName }}_{{ cbFunction.name }}_async);
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;
}

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

Expand Down Expand Up @@ -113,12 +127,12 @@ void {{ cppClassName }}::{{ cppFunctionName }}_{{ cbFunction.name }}_promiseComp
baton->result = (int)result->ToNumber()->Value();
}
else {
baton->result = baton->defaultResult;
baton->result = {{ cbFunction.return.noResults }};
}
{% endif %}
}
else {
baton->result = baton->defaultResult;
baton->result = {{ cbFunction.return.noResults }};
}
{% endeach %}
}
Expand Down
76 changes: 30 additions & 46 deletions generate/templates/partials/field_accessors.cc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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}}.HasCallback()) {
info.GetReturnValue().Set(wrapper->{{ field.name }}.GetCallback()->GetFunction());
if (wrapper->{{field.name}} != NULL) {
info.GetReturnValue().Set(wrapper->{{ field.name }}->GetFunction());
} else {
info.GetReturnValue().SetUndefined();
}
Expand All @@ -31,7 +31,6 @@
}

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

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

Expand All @@ -48,35 +47,16 @@
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 %}
Nan::Callback *callback = NULL;
int throttle = {%if field.return.throttle %}{{ field.return.throttle }}{%else%}0{%endif%};
if (wrapper->{{ field.name }} != NULL) {
delete wrapper->{{ field.name }};
}

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

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

{% 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.return.noResults }});
{{ field.name|titleCase }}Baton* baton = new {{ field.name|titleCase }}Baton();

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

{{ cppClassName }}* instance = {{ field.name }}_getInstanceFromBaton(baton);
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);

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

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

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 = {{ field.name }}_getInstanceFromBaton(baton);
{{ 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 %});

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

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

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

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

Expand All @@ -203,12 +187,12 @@
baton->result = (int)result->ToNumber()->Value();
}
else {
baton->result = baton->defaultResult;
baton->result = {{ field.return.noResults }};
}
{% endif %}
}
else {
baton->result = baton->defaultResult;
baton->result = {{ field.return.noResults }};
}
{% endeach %}
baton->done = true;
Expand Down Expand Up @@ -236,12 +220,12 @@
baton->result = (int)result->ToNumber()->Value();
}
else{
baton->result = baton->defaultResult;
baton->result = {{ field.return.noResults }};
}
{% endif %}
}
else {
baton->result = baton->defaultResult;
baton->result = {{ field.return.noResults }};
}
{% endeach %}
}
Expand Down
1 change: 1 addition & 0 deletions generate/templates/templates/class_content.cc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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

Back | FazBrowse Home | New Git URL