Skip to content

Commit 99e227b

Browse files
authored
Cleanup mpp utils (#5980)
* replace currentTimeMillis -> TimeMark * cleanup mpp-utils * move platform() to testing-support * make compile * fix coroutines test
1 parent d5baf87 commit 99e227b

File tree

37 files changed

+227
-218
lines changed

37 files changed

+227
-218
lines changed

libraries/apollo-mpp-utils/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
# Module apollo-mpp-utils
22

3-
apollo-mpp-utils contains a few utilities for working with multiplatform projects.
3+
apollo-mpp-utils contains a few utilities for working with multiplatform projects.
4+
5+
As of June 2024, it only contains `currentTimeMillis`. In most cases, we should replace that with `kotlin.time.TimeMark` but it's still used in `HttpInfo` as absolute timestamps, and we can't remove it just yet.
Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,4 @@
1-
public final class com/apollographql/apollo3/mpp/Platform : java/lang/Enum {
2-
public static final field Js Lcom/apollographql/apollo3/mpp/Platform;
3-
public static final field Jvm Lcom/apollographql/apollo3/mpp/Platform;
4-
public static final field Native Lcom/apollographql/apollo3/mpp/Platform;
5-
public static final field WasmJs Lcom/apollographql/apollo3/mpp/Platform;
6-
public static fun getEntries ()Lkotlin/enums/EnumEntries;
7-
public static fun valueOf (Ljava/lang/String;)Lcom/apollographql/apollo3/mpp/Platform;
8-
public static fun values ()[Lcom/apollographql/apollo3/mpp/Platform;
9-
}
10-
111
public final class com/apollographql/apollo3/mpp/UtilsKt {
12-
public static final fun currentThreadId ()Ljava/lang/String;
13-
public static final fun currentThreadName ()Ljava/lang/String;
14-
public static final fun currentTimeFormatted ()Ljava/lang/String;
152
public static final fun currentTimeMillis ()J
16-
public static final fun platform ()Lcom/apollographql/apollo3/mpp/Platform;
173
}
184

libraries/apollo-mpp-utils/api/apollo-mpp-utils.klib.api

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,4 @@
66
// - Show declarations: true
77

88
// Library unique name: <com.apollographql.apollo3:apollo-mpp-utils>
9-
final enum class com.apollographql.apollo3.mpp/Platform : kotlin/Enum<com.apollographql.apollo3.mpp/Platform> { // com.apollographql.apollo3.mpp/Platform|null[0]
10-
enum entry Js // com.apollographql.apollo3.mpp/Platform.Js|null[0]
11-
enum entry Jvm // com.apollographql.apollo3.mpp/Platform.Jvm|null[0]
12-
enum entry Native // com.apollographql.apollo3.mpp/Platform.Native|null[0]
13-
enum entry WasmJs // com.apollographql.apollo3.mpp/Platform.WasmJs|null[0]
14-
final fun valueOf(kotlin/String): com.apollographql.apollo3.mpp/Platform // com.apollographql.apollo3.mpp/Platform.valueOf|valueOf#static(kotlin.String){}[0]
15-
final fun values(): kotlin/Array<com.apollographql.apollo3.mpp/Platform> // com.apollographql.apollo3.mpp/Platform.values|values#static(){}[0]
16-
final val entries // com.apollographql.apollo3.mpp/Platform.entries|#static{}entries[0]
17-
final fun <get-entries>(): kotlin.enums/EnumEntries<com.apollographql.apollo3.mpp/Platform> // com.apollographql.apollo3.mpp/Platform.entries.<get-entries>|<get-entries>#static(){}[0]
18-
}
19-
final fun com.apollographql.apollo3.mpp/currentThreadId(): kotlin/String // com.apollographql.apollo3.mpp/currentThreadId|currentThreadId(){}[0]
20-
final fun com.apollographql.apollo3.mpp/currentThreadName(): kotlin/String // com.apollographql.apollo3.mpp/currentThreadName|currentThreadName(){}[0]
21-
final fun com.apollographql.apollo3.mpp/currentTimeFormatted(): kotlin/String // com.apollographql.apollo3.mpp/currentTimeFormatted|currentTimeFormatted(){}[0]
229
final fun com.apollographql.apollo3.mpp/currentTimeMillis(): kotlin/Long // com.apollographql.apollo3.mpp/currentTimeMillis|currentTimeMillis(){}[0]
23-
final fun com.apollographql.apollo3.mpp/platform(): com.apollographql.apollo3.mpp/Platform // com.apollographql.apollo3.mpp/platform|platform(){}[0]
Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,8 @@
11
package com.apollographql.apollo3.mpp
22

33
import platform.Foundation.NSDate
4-
import platform.Foundation.NSDateFormatter
5-
import platform.Foundation.NSThread
64
import platform.Foundation.timeIntervalSince1970
7-
import platform.posix.pthread_self
85

96
actual fun currentTimeMillis(): Long {
107
return (NSDate().timeIntervalSince1970 * 1000).toLong()
118
}
12-
13-
private val nsDateFormatter by lazy { NSDateFormatter().apply { dateFormat = "HH:mm:ss.SSS" } }
14-
15-
actual fun currentTimeFormatted(): String {
16-
return nsDateFormatter.stringFromDate(NSDate())
17-
}
18-
19-
actual fun currentThreadId(): String {
20-
return pthread_self()?.rawValue.toString()
21-
}
22-
23-
actual fun currentThreadName(): String {
24-
return if (NSThread.isMainThread) {
25-
"main"
26-
} else {
27-
currentThreadId()
28-
}
29-
}
30-
31-
actual fun platform() = Platform.Native

libraries/apollo-mpp-utils/src/commonMain/kotlin/com/apollographql/apollo3/mpp/utils.kt

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,7 @@
22

33
package com.apollographql.apollo3.mpp
44

5-
import com.apollographql.apollo3.annotations.ApolloInternal
65
import kotlin.jvm.JvmName
76

87
expect fun currentTimeMillis(): Long
98

10-
/**
11-
* The current time as a human-readable String. Used for debugging.
12-
*/
13-
@ApolloInternal
14-
expect fun currentTimeFormatted(): String
15-
16-
expect fun currentThreadId(): String
17-
18-
/**
19-
* The current thread name ("main" for the main thread). Used for debugging.
20-
*/
21-
@ApolloInternal
22-
expect fun currentThreadName(): String
23-
24-
enum class Platform {
25-
Jvm,
26-
Native,
27-
Js,
28-
WasmJs
29-
}
30-
31-
/**
32-
* The current platform. This is used from tests because Double.toString() doesn't behave the same on JS and other platforms.
33-
* Prefer more specific functions like `assertMainThreadOnNative` when possible instead of checking the platform.
34-
*/
35-
expect fun platform(): Platform
36-
37-
// Helpful for debugging, but not wanted in the final library - uncomment as needed
38-
//fun log(message: String) {
39-
// println("${currentTimeFormatted()} [${currentThreadName()}] $message")
40-
//}

libraries/apollo-mpp-utils/src/jsMain/kotlin/com/apollographql/apollo3/mpp/utils.kt

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,3 @@ actual fun currentTimeMillis(): Long {
77
return Date().getTime().toLong()
88
}
99

10-
actual fun currentTimeFormatted(): String {
11-
return Date().toISOString()
12-
}
13-
14-
actual fun currentThreadId(): String {
15-
return "js"
16-
}
17-
18-
actual fun currentThreadName(): String {
19-
return currentThreadId()
20-
}
21-
22-
actual fun platform() = Platform.Js
23-
24-
/**
25-
* Copyright 2014-2019 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
26-
* From https://github.com/ktorio/ktor/blob/6cd529b2dcedfcfc4ca2af0f62704764e160d7fd/ktor-utils/js/src/io/ktor/util/PlatformUtilsJs.kt#L16
27-
*/
28-
@ApolloInternal
29-
val isNode: Boolean by lazy {
30-
js(
31-
"""
32-
(typeof process !== 'undefined'
33-
&& process.versions != null
34-
&& process.versions.node != null) ||
35-
(typeof window !== 'undefined'
36-
&& typeof window.process !== 'undefined'
37-
&& window.process.versions != null
38-
&& window.process.versions.node != null)
39-
"""
40-
) as Boolean
41-
}
Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,6 @@
11
package com.apollographql.apollo3.mpp
22

3-
import java.text.SimpleDateFormat
4-
import java.util.Locale
5-
63
actual fun currentTimeMillis(): Long {
74
return System.currentTimeMillis()
85
}
96

10-
private val simpleDateFormat by lazy { SimpleDateFormat("HH:mm:ss.SSS", Locale.ROOT) }
11-
12-
actual fun currentTimeFormatted(): String {
13-
return simpleDateFormat.format(currentTimeMillis())
14-
}
15-
16-
actual fun currentThreadId(): String {
17-
return Thread.currentThread().id.toString()
18-
}
19-
20-
actual fun currentThreadName(): String {
21-
return Thread.currentThread().name
22-
}
23-
24-
actual fun platform() = Platform.Jvm

libraries/apollo-mpp-utils/src/wasmJsMain/kotlin/com/apollographql/apollo3/mpp/utils.wasmJs.kt

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,3 @@ actual fun currentTimeMillis(): Long {
55
return currentTimeMillis2().toLong()
66
}
77

8-
actual fun currentTimeFormatted(): String = js("(new Date()).toISOString()")
9-
10-
actual fun currentThreadId(): String {
11-
return "wasm-js"
12-
}
13-
14-
actual fun currentThreadName(): String {
15-
return currentThreadId()
16-
}
17-
18-
actual fun platform() = Platform.WasmJs

libraries/apollo-runtime/src/commonMain/kotlin/com/apollographql/apollo3/network/http/BatchingHttpInterceptor.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import com.apollographql.apollo3.exception.ApolloHttpException
2121
import com.apollographql.apollo3.exception.DefaultApolloException
2222
import com.apollographql.apollo3.exception.JsonDataException
2323
import com.apollographql.apollo3.internal.CloseableSingleThreadDispatcher
24-
import com.apollographql.apollo3.mpp.currentTimeMillis
2524
import kotlinx.coroutines.CompletableDeferred
2625
import kotlinx.coroutines.CoroutineScope
2726
import kotlinx.coroutines.async
@@ -35,6 +34,7 @@ import okio.BufferedSink
3534
import okio.use
3635
import kotlin.jvm.JvmOverloads
3736
import kotlin.jvm.JvmStatic
37+
import kotlin.time.TimeSource.Monotonic.markNow
3838

3939
/**
4040
* An [HttpInterceptor] that batches HTTP queries to execute multiple at once.
@@ -68,7 +68,7 @@ class BatchingHttpInterceptor @JvmOverloads constructor(
6868
private val maxBatchSize: Int = 10,
6969
private val exposeErrorBody: Boolean = false,
7070
) : HttpInterceptor {
71-
private val creationTime = currentTimeMillis()
71+
private val startMark = markNow()
7272
private val dispatcher = CloseableSingleThreadDispatcher()
7373
private val scope = CoroutineScope(dispatcher.coroutineDispatcher)
7474
private val mutex = Mutex()
@@ -107,7 +107,7 @@ class BatchingHttpInterceptor @JvmOverloads constructor(
107107
executePendingRequests()
108108
} else {
109109
scope.launch {
110-
delay(batchIntervalMillis - ((currentTimeMillis() - creationTime) % batchIntervalMillis) - 1)
110+
delay(batchIntervalMillis - (startMark.elapsedNow().inWholeMilliseconds % batchIntervalMillis) - 1)
111111
executePendingRequests()
112112
}
113113
}

libraries/apollo-runtime/src/commonMain/kotlin/com/apollographql/apollo3/network/websocket/internal/SubscribableWebSocket.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import com.apollographql.apollo3.exception.ApolloNetworkException
88
import com.apollographql.apollo3.exception.ApolloWebSocketClosedException
99
import com.apollographql.apollo3.exception.DefaultApolloException
1010
import com.apollographql.apollo3.exception.SubscriptionConnectionException
11-
import com.apollographql.apollo3.mpp.currentTimeMillis
1211
import com.apollographql.apollo3.network.websocket.CLOSE_GOING_AWAY
1312
import com.apollographql.apollo3.network.websocket.CLOSE_NORMAL
1413
import com.apollographql.apollo3.network.websocket.ClientMessage
@@ -35,6 +34,8 @@ import kotlinx.coroutines.Job
3534
import kotlinx.coroutines.cancel
3635
import kotlinx.coroutines.delay
3736
import kotlinx.coroutines.launch
37+
import kotlin.time.TimeMark
38+
import kotlin.time.TimeSource.Monotonic.markNow
3839

3940
/**
4041
* A [SubscribableWebSocket] is the link between the lower level [WebSocket] and GraphQL.
@@ -63,7 +64,7 @@ internal class SubscribableWebSocket(
6364
private var shutdownCause: ApolloException? = null
6465
private var activeListeners = mutableMapOf<String, OperationListener>()
6566
private var pending = mutableListOf<ApolloRequest<*>>()
66-
private var _lastActiveMillis: Long = 0
67+
private var _lastActiveMark: TimeMark? = null
6768

6869
private var webSocket: WebSocket
6970
init {
@@ -75,9 +76,9 @@ internal class SubscribableWebSocket(
7576
webSocket = webSocketEngine.newWebSocket(serverUrl, headers, this)
7677
}
7778

78-
val lastActiveMillis: Long
79+
val lastActiveMark: TimeMark?
7980
get() = lock.withLock {
80-
_lastActiveMillis
81+
_lastActiveMark
8182
}
8283
val shutdown: Boolean
8384
get() = lock.withLock {
@@ -248,7 +249,7 @@ internal class SubscribableWebSocket(
248249
}
249250

250251
if (activeListeners.isEmpty()) {
251-
_lastActiveMillis = currentTimeMillis()
252+
_lastActiveMark = markNow()
252253
}
253254
ret
254255
}
@@ -261,7 +262,7 @@ internal class SubscribableWebSocket(
261262
}
262263

263264
fun markActive() = lock.withLock {
264-
_lastActiveMillis = 0
265+
_lastActiveMark = null
265266
}
266267
}
267268

0 commit comments

Comments
 (0)