Skip to content

Commit

Permalink
Remove ChannelWrapper (#5145)
Browse files Browse the repository at this point in the history
  • Loading branch information
BoD authored Aug 3, 2023
1 parent fa78387 commit 78d7528
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package com.apollographql.apollo3.network.ws

import com.apollographql.apollo3.api.http.HttpHeader
import com.apollographql.apollo3.exception.ApolloNetworkException
import com.apollographql.apollo3.internal.ChannelWrapper
import com.apollographql.apollo3.network.toNSData
import kotlinx.cinterop.convert
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.Channel
import okio.ByteString
import okio.toByteString
Expand Down Expand Up @@ -61,7 +61,7 @@ actual class DefaultWebSocketEngine(
setHTTPMethod("GET")
}

val messageChannel = ChannelWrapper(Channel<String>(Channel.UNLIMITED))
val messageChannel = Channel<String>(Channel.UNLIMITED)
val isOpen = CompletableDeferred<Boolean>()

val connectionListener = object : WebSocketConnectionListener {
Expand Down Expand Up @@ -95,10 +95,11 @@ actual class DefaultWebSocketEngine(

private class WebSocketConnectionImpl(
val webSocket: NSURLSessionWebSocketTask,
val messageChannel: ChannelWrapper<String>,
val messageChannel: Channel<String>,
) : WebSocketConnection {
init {
messageChannel.setInvokeOnClose {
@OptIn(ExperimentalCoroutinesApi::class)
messageChannel.invokeOnClose {
webSocket.cancelWithCloseCode(
closeCode = CLOSE_NORMAL.convert(),
reason = null
Expand All @@ -112,7 +113,8 @@ private class WebSocketConnectionImpl(
}

override fun send(data: ByteString) {
if (!messageChannel.isClosed) {
@OptIn(ExperimentalCoroutinesApi::class)
if (!messageChannel.isClosedForSend) {
val message = NSURLSessionWebSocketMessage(data.toByteArray().toNSData())
val completionHandler = { error: NSError? ->
if (error != null) handleError(error)
Expand All @@ -122,7 +124,8 @@ private class WebSocketConnectionImpl(
}

override fun send(string: String) {
if (!messageChannel.isClosed) {
@OptIn(ExperimentalCoroutinesApi::class)
if (!messageChannel.isClosedForSend) {
val message = NSURLSessionWebSocketMessage(string)
val completionHandler = { error: NSError? ->
if (error != null) handleError(error)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.apollographql.apollo3.network.ws

import com.apollographql.apollo3.api.http.HttpHeader
import com.apollographql.apollo3.internal.ChannelWrapper
import io.ktor.http.Headers
import io.ktor.http.URLBuilder
import io.ktor.http.URLProtocol
Expand Down Expand Up @@ -34,7 +33,7 @@ actual class DefaultWebSocketEngine : WebSocketEngine {
}
}.build()
val socket = createWebSocket(newUrl.toString(), Headers.build { headers.forEach { append(it.name, it.value) } }).awaitConnection()
val messageChannel = ChannelWrapper(Channel<String>(Channel.UNLIMITED))
val messageChannel = Channel<String>(Channel.UNLIMITED)
socket.onmessage = { messageEvent: MessageEvent ->
val data = messageEvent.data
if (data != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package com.apollographql.apollo3.network.ws

import com.apollographql.apollo3.api.http.HttpHeader
import com.apollographql.apollo3.exception.ApolloWebSocketClosedException
import com.apollographql.apollo3.internal.ChannelWrapper
import com.apollographql.apollo3.network.toOkHttpHeaders
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.Channel
import okhttp3.OkHttpClient
import okhttp3.Request
Expand All @@ -25,7 +25,7 @@ actual class DefaultWebSocketEngine(
url: String,
headers: List<HttpHeader>,
): WebSocketConnection {
val messageChannel = ChannelWrapper(Channel<String>(Channel.UNLIMITED))
val messageChannel = Channel<String>(Channel.UNLIMITED)
val webSocketOpenResult = CompletableDeferred<Unit>()

//println("opening $url")
Expand Down Expand Up @@ -71,7 +71,8 @@ actual class DefaultWebSocketEngine(

webSocketOpenResult.await()

messageChannel.setInvokeOnClose {
@OptIn(ExperimentalCoroutinesApi::class)
messageChannel.invokeOnClose {
// I think this is not necessary. The caller must call [WebSocketConnection.close] in all cases.
// This should either trigger onClose or onFailure which should close the messageChannel
//
Expand Down

0 comments on commit 78d7528

Please sign in to comment.