Skip to content

Commit

Permalink
use flag instead of a ref
Browse files Browse the repository at this point in the history
  • Loading branch information
martinbonnin committed Nov 9, 2023
1 parent cdfe8b7 commit 631507b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import okio.Buffer
class AppSyncWsProtocol(
val authorization: suspend () -> Any? = { null },
) : WsProtocol {
override val name = "graphql-ws"
override val name: String
get() = "graphql-ws"

override suspend fun connectionInit(): ClientMessage {
return mapOf("type" to "connection_init").toClientMessage()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ class WebSocketNetworkTransport private constructor(

private val lock = reentrantLock()
/**
* [currentSocket] is set to null as soon as it is disconnected and moved to [previousSocket]
* [currentSocket] is set to null as soon as it is disconnected and moved to [readyToConnect]
* while [reopenWhen] is called to decide if the subscriptions should be retried or not.
* During that time, any new socket is left in a non-started state.
* When [previousSocket] is disposed, it is set to null and at that time [currentSocket] can be started
* When [readyToConnect] is disposed, it is set to null and at that time [currentSocket] can be started
*/
private var currentSocket: SubscribableWebSocket? = null
private var previousSocket: SubscribableWebSocket? = null
private var readyToConnect: Boolean = true

@ApolloExperimental
val isConnected = isConnectedPrivate.asStateFlow()
Expand All @@ -66,15 +66,15 @@ class WebSocketNetworkTransport private constructor(
isConnectedPrivate.value = false

lock.withLock {
previousSocket = currentSocket
readyToConnect = false
currentSocket = null
}
}

private fun onWebSocketDisposed() {
lock.withLock {
if (previousSocket != null) {
previousSocket = null
if (!readyToConnect) {
readyToConnect = true
if (currentSocket != null) {
currentSocket!!.connect()
}
Expand Down Expand Up @@ -120,7 +120,7 @@ class WebSocketNetworkTransport private constructor(
connectionAcknowledgeTimeoutMillis = connectionAcknowledgeTimeoutMillis
)
}
if (previousSocket == null) {
if (readyToConnect) {
currentSocket!!.connect()
}
currentSocket!!.startOperation(newRequest, operationListener)
Expand Down

0 comments on commit 631507b

Please sign in to comment.