     
     func peerConnection(_ nativePeerConnection: RTCPeerConnection,
                         didChange stateChanged: RTCSignalingState) {
-        let newState = PeerChannelSignalingState(nativeValue: stateChanged)
-        Logger.debug(type: .peerChannel,
-                     message: "changed signaling state to \(newState)")
-        internalState.signalingState = newState
+        Logger.debug(type: .peerChannel,
+                     message: "changed signaling state to \(stateChanged)")
+        internalState.signalingState = stateChanged
     }
     
     func peerConnection(_ nativePeerConnection: RTCPeerConnection,

import Foundation
import WebRTC

private let peerChannelSignalingStateTable: PairTable<PeerChannelSignalingState, RTCSignalingState> =
    PairTable(name: "PeerChannelSignalingState",
              pairs: [(.stable, .stable),
                      (.haveLocalOffer, .haveLocalOffer),
                      (.haveLocalPrAnswer, .haveLocalPrAnswer),
                      (.haveRemoteOffer, .haveRemoteOffer),
                      (.haveRemotePrAnswer, .haveRemotePrAnswer),
                      (.closed, .closed)])

private let iceConnectionStateTable: PairTable<ICEConnectionState, RTCIceConnectionState> =
    PairTable(name: "ICEConnectionState",
              pairs: [(.new, .new),
                      (.checking, .checking),
                      (.connected, .connected),
                      (.completed, .completed),
                      (.failed, .failed),
                      (.disconnected, .disconnected),
                      (.closed, .closed),
                      (.count, .count)])

private let iceGatheringStateTable: PairTable<ICEGatheringState, RTCIceGatheringState> =
    PairTable(name: "ICEGatheringState",
              pairs: [(.new, .new),
                      (.gathering, .gathering),
                      (.complete, .complete)])

enum PeerChannelSignalingState {
    
    case stable
    case haveLocalOffer
    case haveLocalPrAnswer
    case haveRemoteOffer
    case haveRemotePrAnswer
    case closed
    
    init(nativeValue: RTCSignalingState) {
        self = peerChannelSignalingStateTable.left(other: nativeValue)!
    }
    
}

enum ICEConnectionState {
    
    case new
    case checking
    case connected
    case completed
    case failed
    case disconnected
    case closed
    case count
    
    init(nativeValue: RTCIceConnectionState) {
        self = iceConnectionStateTable.left(other: nativeValue)!
    }
    
}

enum ICEGatheringState {
    
    case new
    case gathering
    case complete
    
    init(nativeValue: RTCIceGatheringState) {
        self = iceGatheringStateTable.left(other: nativeValue)!
    }
    
}

class PeerChannelInternalState {
    
    var signalingState: PeerChannelSignalingState {
        didSet { validate() }
    }
    
    var iceConnectionState: ICEConnectionState {
        didSet { validate() }
    }
    
    var iceGatheringState: ICEGatheringState {
        didSet { validate() }
    }
    

    var onCompleteHandler: (() -> Void)?
    var onDisconnectHandler: (() -> Void)?
    
    init(signalingState: RTCSignalingState,
         iceConnectionState: RTCIceConnectionState,
         iceGatheringState: RTCIceGatheringState) {
        self.signalingState = signalingState
        self.iceConnectionState = iceConnectionState
        self.iceGatheringState = iceGatheringState
