Skip to content

Commit 248e673

Browse files
committed
Major rewrite for callbacks
1 parent a091b59 commit 248e673

9 files changed

Lines changed: 98 additions & 17 deletions

File tree

generate/templates/partials/async_function.cc

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,65 @@ void {{ cppClassName }}::{{ cppFunctionName }}Worker::HandleOKCallback() {
140140
free((void *)baton->error->message);
141141
free((void *)baton->error);
142142
} else if (baton->error_code < 0) {
143+
std::queue< Local<v8::Value> > workerArguments;
144+
{%each args|argsInfo as arg %}
145+
{%if not arg.isReturn %}
146+
{%if not arg.isSelf %}
147+
{%if not arg.isCallbackFunction %}
148+
workerArguments.push(Nan::New(GetFromPersistent("{{ arg.name }}")));
149+
{%endif%}
150+
{%endif%}
151+
{%endif%}
152+
{%endeach%}
153+
while(!workerArguments.empty()) {
154+
Local<v8::Value> node = workerArguments.front();
155+
workerArguments.pop();
156+
157+
if (
158+
!node->IsObject()
159+
|| node->IsArray()
160+
|| node->IsBooleanObject()
161+
|| node->IsDate()
162+
|| node->IsFunction()
163+
|| node->IsNumberObject()
164+
|| node->IsRegExp()
165+
|| node->IsStringObject()
166+
) {
167+
continue;
168+
}
169+
170+
Local<v8::Object> nodeObj = node->ToObject();
171+
Local<v8::Value> checkValue = nodeObj->GetHiddenValue(Nan::New("NodeGitPromiseError").ToLocalChecked());
172+
173+
if (!checkValue.IsEmpty()) {
174+
Local<v8::Value> argv[1] = {
175+
checkValue->ToObject()
176+
};
177+
callback->Call(1, argv);
178+
return;
179+
}
180+
181+
Local<v8::Array> properties = nodeObj->GetPropertyNames();
182+
for (unsigned int propIndex = 0; propIndex < properties->Length(); ++propIndex) {
183+
Local<v8::String> propName = properties->Get(propIndex)->ToString();
184+
Local<v8::Value> nodeToQueue = Nan::New(nodeObj->Get(propName));
185+
if (!nodeToQueue->IsUndefined()) {
186+
workerArguments.push(nodeToQueue);
187+
}
188+
}
189+
}
190+
// Local<v8::Object> arguments = GetFromPersistent("opts")->ToObject();
191+
// Local<v8::Object> moreTest = test->Get(Nan::New("callbacks").ToLocalChecked())->ToObject();
192+
//
193+
// Local<Value> argmoo[1]; // MSBUILD won't assign an array of length 0
194+
// Local<v8::Object> promise = moreTest->GetHiddenValue(Nan::New("what_if_i_was_error").ToLocalChecked())->ToObject();
195+
// Nan::Callback* isFulfilledFn = new Nan::Callback(Nan::Get(promise, Nan::New("reason").ToLocalChecked()).ToLocalChecked().As<Function>());
196+
// Local<v8::String> test2 = isFulfilledFn->Call(promise, 0, argmoo)->ToString();
197+
// v8::String::Utf8Value param1(test2);
198+
199+
// convert it to string
200+
// std::string foo = std::string(*param1);
201+
// std::cout << foo << std::endl;
143202
Local<v8::Object> err = Nan::Error("Method {{ jsFunctionName }} has thrown an error.")->ToObject();
144203
err->Set(Nan::New("errno").ToLocalChecked(), Nan::New(baton->error_code));
145204
Local<v8::Value> argv[1] = {

generate/templates/partials/callback_helpers.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,12 @@ void {{ cppClassName }}::{{ cppFunctionName }}_{{ cbFunction.name }}_asyncPromis
171171
}
172172
else {
173173
// promise was rejected
174+
{{ cppClassName }}* instance = static_cast<{{ cppClassName }}*>(baton->{% each cbFunction.args|argsInfo as arg %}
175+
{% if arg.payload == true %}{{arg.name}}{% elsif arg.lastArg %}{{arg.name}}{% endif %}
176+
{% endeach %});
177+
Local<v8::Object> parent = instance->handle();
178+
parent->SetHiddenValue(Nan::New("NodeGitPromiseError").ToLocalChecked(), Nan::New(baton->promise));
179+
174180
baton->result = {{ cbFunction.return.error }};
175181
baton->done = true;
176182
}

generate/templates/partials/field_accessors.cc

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

1313
{% elsif field.isCallbackFunction %}
14-
info.GetReturnValue().Set(wrapper->{{ field.name }}->GetFunction());
14+
if (wrapper->{{field.name}} != NULL) {
15+
info.GetReturnValue().Set(wrapper->{{ field.name }}->GetFunction());
16+
} else {
17+
info.GetReturnValue().SetUndefined();
18+
}
1519

1620
{% elsif field.cppClassName == 'String' %}
1721
if (wrapper->GetValue()->{{ field.name }}) {
@@ -260,6 +264,12 @@
260264
}
261265
else {
262266
// promise was rejected
267+
{{ cppClassName }}* instance = static_cast<{{ cppClassName }}*>(baton->{% each field.args|argsInfo as arg %}
268+
{% if arg.payload == true %}{{arg.name}}{% elsif arg.lastArg %}{{arg.name}}{% endif %}
269+
{% endeach %});
270+
Local<v8::Object> parent = instance->handle();
271+
parent->SetHiddenValue(Nan::New("NodeGitPromiseError").ToLocalChecked(), Nan::New(baton->promise));
272+
263273
baton->result = {{ field.return.error }};
264274
baton->done = true;
265275
}

generate/templates/templates/class_content.cc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ using namespace node;
5252
{% endeach %}
5353
{% endif %}
5454
{% endeach %}
55-
56-
this->callbackError.Reset();
57-
5855
}
5956

6057
void {{ cppClassName }}::InitializeComponent(Local<v8::Object> target) {

generate/templates/templates/class_header.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// generated from class_header.h
44
#include <nan.h>
55
#include <string>
6+
#include <queue>
67

78
extern "C" {
89
#include <git2.h>
@@ -176,7 +177,6 @@ class {{ cppClassName }} : public Nan::ObjectWrap {
176177
{%if cType%}
177178
{{ cType }} *raw;
178179
{%endif%}
179-
Nan::Persistent<v8::Value> callbackError;
180180
};
181181

182182
#endif

generate/templates/templates/struct_content.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ using namespace std;
5757
{% endif %}
5858
{% endif %}
5959
{% endeach %}
60-
61-
this->callbackError.Reset();
62-
60+
6361
if (this->selfFreeing) {
6462
free(this->raw);
6563
}

generate/templates/templates/struct_header.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// generated from struct_header.h
44
#include <nan.h>
55
#include <string>
6+
#include <queue>
67

78
extern "C" {
89
#include <git2.h>
@@ -88,7 +89,6 @@ class {{ cppClassName }} : public Nan::ObjectWrap {
8889
{% endeach %}
8990

9091
{{ cType }} *raw;
91-
Nan::Persistent<v8::Value> callbackError;
9292
};
9393

9494
#endif

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"dependencies": {
4040
"fs-extra": "^0.24.0",
4141
"node-pre-gyp": "^0.6.10",
42-
"nodegit-promise": "^3.0.2",
42+
"nodegit-promise": "^3.0.3",
4343
"npm": "^3.3.3",
4444
"promisify-node": "^0.2.1",
4545
"which-native-nodish": "^1.1.3"

test/tests/remote.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ describe("Remote", function() {
245245
});
246246
});
247247

248-
it("will reject if credentials promise rejects", function() {
248+
it.only("will reject if credentials promise rejects", function() {
249249
this.timeout(5000);
250250
var repo = this.repository;
251251
var branch = "should-not-exist";
@@ -256,10 +256,14 @@ describe("Remote", function() {
256256
var options = {
257257
callbacks: {
258258
credentials: function(url, userName) {
259-
return Promise.resolve()
260-
.then(Promise.resolve)
261-
.then(Promise.resolve)
262-
.then(Promise.reject);
259+
var test = Promise.resolve("test")
260+
.then(function() { return; })
261+
.then(function() { return; })
262+
.then(function() { return; })
263+
.then(function() {
264+
return Promise.reject(new Error("Failure case"));
265+
});
266+
return test;
263267
},
264268
certificateCheck: function() {
265269
return 1;
@@ -274,8 +278,15 @@ describe("Remote", function() {
274278
return Promise.reject(
275279
new Error("should not be able to push to the repository"));
276280
}, function(err) {
277-
if (err.errno === NodeGit.Error.CODE.ERROR &&
278-
err.message === "Method push has thrown an error.")
281+
if (err.message === "Error: Failure case")
282+
{
283+
return Promise.resolve();
284+
} else {
285+
throw err;
286+
}
287+
})
288+
.catch(function(err) {
289+
if (err.message === "Error: Failure case")
279290
{
280291
return Promise.resolve();
281292
} else {

0 commit comments

Comments
 (0)