Skip to content

Commit c60cd39

Browse files
committed
CLI: Annotated callback types in pbjs-generated services, see #582
1 parent b1b6a81 commit c60cd39

File tree

4 files changed

+78
-13
lines changed

4 files changed

+78
-13
lines changed

cli/targets/static.js

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var Type = protobuf.Type,
1414
var out = [];
1515
var indent = 0;
1616
var config = {};
17+
var firstService = true;
1718

1819
static_target.description = "Static code without reflection";
1920

@@ -47,13 +48,14 @@ function static_target(root, options, callback) {
4748
push("});");
4849
--indent;
4950
push("});");
50-
callback(null, out.join('\n'));
51+
return callback(null, out.join("\n"));
5152
} catch (err) {
52-
callback(err);
53+
return callback(err);
5354
} finally {
5455
out = [];
5556
indent = 0;
5657
config = {};
58+
firstService = true;
5759
}
5860
}
5961

@@ -63,12 +65,12 @@ function push(line) {
6365
var ind = "";
6466
for (var i = 0; i < indent; ++i)
6567
ind += " ";
66-
out.push(ind + line);
68+
return out.push(ind + line);
6769
}
6870

6971
function pushComment(lines) {
7072
push("/**");
71-
lines.forEach(function(line, i) {
73+
lines.forEach(function(line) {
7274
push(" * " + line);
7375
});
7476
push(" */");
@@ -133,7 +135,7 @@ function buildFunction(type, functionName, gen, scope) {
133135
return field.resolve().resolvedType
134136
? JSON.stringify(field.resolvedType.fullName.substring(1))
135137
: "null";
136-
}).join(',') + "]);");
138+
}).join(",") + "]);");
137139
push("return " + lines[0]);
138140
lines.slice(1).forEach(function(line) {
139141
var prev = indent;
@@ -366,21 +368,47 @@ function buildType(ref, type) {
366368
function buildService(ref, service) {
367369
var fullName = service.fullName.substring(1);
368370

371+
if (firstService) {
372+
firstService = false;
373+
374+
push("");
375+
pushComment([
376+
"RPC implementation passed to services performing a service request on network level, i.e. by utilizing http requests or websockets.",
377+
"@typedef RPCImpl",
378+
"@type {function}",
379+
"@param {Method} method Reflected method being called",
380+
"@param {Uint8Array} requestData Request data",
381+
"@param {RPCCallback} callback Callback function",
382+
"@returns {undefined}"
383+
]);
384+
385+
push("");
386+
pushComment([
387+
"Node-style callback as used by {@link RPCImpl}.",
388+
"@typedef RPCCallback",
389+
"@type {function}",
390+
"@param {?Error} error Error, if any, otherwise `null`",
391+
"@param {Uint8Array} [responseData] Response data or `null` to signal end of stream, if there hasn't been an error",
392+
"@returns {undefined}"
393+
]);
394+
}
395+
369396
push("");
370397
pushComment([
371398
"Constructs a new " + service.name + ".",
372399
"@exports " + fullName,
373400
"@constructor",
374-
"@param {function(function, Uint8Array, function)} rpc RPC implementation",
401+
"@param {RPCImpl} rpc RPC implementation",
375402
"@param {boolean} [requestDelimited=false] Whether requests are length-delimited",
376403
"@param {boolean} [responseDelimited=false] Whether responses are length-delimited"
377404
]);
378405
push("function " + name(service.name) + "(rpc, requestDelimited, responseDelimited) {");
379406
++indent;
407+
380408
push("");
381409
pushComment([
382410
"RPC implementation.",
383-
"@type {function(function, Uint8Array, function)}"
411+
"@type {RPCImpl}"
384412
]);
385413
push("this.rpc = rpc;");
386414
push("");
@@ -397,14 +425,24 @@ function buildService(ref, service) {
397425
push("this.responseDelimited = Boolean(responseDelimited);");
398426
--indent;
399427
push("};");
428+
400429
service.getMethodsArray().forEach(function(method) {
401430
method.resolve();
402431
var lcName = method.name.substring(0, 1).toLowerCase() + method.name.substring(1);
403432
push("");
433+
var cbName = name(service.name) + "_" + name(lcName) + "_Callback";
434+
pushComment([
435+
"Callback as used by {@link " + name(service.name) + "#" + name(lcName) + "}.",
436+
"@typedef " + cbName,
437+
"@type {function}",
438+
"@param {?Error} error Error, if any",
439+
"@param {" + method.resolvedResponseType.fullName.substring(1) + "} [response] " + method.resolvedResponseType.name
440+
]);
441+
push("");
404442
pushComment([
405443
"Calls " + method.name + ".",
406444
"@param {" + method.resolvedRequestType.fullName.substring(1) + "|Object} request " + method.resolvedRequestType.name + " or plain object",
407-
"@param {function(?Error, " + method.resolvedResponseType.fullName.substring(1) + "=)} callback Node-style callback called with the error, if any, and " + method.resolvedResponseType.name,
445+
"@param {" + cbName + "} callback Node-style callback called with the error, if any, and " + method.resolvedResponseType.name,
408446
"@returns {undefined}"
409447
]);
410448
push(name(service.name) + ".prototype[" + JSON.stringify(lcName) + "] = function " + name(lcName) + "(request, callback) {");

tests/data/ambiguous-names.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ $root.B = (function() {
188188
return function encode(m, w) {
189189
w||(w=Writer.create())
190190
if(m["A"]!==undefined&&m["A"]!==null)
191-
types[0].encode(m["A"],w.fork()).len&&w.ldelim(1)||w.reset()
191+
types[0].encode(m["A"],w.uint32(10).fork()).ldelim()
192192
return w
193193
}
194194
/* eslint-enable */

tests/data/package.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ $root.Package = (function() {
173173
if(m["license"]!==undefined&&m["license"]!=="")
174174
w.uint32(42).string(m["license"])
175175
if(m["repository"]!==undefined&&m["repository"]!==null)
176-
types[5].encode(m["repository"],w.fork()).len&&w.ldelim(6)||w.reset()
176+
types[5].encode(m["repository"],w.uint32(50).fork()).ldelim()
177177
if(m["bugs"]!==undefined&&m["bugs"]!=="")
178178
w.uint32(58).string(m["bugs"])
179179
if(m["homepage"]!==undefined&&m["homepage"]!=="")

tests/data/rpc.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,38 @@ var $root = {};
1010

1111
$root.MyService = (function() {
1212

13+
/**
14+
* RPC implementation passed to services performing a service request on network level, i.e. by utilizing http requests or websockets.
15+
* @typedef RPCImpl
16+
* @type {function}
17+
* @param {Method} method Reflected method being called
18+
* @param {Uint8Array} requestData Request data
19+
* @param {RPCCallback} callback Callback function
20+
* @returns {undefined}
21+
*/
22+
23+
/**
24+
* Node-style callback as used by {@link RPCImpl}.
25+
* @typedef RPCCallback
26+
* @type {function}
27+
* @param {?Error} error Error, if any, otherwise `null`
28+
* @param {Uint8Array} [responseData] Response data or `null` to signal end of stream, if there hasn't been an error
29+
* @returns {undefined}
30+
*/
31+
1332
/**
1433
* Constructs a new MyService.
1534
* @exports MyService
1635
* @constructor
17-
* @param {function(function, Uint8Array, function)} rpc RPC implementation
36+
* @param {RPCImpl} rpc RPC implementation
1837
* @param {boolean} [requestDelimited=false] Whether requests are length-delimited
1938
* @param {boolean} [responseDelimited=false] Whether responses are length-delimited
2039
*/
2140
function MyService(rpc, requestDelimited, responseDelimited) {
2241

2342
/**
2443
* RPC implementation.
25-
* @type {function(function, Uint8Array, function)}
44+
* @type {RPCImpl}
2645
*/
2746
this.rpc = rpc;
2847

@@ -39,10 +58,18 @@ $root.MyService = (function() {
3958
this.responseDelimited = Boolean(responseDelimited);
4059
};
4160

61+
/**
62+
* Callback as used by {@link MyService#myMethod}.
63+
* @typedef MyService_myMethod_Callback
64+
* @type {function}
65+
* @param {?Error} error Error, if any
66+
* @param {MyResponse} [response] MyResponse
67+
*/
68+
4269
/**
4370
* Calls MyMethod.
4471
* @param {MyRequest|Object} request MyRequest or plain object
45-
* @param {function(?Error, MyResponse=)} callback Node-style callback called with the error, if any, and MyResponse
72+
* @param {MyService_myMethod_Callback} callback Node-style callback called with the error, if any, and MyResponse
4673
* @returns {undefined}
4774
*/
4875
MyService.prototype["myMethod"] = function myMethod(request, callback) {

0 commit comments

Comments
 (0)