@@ -528,7 +528,7 @@ function buildService(ref, service) {
528528 "RPC implementation passed to services performing a service request on network level, i.e. by utilizing http requests or websockets." ,
529529 "@typedef RPCImpl" ,
530530 "@type {function}" ,
531- "@param {$protobuf.Method } method Reflected method being called" ,
531+ "@param {function } method Method being called" ,
532532 "@param {Uint8Array} requestData Request data" ,
533533 "@param {RPCCallback} callback Callback function" ,
534534 "@returns {undefined}"
@@ -550,13 +550,15 @@ function buildService(ref, service) {
550550 "Constructs a new " + service . name + " service." ,
551551 service . comment ? "@classdesc " + service . comment : null ,
552552 "@exports " + fullName ,
553+ "@extends $protobuf.util.EventEmitter" ,
553554 "@constructor" ,
554555 "@param {RPCImpl} rpc RPC implementation" ,
555556 "@param {boolean} [requestDelimited=false] Whether requests are length-delimited" ,
556- "@param {boolean} [responseDelimited=false] Whether responses are length-delimited"
557+ "@param {boolean} [responseDelimited=false] Whether responses are length-delimited" ,
557558 ] ) ;
558559 push ( "function " + name ( service . name ) + "(rpc, requestDelimited, responseDelimited) {" ) ;
559560 ++ indent ;
561+ push ( "$util.EventEmitter.call(this);" ) ;
560562
561563 push ( "" ) ;
562564 pushComment ( [
@@ -578,6 +580,24 @@ function buildService(ref, service) {
578580 push ( "this.responseDelimited = Boolean(responseDelimited);" ) ;
579581 -- indent ;
580582 push ( "}" ) ;
583+ push ( "" ) ;
584+ push ( "(" + name ( service . name ) + ".prototype = Object.create($util.EventEmitter.prototype)).constructor = " + name ( service . name ) + ";" ) ;
585+
586+ if ( config . create ) {
587+ push ( "" ) ;
588+ pushComment ( [
589+ "Creates a runtime service using the specified rpc implementation." ,
590+ "@param {RPCImpl} rpcImpl RPC implementation" ,
591+ "@param {boolean} [requestDelimited=false] Whether requests are length-delimited" ,
592+ "@param {boolean} [responseDelimited=false] Whether responses are length-delimited" ,
593+ "@returns {" + name ( service . name ) + "} RPC service. Useful where requests and/or responses are streamed."
594+ ] ) ;
595+ push ( name ( service . name ) + ".create = function create(rpcImpl, requestDelimited, responseDelimited) {" ) ;
596+ ++ indent ;
597+ push ( "return new this(rpcImpl, requestDelimited, responseDelimited);" ) ;
598+ -- indent ;
599+ push ( "};" ) ;
600+ }
581601
582602 service . methodsArray . forEach ( function ( method ) {
583603 method . resolve ( ) ;
@@ -589,7 +609,7 @@ function buildService(ref, service) {
589609 "@typedef " + cbName ,
590610 "@type {function}" ,
591611 "@param {?Error} error Error, if any" ,
592- "@param {" + method . resolvedResponseType . fullName . substring ( 1 ) + "} [response] " + method . resolvedResponseType . name
612+ "@param {" + method . resolvedResponseType . fullName . substring ( 1 ) + "} [response] " + method . resolvedResponseType . name + " or `null` if the service has been terminated server-side"
593613 ] ) ;
594614 push ( "" ) ;
595615 pushComment ( [
@@ -600,42 +620,69 @@ function buildService(ref, service) {
600620 ] ) ;
601621 push ( name ( service . name ) + ".prototype" + util . safeProp ( lcName ) + " = function " + name ( lcName ) + "(request, callback) {" ) ;
602622 ++ indent ;
603- push ( "var requestData;" ) ;
604- push ( "try {" ) ;
623+ push ( "if (!request)" ) ;
605624 ++ indent ;
606- push ( "requestData = (this.requestDelimited ? $root" + name ( method . resolvedRequestType . fullName ) + ".encodeDelimited( request) : $root" + name ( method . resolvedRequestType . fullName ) + ".encode(request)).finish( );") ;
625+ push ( "throw TypeError(\" request must be specified\" );" ) ;
607626 -- indent ;
608- push ( "} catch (err) { " ) ;
627+ push ( "if (!callback) " ) ;
609628 ++ indent ;
610- push ( "(typeof setImmediate === \"function\" ? setImmediate : setTimeout)(function() { callback(err); });" ) ;
611- push ( "return;" ) ;
629+ push ( "return $util.asPromise(" + name ( lcName ) + ", this, request);" ) ;
612630 -- indent ;
613- push ( "}" ) ;
614- push ( "var self = this;" ) ;
615- push ( "this.rpc(" + name ( lcName ) + ", requestData, function(err, responseData) {" ) ;
631+ push ( "var $self = this;" ) ;
632+ push ( "this.rpc(" + name ( lcName ) + ", (this.requestDelimited" ) ;
633+ ++ indent ;
634+ push ( "? $root" + method . resolvedRequestType . fullName + ".encodeDelimited(request)" ) ;
635+ push ( ": $root" + method . resolvedRequestType . fullName + ".encode(request)" ) ;
636+ -- indent ;
637+ push ( ").finish(), function $rpcCallback(err, response) {" ) ;
616638 ++ indent ;
617639 push ( "if (err) {" ) ;
618640 ++ indent ;
619- push ( "callback( err);" ) ;
620- push ( "return;" ) ;
641+ push ( "$self.emit(\"error\", err, " + name ( lcName ) + " );") ;
642+ push ( "return callback(err) ;" ) ;
621643 -- indent ;
622644 push ( "}" ) ;
623- push ( "var response;" ) ;
624- push ( "try {" ) ;
645+ push ( "if (response === null) {" ) ;
625646 ++ indent ;
626- push ( "response = self.responseDelimited ? $root" + name ( method . resolvedResponseType . fullName ) + ".decodeDelimited(responseData) : $root" + name ( method . resolvedResponseType . fullName ) + ".decode(responseData);" ) ;
647+ push ( "$self.end(true);" ) ;
648+ push ( "return undefined;" ) ;
627649 -- indent ;
628- push ( "} catch (err2) {" ) ;
650+ push ( "}" ) ;
651+ push ( "if (!(response instanceof $root" + method . resolvedResponseType . fullName + ")) {" ) ;
629652 ++ indent ;
630- push ( "callback(err2);" ) ;
631- push ( "return;" ) ;
653+ push ( "try {" ) ;
654+ ++ indent ;
655+ push ( "response = $self.responseDelimited" ) ;
656+ ++ indent ;
657+ push ( "? $root" + method . resolvedResponseType . fullName + ".decodeDelimited(response)" ) ;
658+ push ( ": $root" + method . resolvedResponseType . fullName + ".decode(response);" ) ;
659+ -- indent ;
660+ -- indent ;
661+ push ( "} catch (err2) {" ) ;
662+ ++ indent ;
663+ push ( "$self.emit(\"error\", err2, " + name ( lcName ) + ");" ) ;
664+ push ( "return callback(err2);" ) ;
665+ -- indent ;
666+ push ( "}" ) ;
632667 -- indent ;
633668 push ( "}" ) ;
634- push ( "callback(null, response);" ) ;
669+ push ( "$self.emit(\"data\", response, " + name ( lcName ) + ");" ) ;
670+ push ( "return callback(null, response);" ) ;
635671 -- indent ;
636672 push ( "});" ) ;
673+ push ( "return undefined;" ) ;
637674 -- indent ;
638675 push ( "};" ) ;
676+ if ( config . comments )
677+ push ( "" ) ;
678+ pushComment ( [
679+ method . comment || "Calls " + method . name + "." ,
680+ "@name " + name ( service . name ) + "#" + lcName ,
681+ "@function" ,
682+ "@param {" + method . resolvedRequestType . fullName . substring ( 1 ) + "|Object} request " + method . resolvedRequestType . name + " message or plain object" ,
683+ "@returns {Promise<" + method . resolvedResponseType . fullName . substring ( 1 ) + ">} Promise" ,
684+ "@variation 2"
685+ ] ) ;
639686 } ) ;
640687}
641688
0 commit comments