Skip to content

Commit

Permalink
Merge pull request #125 from oscahie/oscar/willReconnect
Browse files Browse the repository at this point in the history
Added new `willReconnect` method to Client and Server delegates
  • Loading branch information
Dmitry Bespalov authored Jul 12, 2022
2 parents 030c988 + 35a214f commit 31e350d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Sources/PublicInterface/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public protocol ClientDelegate: AnyObject {

public protocol Client2Delegate: ClientDelegate {
func client(_ client: Client, dappInfoForUrl url: WCURL) -> Session.DAppInfo?
func client(_ client: Client, willReconnect session: Session)
}

public class Client: WalletConnect {
Expand Down Expand Up @@ -307,6 +308,12 @@ public class Client: WalletConnect {
delegate?.client(self, didDisconnect: session)
}

override func willReconnect(_ session: Session) {
if let delegate = delegate as? Client2Delegate {
delegate.client(self, willReconnect: session)
}
}

/// Thread-safe collection of client reponses
private class Responses {

Expand Down
10 changes: 10 additions & 0 deletions Sources/PublicInterface/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public protocol ServerDelegateV2: ServerDelegate {
/// - requestId: connection request's id. Can be Int, Double, or String
/// - session: the session to create. Contains dapp info received in the connection request.
func server(_ server: Server, didReceiveConnectionRequest requestId: RequestID, for session: Session)

/// Called when the session is being reconnected as part of the retry mechanism after the connection
/// has been lost due to e.g. bad connectivity.
func server(_ server: Server, willReconnect session: Session)
}

open class Server: WalletConnect {
Expand Down Expand Up @@ -141,6 +145,12 @@ open class Server: WalletConnect {
delegate?.server(self, didDisconnect: session)
}

override func willReconnect(_ session: Session) {
if let delegate = delegate as? ServerDelegateV2 {
delegate.server(self, willReconnect: session)
}
}

/// Sends response for the create session request.
/// Use this method together with `ServerDelegate.server(_ server:didReceiveConnectionRequest:for:)`.
///
Expand Down
6 changes: 5 additions & 1 deletion Sources/PublicInterface/WalletConnect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ open class WalletConnect {
}
// if a session was not initiated by the wallet or the dApp to disconnect, try to reconnect it.
guard communicator.pendingDisconnectSession(by: url) != nil else {
// TODO: should we notify delegate that we try to reconnect?
LogService.shared.log("WC: trying to reconnect session by url: \(url.bridgeURL.absoluteString)")
willReconnect(session)
try! reconnect(to: session)
return
}
Expand Down Expand Up @@ -125,6 +125,10 @@ open class WalletConnect {
preconditionFailure("Should be implemented in subclasses")
}

func willReconnect(_ session: Session) {
preconditionFailure("Should be implemented in subclasses")
}

func log(_ request: Request) {
guard let text = try? request.json().string else { return }
LogService.shared.log("WC: <== [request] \(text)")
Expand Down

0 comments on commit 31e350d

Please sign in to comment.