"use strict";module.exports=Service;var util=require("../util/minimal");function Service(e,t,r){if("function"!=typeof e)throw TypeError("rpcImpl must be a function");util.EventEmitter.call(this),this.rpcImpl=e,this.requestDelimited=!!t,this.responseDelimited=!!r}(Service.prototype=Object.create(util.EventEmitter.prototype)).constructor=Service,Service.prototype.rpcCall=function e(t,r,i,n,o){if(!n)throw TypeError("request must be specified");var u=this;if(!o)return util.asPromise(e,u,t,r,i,n);if(!u.rpcImpl)return void setTimeout(function(){o(Error("already ended"))},0);try{return u.rpcImpl(t,r[u.requestDelimited?"encodeDelimited":"encode"](n).finish(),function(e,r){if(e)return u.emit("error",e,t),o(e);if(null===r)return void u.end(!0);if(!(r instanceof i))try{r=i[u.responseDelimited?"decodeDelimited":"decode"](r)}catch(e){return u.emit("error",e,t),o(e)}return u.emit("data",r,t),o(null,r)})}catch(e){u.emit("error",e,t),setTimeout(function(){o(e)},0);return}},Service.prototype.end=function(e){return this.rpcImpl&&(e||this.rpcImpl(null,null,null),this.rpcImpl=null,this.emit("end").off()),this};