Skip to content

Commit b76a20f

Browse files
committed
Merge pull request #943 from nodegit/revert-callback-throttle
Revert "Merge pull request #932 from srajko/callback-throttle"
2 parents 37424d2 + a2b870a commit b76a20f

11 files changed

Lines changed: 72 additions & 265 deletions

File tree

generate/input/callbacks.json

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@
9696
"type": "int",
9797
"noResults": 1,
9898
"success": 0,
99-
"error": -1,
100-
"throttle": 100
99+
"error": -1
101100
}
102101
},
103102
"git_checkout_perfdata_cb": {
@@ -208,8 +207,7 @@
208207
"type": "int",
209208
"noResults": 1,
210209
"success": 0,
211-
"error": -1,
212-
"throttle": 100
210+
"error": -1
213211
}
214212
},
215213
"git_diff_hunk_cb": {
@@ -562,8 +560,7 @@
562560
"type": "int",
563561
"noResults":0,
564562
"success": 0,
565-
"error": -1,
566-
"throttle": 100
563+
"error": -1
567564
}
568565
},
569566
"git_stash_cb": {
@@ -673,8 +670,7 @@
673670
"type": "int",
674671
"noResults": 0,
675672
"success": 0,
676-
"error": -1,
677-
"throttle": 100
673+
"error": -1
678674
}
679675
},
680676
"git_transport_cb": {

generate/templates/manual/include/async_baton.h

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
#include <uv.h>
55
#include <nan.h>
66

7-
#include "lock_master.h"
8-
#include "functions/sleep_for_ms.h"
9-
107
// Base class for Batons used for callbacks (for example,
118
// JS functions passed as callback parameters,
129
// or field properties of configuration objects whose values are callbacks)
@@ -16,33 +13,4 @@ struct AsyncBaton {
1613
bool done;
1714
};
1815

19-
template<typename ResultT>
20-
struct AsyncBatonWithResult : public AsyncBaton {
21-
ResultT result;
22-
ResultT defaultResult; // result returned if the callback doesn't return anything valid
23-
24-
AsyncBatonWithResult(const ResultT &defaultResult)
25-
: defaultResult(defaultResult) {
26-
}
27-
28-
ResultT ExecuteAsync(uv_async_cb asyncCallback) {
29-
result = 0;
30-
req.data = this;
31-
done = false;
32-
33-
uv_async_init(uv_default_loop(), &req, asyncCallback);
34-
{
35-
LockMaster::TemporaryUnlock temporaryUnlock;
36-
37-
uv_async_send(&req);
38-
39-
while(!done) {
40-
sleep_for_ms(1);
41-
}
42-
}
43-
44-
return result;
45-
}
46-
};
47-
4816
#endif
Lines changed: 6 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,17 @@
11
#ifndef CALLBACK_WRAPPER_H
22
#define CALLBACK_WRAPPER_H
33

4-
#include <nan.h>
5-
#include <uv.h>
4+
#include <v8.h>
5+
#include <node.h>
6+
7+
#include "nan.h"
68

79
using namespace v8;
810
using namespace node;
911

10-
class CallbackWrapper {
12+
struct CallbackWrapper {
1113
Nan::Callback* jsCallback;
12-
13-
// throttling data, used for callbacks that need to be throttled
14-
int throttle; // in milliseconds - if > 0, calls to the JS callback will be throttled
15-
uint64_t lastCallTime;
16-
17-
public:
18-
CallbackWrapper() {
19-
jsCallback = NULL;
20-
lastCallTime = 0;
21-
throttle = 0;
22-
}
23-
24-
~CallbackWrapper() {
25-
SetCallback(NULL);
26-
}
27-
28-
bool HasCallback() {
29-
return jsCallback != NULL;
30-
}
31-
32-
Nan::Callback* GetCallback() {
33-
return jsCallback;
34-
}
35-
36-
void SetCallback(Nan::Callback* callback, int throttle = 0) {
37-
if(jsCallback) {
38-
delete jsCallback;
39-
}
40-
jsCallback = callback;
41-
this->throttle = throttle;
42-
}
43-
44-
bool WillBeThrottled() {
45-
if(!throttle) {
46-
return false;
47-
}
48-
// throttle if needed
49-
uint64_t now = uv_hrtime();
50-
if(lastCallTime > 0 && now < lastCallTime + throttle * 1000000) {
51-
// throttled
52-
return true;
53-
} else {
54-
lastCallTime = now;
55-
return false;
56-
}
57-
}
14+
void * payload;
5815
};
5916

6017
#endif

generate/templates/manual/include/lock_master.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#ifndef LOCK_MASTER_H
22
#define LOCK_MASTER_H
33

4-
#include <git2.h>
5-
64
class LockMasterImpl;
75

86
class LockMaster {

generate/templates/partials/callback_helpers.cc

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,28 @@
66
{{ arg.cType }} {{ arg.name}}{% if not arg.lastArg %},{% endif %}
77
{% endeach %}
88
) {
9-
{{ cppFunctionName }}_{{ cbFunction.name|titleCase }}Baton* baton =
10-
new {{ cppFunctionName }}_{{ cbFunction.name|titleCase }}Baton({{ cbFunction.return.noResults }});
9+
{{ cppFunctionName }}_{{ cbFunction.name|titleCase }}Baton* baton = new {{ cppFunctionName }}_{{ cbFunction.name|titleCase }}Baton();
1110

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

16-
return baton->ExecuteAsync((uv_async_cb) {{ cppFunctionName }}_{{ cbFunction.name }}_async);
15+
baton->result = 0;
16+
baton->req.data = baton;
17+
baton->done = false;
18+
19+
uv_async_init(uv_default_loop(), &baton->req, (uv_async_cb) {{ cppFunctionName }}_{{ cbFunction.name }}_async);
20+
{
21+
LockMaster::TemporaryUnlock temporaryUnlock;
22+
23+
uv_async_send(&baton->req);
24+
25+
while(!baton->done) {
26+
sleep_for_ms(1);
27+
}
28+
}
29+
30+
return baton->result;
1731
}
1832

1933
void {{ cppClassName }}::{{ cppFunctionName }}_{{ cbFunction.name }}_async(uv_async_t* req, int status) {
@@ -79,12 +93,12 @@ void {{ cppClassName }}::{{ cppFunctionName }}_{{ cbFunction.name }}_async(uv_as
7993
baton->result = (int)result->ToNumber()->Value();
8094
}
8195
else {
82-
baton->result = baton->defaultResult;
96+
baton->result = {{ cbFunction.return.noResults }};
8397
}
8498
{% endif %}
8599
}
86100
else {
87-
baton->result = baton->defaultResult;
101+
baton->result = {{ cbFunction.return.noResults }};
88102
}
89103
{% endeach %}
90104

@@ -113,12 +127,12 @@ void {{ cppClassName }}::{{ cppFunctionName }}_{{ cbFunction.name }}_promiseComp
113127
baton->result = (int)result->ToNumber()->Value();
114128
}
115129
else {
116-
baton->result = baton->defaultResult;
130+
baton->result = {{ cbFunction.return.noResults }};
117131
}
118132
{% endif %}
119133
}
120134
else {
121-
baton->result = baton->defaultResult;
135+
baton->result = {{ cbFunction.return.noResults }};
122136
}
123137
{% endeach %}
124138
}

generate/templates/partials/field_accessors.cc

Lines changed: 30 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
info.GetReturnValue().Set(Nan::New(wrapper->{{ field.name }}));
1212

1313
{% elsif field.isCallbackFunction %}
14-
if (wrapper->{{field.name}}.HasCallback()) {
15-
info.GetReturnValue().Set(wrapper->{{ field.name }}.GetCallback()->GetFunction());
14+
if (wrapper->{{field.name}} != NULL) {
15+
info.GetReturnValue().Set(wrapper->{{ field.name }}->GetFunction());
1616
} else {
1717
info.GetReturnValue().SetUndefined();
1818
}
@@ -31,7 +31,6 @@
3131
}
3232

3333
NAN_SETTER({{ cppClassName }}::Set{{ field.cppFunctionName }}) {
34-
Nan::HandleScope scope;
3534

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

@@ -48,35 +47,16 @@
4847
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 %};
4948

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

5454
if (value->IsFunction()) {
55-
callback = new Nan::Callback(value.As<Function>());
56-
} else if (value->IsObject()) {
57-
Local<Object> object = value.As<Object>();
58-
Local<String> callbackKey;
59-
Nan::MaybeLocal<Value> maybeObjectCallback = Nan::Get(object, Nan::New("callback").ToLocalChecked());
60-
if (!maybeObjectCallback.IsEmpty()) {
61-
Local<Value> objectCallback = maybeObjectCallback.ToLocalChecked();
62-
if (objectCallback->IsFunction()) {
63-
callback = new Nan::Callback(objectCallback.As<Function>());
64-
Nan::MaybeLocal<Value> maybeObjectThrottle = Nan::Get(object, Nan::New("throttle").ToLocalChecked());
65-
if(!maybeObjectThrottle.IsEmpty()) {
66-
Local<Value> objectThrottle = maybeObjectThrottle.ToLocalChecked();
67-
if (objectThrottle->IsNumber()) {
68-
throttle = (int)objectThrottle.As<Number>()->Value();
69-
}
70-
}
71-
}
72-
}
73-
}
74-
if (callback) {
7555
if (!wrapper->raw->{{ field.name }}) {
7656
wrapper->raw->{{ field.name }} = ({{ field.cType }}){{ field.name }}_cppCallback;
7757
}
7858

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

8262
{% elsif field.payloadFor %}
@@ -102,42 +82,46 @@
10282
}
10383

10484
{% if field.isCallbackFunction %}
105-
{{ cppClassName }}* {{ cppClassName }}::{{ field.name }}_getInstanceFromBaton({{ field.name|titleCase }}Baton* baton) {
106-
return static_cast<{{ cppClassName }}*>(baton->{% each field.args|argsInfo as arg %}
107-
{% if arg.payload == true %}{{arg.name}}{% elsif arg.lastArg %}{{arg.name}}{% endif %}
108-
{% endeach %});
109-
}
110-
11185
{{ field.return.type }} {{ cppClassName }}::{{ field.name }}_cppCallback (
11286
{% each field.args|argsInfo as arg %}
11387
{{ arg.cType }} {{ arg.name}}{% if not arg.lastArg %},{% endif %}
11488
{% endeach %}
11589
) {
116-
{{ field.name|titleCase }}Baton* baton =
117-
new {{ field.name|titleCase }}Baton({{ field.return.noResults }});
90+
{{ field.name|titleCase }}Baton* baton = new {{ field.name|titleCase }}Baton();
11891

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

123-
{{ cppClassName }}* instance = {{ field.name }}_getInstanceFromBaton(baton);
96+
baton->result = 0;
97+
baton->req.data = baton;
98+
baton->done = false;
99+
100+
uv_async_init(uv_default_loop(), &baton->req, (uv_async_cb) {{ field.name }}_async);
101+
{
102+
LockMaster::TemporaryUnlock temporaryUnlock;
103+
104+
uv_async_send(&baton->req);
124105

125-
if (instance->{{ field.name }}.WillBeThrottled()) {
126-
return baton->defaultResult;
106+
while(!baton->done) {
107+
sleep_for_ms(1);
108+
}
127109
}
128110

129-
return baton->ExecuteAsync((uv_async_cb) {{ field.name }}_async);
111+
return baton->result;
130112
}
131113

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

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

138-
if (instance->{{ field.name }}.GetCallback()->IsEmpty()) {
122+
if (instance->{{ field.name }}->IsEmpty()) {
139123
{% if field.return.type == "int" %}
140-
baton->result = baton->defaultResult; // no results acquired
124+
baton->result = {{ field.return.noResults }}; // no results acquired
141125
{% endif %}
142126

143127
baton->done = true;
@@ -179,7 +163,7 @@
179163
};
180164

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

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

@@ -203,12 +187,12 @@
203187
baton->result = (int)result->ToNumber()->Value();
204188
}
205189
else {
206-
baton->result = baton->defaultResult;
190+
baton->result = {{ field.return.noResults }};
207191
}
208192
{% endif %}
209193
}
210194
else {
211-
baton->result = baton->defaultResult;
195+
baton->result = {{ field.return.noResults }};
212196
}
213197
{% endeach %}
214198
baton->done = true;
@@ -236,12 +220,12 @@
236220
baton->result = (int)result->ToNumber()->Value();
237221
}
238222
else{
239-
baton->result = baton->defaultResult;
223+
baton->result = {{ field.return.noResults }};
240224
}
241225
{% endif %}
242226
}
243227
else {
244-
baton->result = baton->defaultResult;
228+
baton->result = {{ field.return.noResults }};
245229
}
246230
{% endeach %}
247231
}

generate/templates/templates/class_content.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ extern "C" {
1111
#include "../include/lock_master.h"
1212
#include "../include/functions/copy.h"
1313
#include "../include/{{ filename }}.h"
14+
#include "../include/functions/sleep_for_ms.h"
1415

1516
{% each dependencies as dependency %}
1617
#include "{{ dependency }}"

0 commit comments

Comments
 (0)