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

♻️ Refactor session disconnect flow callbacks #4

Open
wants to merge 5 commits into
base: gson-payload-adapter
Choose a base branch
from
Open
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
13 changes: 9 additions & 4 deletions lib/src/main/kotlin/org/walletconnect/Session.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ interface Session {
fun removeCallback(cb: Callback)
fun clearCallbacks()

fun disconnect()

data class FullyQualifiedConfig(
val handshakeTopic: String,
val bridge: String,
Expand Down Expand Up @@ -76,11 +78,13 @@ interface Session {
}

sealed class Status {
data class Connected(val clientId: String, val peerId: String?) : Status()
data class Disconnected(val isSessionDeletionNeeded: Boolean) : Status()
data class Approved(val clientId: String, val peerId: String?) : Status()
data class Connected(val clientId: String) : Status()
data class Disconnected(val cause: Throwable, val code: Int) : Status()
data class ConnectionFailed(val cause: Throwable, val code: Int?) : Status()
data class Approved(val clientId: String) : Status()
object Closed : Status()
data class Error(val throwable: Throwable) : Status()
data class Killed(val sessionId: String, val cause: Throwable?): Status()
}

data class TransportError(override val cause: Throwable) : RuntimeException("Transport exception caused by $cause", cause)
Expand All @@ -103,8 +107,9 @@ interface Session {

sealed class Status {
object Connected : Status()
data class Disconnected(val isSessionDeletionNeeded: Boolean) : Status()
data class Error(val throwable: Throwable) : Status()
data class ConnectionClosed(val cause: Throwable, val code: Int): Status()
data class ConnectionFailed(val cause: Throwable, val code: Int?) : Status()
}

data class Message(
Expand Down
11 changes: 4 additions & 7 deletions lib/src/main/kotlin/org/walletconnect/impls/OkHttpTransport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,16 @@ class OkHttpTransport(

override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
super.onFailure(webSocket, t, response)
statusHandler(Error(t))
disconnected(isSessionDeletionNeeded = false)
socket = null
connected = false
statusHandler(Session.Transport.Status.ConnectionFailed(t, response?.code()))
}

override fun onClosed(webSocket: WebSocket, code: Int, reason: String) {
super.onClosed(webSocket, code, reason)
disconnected(isSessionDeletionNeeded = true)
}

private fun disconnected(isSessionDeletionNeeded: Boolean) {
socket = null
connected = false
statusHandler(Disconnected(isSessionDeletionNeeded))
statusHandler(Session.Transport.Status.ConnectionClosed(Throwable(reason), code))
}

private fun tryExec(block: () -> Unit): Boolean {
Expand Down
19 changes: 12 additions & 7 deletions lib/src/main/kotlin/org/walletconnect/impls/WCSession.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class WCSession(
private var currentKey: String

private var approvedAccounts: List<String>? = null
private var chainId: Long? = null
var chainId: Long? = null
private set
private var handshakeId: Long? = null
private var peerId: String? = null
private var peerMeta: Session.PeerMeta? = null
Expand Down Expand Up @@ -71,6 +72,10 @@ class WCSession(
sessionCallbacks.clear()
}

override fun disconnect() {
internalClose()
}

private fun propagateToCallbacks(action: Session.Callback.() -> Unit) {
sessionCallbacks.forEach {
try { it.action() }
Expand Down Expand Up @@ -109,7 +114,7 @@ class WCSession(
propagateToCallbacks {
onStatus(
if (params.approved){
Session.Status.Approved(clientData.id, peerId)
Session.Status.Approved(clientData.id)
} else{
Session.Status.Closed
}
Expand All @@ -129,7 +134,7 @@ class WCSession(
val params = Session.SessionParams(true, chainId, accounts, clientData).intoMap()
send(Session.MethodCall.Response(handshakeId, params))
storeSession()
propagateToCallbacks { onStatus(Session.Status.Approved(clientData.id, peerId)) }
propagateToCallbacks { onStatus(Session.Status.Approved(clientData.id)) }
}

override fun update(accounts: List<String>, chainId: Long) {
Expand Down Expand Up @@ -177,9 +182,10 @@ class WCSession(
}
propagateToCallbacks {
onStatus(when(status) {
Session.Transport.Status.Connected -> Session.Status.Connected(clientData.id, peerId)
is Session.Transport.Status.Disconnected -> Session.Status.Disconnected(status.isSessionDeletionNeeded)
Session.Transport.Status.Connected -> Session.Status.Connected(clientData.id)
is Session.Transport.Status.Error -> Session.Status.Error(Session.TransportError(status.throwable))
is Session.Transport.Status.ConnectionClosed -> Session.Status.Disconnected(status.cause, status.code)
is Session.Transport.Status.ConnectionFailed -> Session.Status.ConnectionFailed(status.cause, status.code)
})
}
}
Expand Down Expand Up @@ -291,8 +297,7 @@ class WCSession(
}

override fun kill() {
val params = Session.SessionParams(false, null, null, null)
send(Session.MethodCall.SessionUpdate(createCallId(), params))
propagateToCallbacks { onStatus(Session.Status.Killed(sessionId = clientData.id, cause = null)) }
endSession()
}
}
Expand Down