Skip to content

Commit

Permalink
Add a ThenableCall base class for the promise-based unaryCall function
Browse files Browse the repository at this point in the history
  • Loading branch information
stanley-cheung committed Jun 10, 2020
1 parent b80a65f commit 63ebf39
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
5 changes: 3 additions & 2 deletions javascript/net/grpc/web/abstractclientbase.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,18 @@ AbstractClientBase.prototype.rpcCall = function(

/**
* @abstract
* @protected
* @template REQUEST, RESPONSE
* @param {string} method The method to invoke
* @param {REQUEST} requestMessage The request proto
* @param {!Object<string, string>} metadata User defined call metadata
* @param {!MethodDescriptor<REQUEST, RESPONSE>|
* !AbstractClientBase.MethodInfo<REQUEST,RESPONSE>}
* methodDescriptor Information of this RPC method
* @return {!Promise<!RESPONSE>}
* @return {!IThenable <!RESPONSE>}
* A promise that resolves to the response message
*/
AbstractClientBase.prototype.unaryCall = function(
AbstractClientBase.prototype.thenableCall = function(
method, requestMessage, metadata, methodDescriptor) {};


Expand Down
20 changes: 19 additions & 1 deletion javascript/net/grpc/web/grpcwebclientbase.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const ClientReadableStream = goog.require('grpc.web.ClientReadableStream');
const Error = goog.require('grpc.web.Error');
const GrpcWebClientReadableStream = goog.require('grpc.web.GrpcWebClientReadableStream');
const HttpCors = goog.require('goog.net.rpc.HttpCors');
const MethodDescriptor = goog.requireType('grpc.web.MethodDescriptor');
const MethodType = goog.require('grpc.web.MethodType');
const Request = goog.require('grpc.web.Request');
const StatusCode = goog.require('grpc.web.StatusCode');
Expand Down Expand Up @@ -112,7 +113,7 @@ GrpcWebClientBase.prototype.rpcCall = function(
* @override
* @export
*/
GrpcWebClientBase.prototype.unaryCall = function(
GrpcWebClientBase.prototype.thenableCall = function(
method, requestMessage, metadata, methodDescriptor) {
methodDescriptor = AbstractClientBase.ensureMethodDescriptor(
method, requestMessage, MethodType.UNARY, methodDescriptor);
Expand Down Expand Up @@ -144,6 +145,23 @@ GrpcWebClientBase.prototype.unaryCall = function(
return unaryResponse.then((response) => response.getResponseMessage());
};

/**
* @export
* @param {string} method The method to invoke
* @param {REQUEST} requestMessage The request proto
* @param {!Object<string, string>} metadata User defined call metadata
* @param {!MethodDescriptor<REQUEST, RESPONSE>|
* !AbstractClientBase.MethodInfo<REQUEST,RESPONSE>}
* methodDescriptor Information of this RPC method
* @return {!Promise<RESPONSE>}
* @template REQUEST, RESPONSE
*/
GrpcWebClientBase.prototype.unaryCall = function(
method, requestMessage, metadata, methodDescriptor) {
return /** @type {!Promise<RESPONSE>}*/ (
this.thenableCall(method, requestMessage, metadata, methodDescriptor));
};


/**
* @override
Expand Down

0 comments on commit 63ebf39

Please sign in to comment.