@@ -26,6 +26,7 @@ class RNGrpc: RCTEventEmitter {
2626 var grpcKeepaliveTime : Int64 ?
2727 var grpcKeepaliveTimeout : Int64 ?
2828 var calls = [ Int: GrpcCall] ( )
29+ var connection : GRPCChannel ?
2930
3031 deinit {
3132 try ! group. syncShutdownGracefully ( )
@@ -34,11 +35,13 @@ class RNGrpc: RCTEventEmitter {
3435 @objc
3536 public func setInsecure( _ insecure: NSNumber ) {
3637 self . grpcInsecure = insecure. boolValue
38+ self . handleOptionsChange ( )
3739 }
3840
3941 @objc
4042 public func setHost( _ host: String ) {
4143 self . grpcHost = host
44+ self . handleOptionsChange ( )
4245 }
4346
4447 @objc
@@ -47,17 +50,21 @@ class RNGrpc: RCTEventEmitter {
4750 self . grpcCompression = enabled. boolValue
4851 self . grpcCompressorName = compressorName
4952 self . grpcCompressionLimit = Int ( limit ?? " " )
53+ self . handleOptionsChange ( )
54+ }
5055
5156 @objc
5257 public func setKeepalive( _ enabled: NSNumber , time: NSNumber , timeout: NSNumber ) {
5358 self . grpcKeepaliveEnabled = enabled. boolValue
5459 self . grpcKeepaliveTime = time. int64Value
5560 self . grpcKeepaliveTimeout = timeout. int64Value
61+ self . handleOptionsChange ( )
5662 }
5763
5864 @objc
5965 public func setResponseSizeLimit( _ responseSizeLimit: NSNumber ) {
6066 self . grpcResponseSizeLimit = responseSizeLimit. intValue
67+ self . handleOptionsChange ( )
6168 }
6269
6370 @objc
@@ -195,7 +202,7 @@ class RNGrpc: RCTEventEmitter {
195202 type: GRPCCallType ,
196203 path: String ,
197204 headers: NSDictionary ) throws -> GrpcCall {
198- guard let conn = try createConnection ( ) else {
205+ guard let conn = self . connection else {
199206 throw GrpcError . connectionFailure
200207 }
201208
@@ -349,6 +356,15 @@ class RNGrpc: RCTEventEmitter {
349356 return CallOptions ( customMetadata: headers, messageEncoding: encoding)
350357 }
351358
359+ private func handleOptionsChange( ) {
360+ if let conn = self . connection {
361+ let loop = self . group. next ( )
362+ conn. closeGracefully ( deadline: . distantFuture, promise: loop. makePromise ( ) )
363+ }
364+
365+ self . connection = try ? self . createConnection ( )
366+ }
367+
352368 private func createConnection( ) throws -> GRPCChannel ? {
353369 guard let host = self . grpcHost else {
354370 throw GrpcError . invalidHost
0 commit comments