Browse Source

minor improvements to CompletionHandler

Tim Burks 9 years ago
parent
commit
de3be501da

+ 2 - 1
Packages/gRPC/Sources/Call.swift

@@ -108,6 +108,7 @@ public struct CallResult {
 }
 
 public typealias CallCompletion = (CallResult) throws -> Void
+
 public typealias SendMessageCompletion = () -> Void
 
 /// A gRPC API call
@@ -151,7 +152,7 @@ public class Call {
   /// - Parameter operations: group of operations to be performed
   /// - Returns: the result of initiating the call
   func perform(_ operations: OperationGroup) throws -> Void {
-    completionQueue.operationGroups[operations.tag] = operations
+    completionQueue.register(operations)
     let mutex = CallLock.sharedInstance.mutex
     mutex.lock()
     let error = cgrpc_call_perform(underlyingCall, operations.underlyingOperations, operations.tag)

+ 2 - 2
Packages/gRPC/Sources/Client.swift

@@ -83,7 +83,7 @@ public class Client {
   /// - Parameter timeout: a timeout value in seconds
   /// - Returns: a Call object that can be used to perform the request
   public func makeCall(host:String, method:String, timeout:TimeInterval) -> Call {
-    let call = cgrpc_client_create_call(underlyingClient, method, host, timeout)!
-    return Call(underlyingCall:call, owned:true, completionQueue:self.completionQueue)
+    let underlyingCall = cgrpc_client_create_call(underlyingClient, method, host, timeout)!
+    return Call(underlyingCall:underlyingCall, owned:true, completionQueue:self.completionQueue)
   }
 }

+ 14 - 5
Packages/gRPC/Sources/CompletionQueue.swift

@@ -35,13 +35,14 @@
 #endif
 import Foundation
 
+/// A type indicating the kind of event returned by the completion queue
 internal enum CompletionType {
   case queueShutdown
   case queueTimeout
   case complete
   case unknown
 
-  static func completionType(grpcCompletionType value: grpc_completion_type) -> CompletionType {
+  fileprivate static func completionType(_ value: grpc_completion_type) -> CompletionType {
     switch(value) {
     case GRPC_QUEUE_SHUTDOWN:
       return .queueShutdown
@@ -55,13 +56,14 @@ internal enum CompletionType {
   }
 }
 
+/// An event that is returned by the completion queue
 internal struct CompletionQueueEvent {
   internal var type: CompletionType
   internal var success: Int32
   internal var tag: Int64
 
-  init(_ event: grpc_event) {
-    type = CompletionType.completionType(grpcCompletionType: event.type)
+  internal init(_ event: grpc_event) {
+    type = CompletionType.completionType(event.type)
     success = event.success
     tag = cgrpc_event_tag(event)
   }
@@ -77,13 +79,13 @@ internal class CompletionQueue {
   private var underlyingCompletionQueue : UnsafeMutableRawPointer
 
   /// Operation groups that are awaiting completion, keyed by tag
-  internal var operationGroups : [Int64 : OperationGroup] = [:]
+  private var operationGroups : [Int64 : OperationGroup] = [:]
 
   /// Initializes a CompletionQueue
   ///
   /// - Parameter cq: the underlying C representation
   init(underlyingCompletionQueue: UnsafeMutableRawPointer) {
-    // The underlying completion queue NOT OWNED by this class, so we don't dealloc it in a deinit
+    // The underlying completion queue is NOT OWNED by this class, so we don't dealloc it in a deinit
     self.underlyingCompletionQueue = underlyingCompletionQueue
   }
 
@@ -96,6 +98,13 @@ internal class CompletionQueue {
     return CompletionQueueEvent(event)
   }
 
+  /// Register an operation group for handling upon completion
+  ///
+  /// - Parameter operationGroup: the operation group to handle
+  internal func register(_ operationGroup:OperationGroup) -> Void {
+    operationGroups[operationGroup.tag] = operationGroup
+  }
+
   /// Runs a completion queue and call a completion handler when finished
   ///
   /// - Parameter: a completion handler that is called when the queue stops running