@@ -14,6 +14,7 @@ var Type = protobuf.Type,
1414var out = [ ] ;
1515var indent = 0 ;
1616var config = { } ;
17+ var firstService = true ;
1718
1819static_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
6971function 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) {
366368function 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) {" ) ;
0 commit comments