Skip to content

Commit 266aa3f

Browse files
committed
Store defaultResult in baton
I originally tried doing the throttling in the baton but that ended up being a mistake. This moved the default result to the baton so the baton could use it. I don't think the change is necessary for the final version of this PR, but I still like moving things out of combyne-templated files so I kept it.
1 parent 2e98617 commit 266aa3f

File tree

5 files changed

+26
-11
lines changed

5 files changed

+26
-11
lines changed

generate/templates/manual/include/async_baton.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ struct AsyncBaton {
1919
template<typename ResultT>
2020
struct AsyncBatonWithResult : public AsyncBaton {
2121
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+
}
2227

2328
ResultT ExecuteAsync(uv_async_cb asyncCallback) {
2429
result = 0;

generate/templates/partials/callback_helpers.cc

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

1112
{% each cbFunction.args|argsInfo as arg %}
1213
baton->{{ arg.name }} = {{ arg.name }};
@@ -78,12 +79,12 @@ void {{ cppClassName }}::{{ cppFunctionName }}_{{ cbFunction.name }}_async(uv_as
7879
baton->result = (int)result->ToNumber()->Value();
7980
}
8081
else {
81-
baton->result = {{ cbFunction.return.noResults }};
82+
baton->result = baton->defaultResult;
8283
}
8384
{% endif %}
8485
}
8586
else {
86-
baton->result = {{ cbFunction.return.noResults }};
87+
baton->result = baton->defaultResult;
8788
}
8889
{% endeach %}
8990

@@ -112,12 +113,12 @@ void {{ cppClassName }}::{{ cppFunctionName }}_{{ cbFunction.name }}_promiseComp
112113
baton->result = (int)result->ToNumber()->Value();
113114
}
114115
else {
115-
baton->result = {{ cbFunction.return.noResults }};
116+
baton->result = baton->defaultResult;
116117
}
117118
{% endif %}
118119
}
119120
else {
120-
baton->result = {{ cbFunction.return.noResults }};
121+
baton->result = baton->defaultResult;
121122
}
122123
{% endeach %}
123124
}

generate/templates/partials/field_accessors.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@
8787
{{ arg.cType }} {{ arg.name}}{% if not arg.lastArg %},{% endif %}
8888
{% endeach %}
8989
) {
90-
{{ field.name|titleCase }}Baton* baton = new {{ field.name|titleCase }}Baton();
90+
{{ field.name|titleCase }}Baton* baton =
91+
new {{ field.name|titleCase }}Baton({{ field.return.noResults }});
9192

9293
{% each field.args|argsInfo as arg %}
9394
baton->{{ arg.name }} = {{ arg.name }};
@@ -106,7 +107,7 @@
106107

107108
if (instance->{{ field.name }}->IsEmpty()) {
108109
{% if field.return.type == "int" %}
109-
baton->result = {{ field.return.noResults }}; // no results acquired
110+
baton->result = baton->defaultResult; // no results acquired
110111
{% endif %}
111112

112113
baton->done = true;
@@ -172,12 +173,12 @@
172173
baton->result = (int)result->ToNumber()->Value();
173174
}
174175
else {
175-
baton->result = {{ field.return.noResults }};
176+
baton->result = baton->defaultResult;
176177
}
177178
{% endif %}
178179
}
179180
else {
180-
baton->result = {{ field.return.noResults }};
181+
baton->result = baton->defaultResult;
181182
}
182183
{% endeach %}
183184
baton->done = true;
@@ -205,12 +206,12 @@
205206
baton->result = (int)result->ToNumber()->Value();
206207
}
207208
else{
208-
baton->result = {{ field.return.noResults }};
209+
baton->result = baton->defaultResult;
209210
}
210211
{% endif %}
211212
}
212213
else {
213-
baton->result = {{ field.return.noResults }};
214+
baton->result = baton->defaultResult;
214215
}
215216
{% endeach %}
216217
}

generate/templates/templates/class_header.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ class {{ cppClassName }} : public Nan::ObjectWrap {
6868
{% each arg.args|argsInfo as cbArg %}
6969
{{ cbArg.cType }} {{ cbArg.name }};
7070
{% endeach %}
71+
72+
{{ function.cppFunctionName }}_{{ arg.name|titleCase }}Baton(const {{ arg.return.type }} &defaultResult)
73+
: AsyncBatonWithResult<{{ arg.return.type }}>(defaultResult) {
74+
}
7175
};
7276
{% endif %}
7377
{% endeach %}

generate/templates/templates/struct_header.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ class {{ cppClassName }} : public Nan::ObjectWrap {
5252
{% each field.args|argsInfo as arg %}
5353
{{ arg.cType }} {{ arg.name}};
5454
{% endeach %}
55+
56+
{{ field.name|titleCase }}Baton(const {{ field.return.type }} &defaultResult)
57+
: AsyncBatonWithResult<{{ field.return.type }}>(defaultResult) {
58+
}
5559
};
5660
{% endif %}
5761
{% endif %}

0 commit comments

Comments
 (0)