Move ConnectivityState to new file & update initializer
Move `ConnectivityState` to its own file since this isn't a very small entity
Change the `static func connectivityState` to an initializer
Remove a redundant case check for `queueTimeout`
MODIFY
Sources/SwiftGRPC/Core/Channel.swift
Sources/SwiftGRPC/Core/Channel.swift
@@ -120,7 +120,7 @@ public class Channel {
/// - Parameter tryToConnect: boolean value to indicate if should try to connect if channel's connectivity state is idle
/// - Returns: a ConnectivityState value representing the current connectivity state of the channel
public func connectivityState(tryToConnect: Bool = false) -> ConnectivityState {
- return ConnectivityState.connectivityState(cgrpc_channel_check_connectivity_state(underlyingChannel, tryToConnect ? 1 : 0))
+ return ConnectivityState(cgrpc_channel_check_connectivity_state(underlyingChannel, tryToConnect ? 1 : 0))
}
/// Subscribe to connectivity state changes
@@ -175,17 +175,16 @@ private extension Channel {
switch event.type {
case .complete:
- let newState = ConnectivityState.connectivityState(cgrpc_channel_check_connectivity_state(self.underlyingChannel, 0))
+ let newState = ConnectivityState(cgrpc_channel_check_connectivity_state(self.underlyingChannel, 0))
if newState != self.lastState {
self.callback(newState)
}
-
self.lastState = newState
- case .queueTimeout:
- continue
+
case .queueShutdown:
return
+
default:
continue
}
@@ -201,60 +200,3 @@ private extension Channel {
}
}
}
-
-extension Channel {
- public enum ConnectivityState {
- /// Channel has just been initialized
- case initialized
- /// Channel is idle
- case idle
- /// Channel is connecting
- case connecting
- /// Channel is ready for work
- case ready
- /// Channel has seen a failure but expects to recover
- case transientFailure
- /// Channel has seen a failure that it cannot recover from
- case shutdown
- /// Channel connectivity state is unknown
- case unknown
-
- fileprivate static func connectivityState(_ value: grpc_connectivity_state) -> ConnectivityState {
- switch value {
- case GRPC_CHANNEL_INIT:
- return .initialized
- case GRPC_CHANNEL_IDLE:
- return .idle
- case GRPC_CHANNEL_CONNECTING:
- return .connecting
- case GRPC_CHANNEL_READY:
- return .ready
- case GRPC_CHANNEL_TRANSIENT_FAILURE:
- return .transientFailure
- case GRPC_CHANNEL_SHUTDOWN:
- return .shutdown
- default:
- return .unknown
- }
- }
-
- fileprivate var underlyingState: grpc_connectivity_state? {
- switch self {
- case .initialized:
- return GRPC_CHANNEL_INIT
- case .idle:
- return GRPC_CHANNEL_IDLE
- case .connecting:
- return GRPC_CHANNEL_CONNECTING
- case .ready:
- return GRPC_CHANNEL_READY
- case .transientFailure:
- return GRPC_CHANNEL_TRANSIENT_FAILURE
- case .shutdown:
- return GRPC_CHANNEL_SHUTDOWN
- default:
- return nil
- }
- }
- }
-}

ADD
Sources/SwiftGRPC/Core/ChannelConnectivityState.swift
+/*
+ * Copyright 2016, gRPC Authors All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#if SWIFT_PACKAGE
+import CgRPC
+#endif
+
+extension Channel {
+ /// The connectivity state of a given gRPC channel.
+ public enum ConnectivityState {
+ /// Channel has just been initialized
+ case initialized
+ /// Channel is idle
+ case idle
+ /// Channel is connecting
+ case connecting
+ /// Channel is ready for work
+ case ready
+ /// Channel has seen a failure but expects to recover
+ case transientFailure
+ /// Channel has seen a failure that it cannot recover from
+ case shutdown
+ /// Channel connectivity state is unknown
+ case unknown
+
+ init(_ underlyingState: grpc_connectivity_state) {
+ switch underlyingState {
+ case GRPC_CHANNEL_INIT:
+ self = .initialized
+ case GRPC_CHANNEL_IDLE:
+ self = .idle
+ case GRPC_CHANNEL_CONNECTING:
+ self = .connecting
+ case GRPC_CHANNEL_READY:
+ self = .ready
+ case GRPC_CHANNEL_TRANSIENT_FAILURE:
+ self = .transientFailure
+ case GRPC_CHANNEL_SHUTDOWN:
+ self = .shutdown
+ default:
+ self = .unknown
+ }
+ }
+
+ var underlyingState: grpc_connectivity_state? {
+ switch self {
+ case .initialized:
+ return GRPC_CHANNEL_INIT
+ case .idle:
+ return GRPC_CHANNEL_IDLE
+ case .connecting:
+ return GRPC_CHANNEL_CONNECTING
+ case .ready:
+ return GRPC_CHANNEL_READY
+ case .transientFailure:
+ return GRPC_CHANNEL_TRANSIENT_FAILURE
+ case .shutdown:
+ return GRPC_CHANNEL_SHUTDOWN
+ default:
+ return nil
+ }
+ }
+ }
+}

Move ConnectivityState to new file & update initializer (#376)

- Move `ConnectivityState` to its own file since this isn't a very small entity
- Change the `static func connectivityState` to an initializer
- Remove a redundant case check for `queueTimeout`