Skip to content

Commit f52d13b

Browse files
authored
Allow to initialize WebSocketEngine lazily (#6290)
See also #5784
1 parent 15f1785 commit f52d13b

File tree

4 files changed

+42
-9
lines changed

4 files changed

+42
-9
lines changed

libraries/apollo-runtime/api/android/apollo-runtime.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ public final class com/apollographql/apollo/network/ws/AppSyncWsProtocol$Factory
354354

355355
public final class com/apollographql/apollo/network/ws/DefaultWebSocketEngine : com/apollographql/apollo/network/ws/WebSocketEngine {
356356
public fun <init> ()V
357+
public fun <init> (Lkotlin/jvm/functions/Function0;)V
357358
public fun <init> (Lokhttp3/WebSocket$Factory;)V
358359
public fun open (Ljava/lang/String;Ljava/util/List;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
359360
}

libraries/apollo-runtime/api/jvm/apollo-runtime.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ public final class com/apollographql/apollo/network/ws/AppSyncWsProtocol$Factory
354354

355355
public final class com/apollographql/apollo/network/ws/DefaultWebSocketEngine : com/apollographql/apollo/network/ws/WebSocketEngine {
356356
public fun <init> ()V
357+
public fun <init> (Lkotlin/jvm/functions/Function0;)V
357358
public fun <init> (Lokhttp3/WebSocket$Factory;)V
358359
public fun open (Ljava/lang/String;Ljava/util/List;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
359360
}

libraries/apollo-runtime/src/jvmCommonMain/kotlin/com/apollographql/apollo/network/websocket/WebSocketEngine.jvm.kt

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ import java.util.concurrent.atomic.AtomicReference
1515
import okhttp3.WebSocket as PlatformWebSocket
1616
import okhttp3.WebSocketListener as PlatformWebSocketListener
1717

18-
internal class JvmWebSocketEngine(private val webSocketFactory: PlatformWebSocket.Factory) : WebSocketEngine {
18+
internal class JvmWebSocketEngine(webSocketFactory: () -> PlatformWebSocket.Factory) : WebSocketEngine {
19+
private val webSocketFactory by lazy { webSocketFactory() }
20+
21+
constructor(webSocketFactory: PlatformWebSocket.Factory) : this({ webSocketFactory })
22+
1923
var closed = false
2024
override fun newWebSocket(url: String, headers: List<HttpHeader>, listener: WebSocketListener): WebSocket {
2125
require(!closed) {
@@ -88,11 +92,11 @@ internal class JvmWebSocket(
8892
}
8993

9094
private fun List<HttpHeader>.toOkHttpHeaders(): Headers =
91-
Headers.Builder().also { headers ->
92-
this.forEach {
93-
headers.add(it.name, it.value)
94-
}
95-
}.build()
95+
Headers.Builder().also { headers ->
96+
this.forEach {
97+
headers.add(it.name, it.value)
98+
}
99+
}.build()
96100

97101
override fun send(data: ByteArray) {
98102
platformWebSocket.get()?.send(data.toByteString())
@@ -110,7 +114,21 @@ internal class JvmWebSocket(
110114
}
111115

112116

113-
actual fun WebSocketEngine(): WebSocketEngine = JvmWebSocketEngine(defaultOkHttpClientBuilder.build())
117+
actual fun WebSocketEngine(): WebSocketEngine = JvmWebSocketEngine { defaultOkHttpClientBuilder.build() }
114118

119+
/**
120+
* Creates a new [WebSocketEngine] from [webSocketFactory]
121+
*
122+
* This factory function accepts a function so that OkHttp is initialized from a background thread
123+
*/
124+
@ApolloExperimental
125+
fun WebSocketEngine(webSocketFactory: () -> PlatformWebSocket.Factory): WebSocketEngine = JvmWebSocketEngine(webSocketFactory)
126+
127+
/**
128+
* Creates a new [WebSocketEngine] from [webSocketFactory]
129+
*
130+
* Prefer using the factory function accepting a function so that OkHttp is initialized from a background thread.
131+
* See https://github.com/square/okhttp/pull/8248
132+
*/
115133
@ApolloExperimental
116134
fun WebSocketEngine(webSocketFactory: PlatformWebSocket.Factory): WebSocketEngine = JvmWebSocketEngine(webSocketFactory)

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,23 @@ import okhttp3.WebSocket
1313
import okhttp3.WebSocketListener
1414
import okio.ByteString
1515

16+
/**
17+
* Creates a new [DefaultWebSocketEngine] from [webSocketFactory]
18+
*
19+
* This constructor accepts a function so that OkHttp is initialized from a background thread
20+
*/
1621
actual class DefaultWebSocketEngine(
17-
private val webSocketFactory: WebSocket.Factory,
22+
webSocketFactory: () -> WebSocket.Factory,
1823
) : WebSocketEngine {
19-
24+
private val webSocketFactory by lazy { webSocketFactory() }
25+
26+
/**
27+
* Creates a new [DefaultWebSocketEngine] from [webSocketFactory]
28+
*
29+
* Prefer using the factory function accepting a function so that OkHttp is initialized from a background thread.
30+
* See https://github.com/square/okhttp/pull/8248
31+
*/
32+
constructor(webSocketFactory: WebSocket.Factory): this({webSocketFactory})
2033
actual constructor() : this(
2134
webSocketFactory = defaultOkHttpClientBuilder.build()
2235
)

0 commit comments

Comments
 (0)