mirror of https://github.com/grpc/grpc-web.git
				
				
				
			Refactor ClientUnaryCallImpl
This commit is contained in:
		
							parent
							
								
									0599c38922
								
							
						
					
					
						commit
						68c1810a37
					
				|  | @ -60,6 +60,17 @@ closure_js_library( | ||||||
|     visibility = ["//visibility:public"], |     visibility = ["//visibility:public"], | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
|  | closure_js_library( | ||||||
|  |     name = "clientunarycallimpl", | ||||||
|  |     srcs = [ | ||||||
|  |         "clientunarycallimpl.js", | ||||||
|  |     ], | ||||||
|  |     visibility = ["//visibility:public"], | ||||||
|  |     deps = [ | ||||||
|  |         ":clientreadablestream", | ||||||
|  |     ], | ||||||
|  | ) | ||||||
|  | 
 | ||||||
| closure_js_library( | closure_js_library( | ||||||
|     name = "error", |     name = "error", | ||||||
|     srcs = [ |     srcs = [ | ||||||
|  | @ -96,6 +107,7 @@ closure_js_library( | ||||||
|     deps = [ |     deps = [ | ||||||
|         ":abstractclientbase", |         ":abstractclientbase", | ||||||
|         ":clientreadablestream", |         ":clientreadablestream", | ||||||
|  |         ":clientunarycallimpl", | ||||||
|         ":error", |         ":error", | ||||||
|         ":grpcwebclientreadablestream", |         ":grpcwebclientreadablestream", | ||||||
|         ":interceptor", |         ":interceptor", | ||||||
|  |  | ||||||
|  | @ -0,0 +1,51 @@ | ||||||
|  | /** | ||||||
|  |  * @fileoverview This class handles ClientReadableStream returned by unary | ||||||
|  |  * calls. | ||||||
|  |  */ | ||||||
|  | 
 | ||||||
|  | goog.module('grpc.web.ClientUnaryCallImpl'); | ||||||
|  | 
 | ||||||
|  | goog.module.declareLegacyNamespace(); | ||||||
|  | 
 | ||||||
|  | const ClientReadableStream = goog.require('grpc.web.ClientReadableStream'); | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * @implements {ClientReadableStream<RESPONSE>} | ||||||
|  |  * @template RESPONSE | ||||||
|  |  */ | ||||||
|  | class ClientUnaryCallImpl { | ||||||
|  |   /** | ||||||
|  |    * @param {!ClientReadableStream<RESPONSE>} stream | ||||||
|  |    */ | ||||||
|  |   constructor(stream) { | ||||||
|  |     this.stream = stream; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * @override | ||||||
|  |    */ | ||||||
|  |   on(eventType, callback) { | ||||||
|  |     if (eventType == 'data' || eventType == 'error') { | ||||||
|  |       // unary call responses and errors should be handled by the main
 | ||||||
|  |       // (err, resp) => ... callback
 | ||||||
|  |       return this; | ||||||
|  |     } | ||||||
|  |     return this.stream.on(eventType, callback); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * @override | ||||||
|  |    */ | ||||||
|  |   removeListener(eventType, callback) { | ||||||
|  |     return this.stream.removeListener(eventType, callback); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * @override | ||||||
|  |    */ | ||||||
|  |   cancel() { | ||||||
|  |     this.stream.cancel(); | ||||||
|  |   } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | exports = ClientUnaryCallImpl; | ||||||
|  | @ -30,6 +30,7 @@ goog.module.declareLegacyNamespace(); | ||||||
| 
 | 
 | ||||||
| const AbstractClientBase = goog.require('grpc.web.AbstractClientBase'); | const AbstractClientBase = goog.require('grpc.web.AbstractClientBase'); | ||||||
| const ClientReadableStream = goog.require('grpc.web.ClientReadableStream'); | const ClientReadableStream = goog.require('grpc.web.ClientReadableStream'); | ||||||
|  | const ClientUnaryCallImpl = goog.require('grpc.web.ClientUnaryCallImpl'); | ||||||
| const Error = goog.require('grpc.web.Error'); | const Error = goog.require('grpc.web.Error'); | ||||||
| const GrpcWebClientReadableStream = goog.require('grpc.web.GrpcWebClientReadableStream'); | const GrpcWebClientReadableStream = goog.require('grpc.web.GrpcWebClientReadableStream'); | ||||||
| const HttpCors = goog.require('goog.net.rpc.HttpCors'); | const HttpCors = goog.require('goog.net.rpc.HttpCors'); | ||||||
|  | @ -95,45 +96,6 @@ class GrpcWebClientBase { | ||||||
|    * @export |    * @export | ||||||
|    */ |    */ | ||||||
|   rpcCall(method, requestMessage, metadata, methodDescriptor, callback) { |   rpcCall(method, requestMessage, metadata, methodDescriptor, callback) { | ||||||
|     /** |  | ||||||
|      * @implements {ClientReadableStream} |  | ||||||
|      */ |  | ||||||
|     class ClientUnaryCallImpl { |  | ||||||
|       /** |  | ||||||
|        * @param {!ClientReadableStream<RESPONSE>} stream |  | ||||||
|        * @template RESPONSE |  | ||||||
|        */ |  | ||||||
|       constructor(stream) { |  | ||||||
|         this.stream = stream; |  | ||||||
|       } |  | ||||||
| 
 |  | ||||||
|       /** |  | ||||||
|        * @override |  | ||||||
|        */ |  | ||||||
|       on(eventType, callback) { |  | ||||||
|         if (eventType == 'data' || eventType == 'error') { |  | ||||||
|           // unary call responses and errors should be handled by the main
 |  | ||||||
|           // (err, resp) => ... callback
 |  | ||||||
|           return this; |  | ||||||
|         } |  | ||||||
|         return this.stream.on(eventType, callback); |  | ||||||
|       } |  | ||||||
| 
 |  | ||||||
|       /** |  | ||||||
|        * @override |  | ||||||
|        */ |  | ||||||
|       removeListener(eventType, callback) { |  | ||||||
|         return this.stream.removeListener(eventType, callback); |  | ||||||
|       } |  | ||||||
| 
 |  | ||||||
|       /** |  | ||||||
|        * @override |  | ||||||
|        */ |  | ||||||
|       cancel() { |  | ||||||
|         this.stream.cancel(); |  | ||||||
|       } |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     methodDescriptor = AbstractClientBase.ensureMethodDescriptor( |     methodDescriptor = AbstractClientBase.ensureMethodDescriptor( | ||||||
|         method, requestMessage, MethodType.UNARY, methodDescriptor); |         method, requestMessage, MethodType.UNARY, methodDescriptor); | ||||||
|     var hostname = AbstractClientBase.getHostname(method, methodDescriptor); |     var hostname = AbstractClientBase.getHostname(method, methodDescriptor); | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue