Reconnect WC interactors when network appears

This commit is contained in:
Ivan Grachyov 2021-07-11 18:47:51 +03:00
parent 45bdb393bc
commit 6be425a67f

View File

@ -6,10 +6,19 @@ import WalletConnect
class WalletConnect {
private let sessionStorage = SessionStorage.shared
private let networkMonitor = NetworkMonitor.shared
static let shared = WalletConnect()
private init() {}
private init() {
NotificationCenter.default.addObserver(self, selector: #selector(connectionAppeared), name: .connectionAppeared, object: nil)
}
deinit {
NotificationCenter.default.removeObserver(self)
}
private var interactors = [WCInteractor]()
private var interactorsPendingReconnection = [String: WCInteractor]()
private var peers = [String: WCPeerMeta]()
func sessionWithLink(_ link: String) -> WCSession? {
@ -42,6 +51,20 @@ class WalletConnect {
}
}
@objc private func connectionAppeared() {
if !interactorsPendingReconnection.isEmpty {
reconnectPendingInteractors()
}
}
private func reconnectPendingInteractors() {
let pending = interactorsPendingReconnection.values
interactorsPendingReconnection = [:]
pending.forEach {
$0.resume()
}
}
private func didDisconnect(interactor: WCInteractor) {
if sessionStorage.shouldReconnect(interactor: interactor) {
reconnectWhenPossible(interactor: interactor)
@ -92,8 +115,13 @@ class WalletConnect {
}
private func reconnectWhenPossible(interactor: WCInteractor) {
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(10)) { [weak interactor] in
interactor?.resume() // TODO: reconnect when appropriate.
DispatchQueue.main.async { [weak self] in
self?.interactorsPendingReconnection[interactor.clientId] = interactor
if self?.interactorsPendingReconnection.count == 1 && self?.networkMonitor.hasConnection == true {
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(7)) {
self?.reconnectPendingInteractors()
}
}
}
}