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 2 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
10 changes: 10 additions & 0 deletions Sources/PublicInterface/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ public protocol ClientDelegate: AnyObject {
func client(_ client: Client, didConnect session: Session)
func client(_ client: Client, didDisconnect session: Session)
func client(_ client: Client, didUpdate session: Session)
func client(_ client: Client, willReconnect session: Session)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you want to move it to a separate Client2Delegate?

I don't remember the specifics (why I didn't go for the change to the ClientDelegate + default implementation), but the reason was to not break any source code that would update the library version.

I'll double check this anyway during review.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a possibility too, I didn't have the latest changes when coding this new method. Although from a pure aesthetics point of view I kinda like it better in the original delegate protocol 😀 Up to you, really.

Copy link
Collaborator

@DmitryBespalov DmitryBespalov Jul 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I have checked out the branch in my main project, and the build fails because my existing class does not conform to protocol ServerDelegate now.

Please move this method to the Client2Delegate protocol, and the same for the ServerDelegate - to the ServerDelegateV2

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, done!

}

extension ClientDelegate {
// Default implementation, override if actually needed
func client(_ client: Client, willReconnect session: Session) { }
}

public protocol Client2Delegate: ClientDelegate {
Expand Down Expand Up @@ -307,6 +313,10 @@ public class Client: WalletConnect {
delegate?.client(self, didDisconnect: session)
}

override func willReconnect(_ session: Session) {
delegate?.client(self, willReconnect: session)
}

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

Expand Down
13 changes: 13 additions & 0 deletions Sources/PublicInterface/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ public protocol ServerDelegate: AnyObject {

/// Called only when the session is updated with intention of the dAppt.
func server(_ server: Server, didUpdate 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)
}

extension ServerDelegate {
// Default implementation, override if actually needed
func server(_ server: Server, willReconnect session: Session) { }
}

public protocol ServerDelegateV2: ServerDelegate {
Expand Down Expand Up @@ -141,6 +150,10 @@ open class Server: WalletConnect {
delegate?.server(self, didDisconnect: session)
}

override func willReconnect(_ session: Session) {
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