Skip to content

Commit 78d7528

Browse files
authored
Remove ChannelWrapper (#5145)
1 parent fa78387 commit 78d7528

File tree

4 files changed

+14
-46
lines changed

4 files changed

+14
-46
lines changed

libraries/apollo-runtime/src/appleMain/kotlin/com/apollographql/apollo3/network/ws/NSURLSessionWebSocketEngine.kt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package com.apollographql.apollo3.network.ws
22

33
import com.apollographql.apollo3.api.http.HttpHeader
44
import com.apollographql.apollo3.exception.ApolloNetworkException
5-
import com.apollographql.apollo3.internal.ChannelWrapper
65
import com.apollographql.apollo3.network.toNSData
76
import kotlinx.cinterop.convert
87
import kotlinx.coroutines.CompletableDeferred
8+
import kotlinx.coroutines.ExperimentalCoroutinesApi
99
import kotlinx.coroutines.channels.Channel
1010
import okio.ByteString
1111
import okio.toByteString
@@ -61,7 +61,7 @@ actual class DefaultWebSocketEngine(
6161
setHTTPMethod("GET")
6262
}
6363

64-
val messageChannel = ChannelWrapper(Channel<String>(Channel.UNLIMITED))
64+
val messageChannel = Channel<String>(Channel.UNLIMITED)
6565
val isOpen = CompletableDeferred<Boolean>()
6666

6767
val connectionListener = object : WebSocketConnectionListener {
@@ -95,10 +95,11 @@ actual class DefaultWebSocketEngine(
9595

9696
private class WebSocketConnectionImpl(
9797
val webSocket: NSURLSessionWebSocketTask,
98-
val messageChannel: ChannelWrapper<String>,
98+
val messageChannel: Channel<String>,
9999
) : WebSocketConnection {
100100
init {
101-
messageChannel.setInvokeOnClose {
101+
@OptIn(ExperimentalCoroutinesApi::class)
102+
messageChannel.invokeOnClose {
102103
webSocket.cancelWithCloseCode(
103104
closeCode = CLOSE_NORMAL.convert(),
104105
reason = null
@@ -112,7 +113,8 @@ private class WebSocketConnectionImpl(
112113
}
113114

114115
override fun send(data: ByteString) {
115-
if (!messageChannel.isClosed) {
116+
@OptIn(ExperimentalCoroutinesApi::class)
117+
if (!messageChannel.isClosedForSend) {
116118
val message = NSURLSessionWebSocketMessage(data.toByteArray().toNSData())
117119
val completionHandler = { error: NSError? ->
118120
if (error != null) handleError(error)
@@ -122,7 +124,8 @@ private class WebSocketConnectionImpl(
122124
}
123125

124126
override fun send(string: String) {
125-
if (!messageChannel.isClosed) {
127+
@OptIn(ExperimentalCoroutinesApi::class)
128+
if (!messageChannel.isClosedForSend) {
126129
val message = NSURLSessionWebSocketMessage(string)
127130
val completionHandler = { error: NSError? ->
128131
if (error != null) handleError(error)

libraries/apollo-runtime/src/commonMain/kotlin/com/apollographql/apollo3/internal/ChannelWrapper.kt

Lines changed: 0 additions & 35 deletions
This file was deleted.

libraries/apollo-runtime/src/jsMain/kotlin/com/apollographql/apollo3/network/ws/JsWebSocketEngine.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.apollographql.apollo3.network.ws
22

33
import com.apollographql.apollo3.api.http.HttpHeader
4-
import com.apollographql.apollo3.internal.ChannelWrapper
54
import io.ktor.http.Headers
65
import io.ktor.http.URLBuilder
76
import io.ktor.http.URLProtocol
@@ -34,7 +33,7 @@ actual class DefaultWebSocketEngine : WebSocketEngine {
3433
}
3534
}.build()
3635
val socket = createWebSocket(newUrl.toString(), Headers.build { headers.forEach { append(it.name, it.value) } }).awaitConnection()
37-
val messageChannel = ChannelWrapper(Channel<String>(Channel.UNLIMITED))
36+
val messageChannel = Channel<String>(Channel.UNLIMITED)
3837
socket.onmessage = { messageEvent: MessageEvent ->
3938
val data = messageEvent.data
4039
if (data != null) {

libraries/apollo-runtime/src/jvmMain/kotlin/com/apollographql/apollo3/network/ws/OkHttpWebSocketEngine.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package com.apollographql.apollo3.network.ws
22

33
import com.apollographql.apollo3.api.http.HttpHeader
44
import com.apollographql.apollo3.exception.ApolloWebSocketClosedException
5-
import com.apollographql.apollo3.internal.ChannelWrapper
65
import com.apollographql.apollo3.network.toOkHttpHeaders
76
import kotlinx.coroutines.CompletableDeferred
7+
import kotlinx.coroutines.ExperimentalCoroutinesApi
88
import kotlinx.coroutines.channels.Channel
99
import okhttp3.OkHttpClient
1010
import okhttp3.Request
@@ -25,7 +25,7 @@ actual class DefaultWebSocketEngine(
2525
url: String,
2626
headers: List<HttpHeader>,
2727
): WebSocketConnection {
28-
val messageChannel = ChannelWrapper(Channel<String>(Channel.UNLIMITED))
28+
val messageChannel = Channel<String>(Channel.UNLIMITED)
2929
val webSocketOpenResult = CompletableDeferred<Unit>()
3030

3131
//println("opening $url")
@@ -71,7 +71,8 @@ actual class DefaultWebSocketEngine(
7171

7272
webSocketOpenResult.await()
7373

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

0 commit comments

Comments
 (0)