Skip to content

Commit 89faca2

Browse files
committed
Refactor structs to use CallbackWrapper for callbacks
just refactoring, leverages the previously unused CallbackWrapper to set us up for bundling throttling state with the javascript callback.
1 parent 266aa3f commit 89faca2

File tree

4 files changed

+31
-16
lines changed

4 files changed

+31
-16
lines changed

generate/templates/manual/include/callback_wrapper.h

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,31 @@
99
using namespace v8;
1010
using namespace node;
1111

12-
struct CallbackWrapper {
12+
class CallbackWrapper {
1313
Nan::Callback* jsCallback;
14-
void * payload;
14+
15+
public:
16+
CallbackWrapper() {
17+
jsCallback = NULL;
18+
}
19+
~CallbackWrapper() {
20+
SetCallback(NULL);
21+
}
22+
23+
bool HasCallback() {
24+
return jsCallback != NULL;
25+
}
26+
27+
Nan::Callback* GetCallback() {
28+
return jsCallback;
29+
}
30+
31+
void SetCallback(Nan::Callback* callback) {
32+
if(jsCallback) {
33+
delete jsCallback;
34+
}
35+
jsCallback = callback;
36+
}
1537
};
1638

1739
#endif

generate/templates/partials/field_accessors.cc

Lines changed: 5 additions & 9 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}} != NULL) {
15-
info.GetReturnValue().Set(wrapper->{{ field.name }}->GetFunction());
14+
if (wrapper->{{field.name}}.HasCallback()) {
15+
info.GetReturnValue().Set(wrapper->{{ field.name }}.GetCallback()->GetFunction());
1616
} else {
1717
info.GetReturnValue().SetUndefined();
1818
}
@@ -47,16 +47,12 @@
4747
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 %};
4848

4949
{% elsif field.isCallbackFunction %}
50-
if (wrapper->{{ field.name }} != NULL) {
51-
delete wrapper->{{ field.name }};
52-
}
53-
5450
if (value->IsFunction()) {
5551
if (!wrapper->raw->{{ field.name }}) {
5652
wrapper->raw->{{ field.name }} = ({{ field.cType }}){{ field.name }}_cppCallback;
5753
}
5854

59-
wrapper->{{ field.name }} = new Nan::Callback(value.As<Function>());
55+
wrapper->{{ field.name }}.SetCallback(new Nan::Callback(value.As<Function>()));
6056
}
6157

6258
{% elsif field.payloadFor %}
@@ -105,7 +101,7 @@
105101
{% if arg.payload == true %}{{arg.name}}{% elsif arg.lastArg %}{{arg.name}}{% endif %}
106102
{% endeach %});
107103

108-
if (instance->{{ field.name }}->IsEmpty()) {
104+
if (instance->{{ field.name }}.GetCallback()->IsEmpty()) {
109105
{% if field.return.type == "int" %}
110106
baton->result = baton->defaultResult; // no results acquired
111107
{% endif %}
@@ -149,7 +145,7 @@
149145
};
150146

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

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

generate/templates/templates/struct_content.cc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ using namespace std;
5252
{% if not field.ignore %}
5353
{% if not field.isEnum %}
5454
{% if field.isCallbackFunction %}
55-
if (this->{{ field.name }} != NULL) {
56-
delete this->{{ field.name }};
5755
this->raw->{{ fields|payloadFor field.name }} = NULL;
58-
}
5956
{% endif %}
6057
{% endif %}
6158
{% endif %}
@@ -83,7 +80,6 @@ void {{ cppClassName }}::ConstructFields() {
8380
// the current instance
8481
this->raw->{{ field.name }} = NULL;
8582
this->raw->{{ fields|payloadFor field.name }} = (void *)this;
86-
this->{{ field.name }} = NULL;
8783
{% elsif field.payloadFor %}
8884

8985
Local<Value> {{ field.name }} = Nan::Undefined();

generate/templates/templates/struct_header.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <utility>
77

88
#include "async_baton.h"
9+
#include "callback_wrapper.h"
910

1011
extern "C" {
1112
#include <git2.h>
@@ -75,7 +76,7 @@ class {{ cppClassName }} : public Nan::ObjectWrap {
7576
{% if field.isLibgitType %}
7677
Nan::Persistent<Object> {{ field.name }};
7778
{% elsif field.isCallbackFunction %}
78-
Nan::Callback* {{ field.name }};
79+
CallbackWrapper {{ field.name }};
7980
{% elsif field.payloadFor %}
8081
Nan::Persistent<Value> {{ field.name }};
8182
{% endif %}

0 commit comments

Comments
 (0)