Signal-iOS/Signal/src/util/ProxyConnectionChecker.swift
Jordan Rose 95776b610f ChatConnectionManager: only expose state of identified connection
Nobody was using the unidentified connection, and also we expect the
unidentified connection to be allowed to go idle in a way the
identified one does not.
2024-03-25 14:20:58 -07:00

36 lines
1.3 KiB
Swift

//
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
import SignalServiceKit
enum ProxyConnectionChecker: Dependencies {
static func checkConnectionAndNotify(completion: @escaping (Bool) -> Void) {
var observer: NSObjectProtocol?
func unregisterObserver() {
observer.map { NotificationCenter.default.removeObserver($0) }
observer = nil
}
var hasTransitionedToConnecting = false
// Wait to see if we can establish a websocket connection via the new proxy.
observer = NotificationCenter.default.addObserver(forName: OWSChatConnection.chatConnectionStateDidChange, object: nil, queue: nil) { _ in
switch DependenciesBridge.shared.chatConnectionManager.identifiedConnectionState {
case .closed:
// Ignore closed state until we start connecting, it's expected that old sockets will close
guard hasTransitionedToConnecting else { break }
unregisterObserver()
completion(false)
case .connecting:
hasTransitionedToConnecting = true
case .open:
unregisterObserver()
completion(true)
}
}
}
}