Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new willReconnect method to Client and Server delegates #125

Merged
merged 3 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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