Skip to content

Commit ee8fbf3

Browse files
committed
DataConnectCredentialsTokenManager.kt: awaitTokenProvider() added
1 parent 2adbdfe commit ee8fbf3

File tree

2 files changed

+26
-34
lines changed

2 files changed

+26
-34
lines changed

firebase-dataconnect/src/main/kotlin/com/google/firebase/dataconnect/core/DataConnectCredentialsTokenManager.kt

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ import kotlinx.coroutines.async
4545
import kotlinx.coroutines.cancel
4646
import kotlinx.coroutines.ensureActive
4747
import kotlinx.coroutines.flow.MutableStateFlow
48-
import kotlinx.coroutines.flow.StateFlow
49-
import kotlinx.coroutines.flow.asStateFlow
48+
import kotlinx.coroutines.flow.filter
49+
import kotlinx.coroutines.flow.first
5050
import kotlinx.coroutines.launch
5151

5252
/** Base class that shares logic for managing the Auth token and AppCheck token. */
@@ -59,9 +59,6 @@ internal sealed class DataConnectCredentialsTokenManager<T : Any>(
5959
val instanceId: String
6060
get() = logger.nameWithId
6161

62-
private val _providerAvailable = MutableStateFlow(false)
63-
val providerAvailable: StateFlow<Boolean> = _providerAvailable.asStateFlow()
64-
6562
@Suppress("LeakingThis") private val weakThis = WeakReference(this)
6663

6764
private val coroutineScope =
@@ -156,6 +153,28 @@ internal sealed class DataConnectCredentialsTokenManager<T : Any>(
156153
setClosedState()
157154
}
158155

156+
/**
157+
* Suspends until the token provider becomes available to this object.
158+
*
159+
* If [close] has been invoked, or is invoked _before_ a token provider becomes available, then
160+
* this method returns normally, as if a token provider _had_ become available.
161+
*/
162+
suspend fun awaitTokenProvider() {
163+
logger.debug { "awaitTokenProvider() start" }
164+
val currentState =
165+
state
166+
.filter {
167+
when (it) {
168+
State.Closed -> true
169+
is State.New -> false
170+
is State.Idle -> true
171+
is State.Active -> true
172+
}
173+
}
174+
.first()
175+
logger.debug { "awaitTokenProvider() done: currentState=$currentState" }
176+
}
177+
159178
// This function must ONLY be called from close().
160179
private fun setClosedState() {
161180
while (true) {
@@ -357,8 +376,6 @@ internal sealed class DataConnectCredentialsTokenManager<T : Any>(
357376
break
358377
}
359378
}
360-
361-
_providerAvailable.value = true
362379
}
363380

364381
/**

firebase-dataconnect/src/main/kotlin/com/google/firebase/dataconnect/core/FirebaseDataConnectImpl.kt

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ import kotlinx.coroutines.async
5454
import kotlinx.coroutines.cancel
5555
import kotlinx.coroutines.flow.MutableStateFlow
5656
import kotlinx.coroutines.flow.collect
57-
import kotlinx.coroutines.flow.first
58-
import kotlinx.coroutines.launch
5957
import kotlinx.coroutines.runBlocking
6058
import kotlinx.coroutines.sync.Mutex
6159
import kotlinx.coroutines.sync.withLock
@@ -120,9 +118,6 @@ internal class FirebaseDataConnectImpl(
120118
}
121119
)
122120

123-
private val authProviderAvailable = MutableStateFlow(false)
124-
private val appCheckProviderAvailable = MutableStateFlow(false)
125-
126121
// Protects `closed`, `grpcClient`, `emulatorSettings`, and `queryManager`.
127122
private val mutex = Mutex()
128123

@@ -141,17 +136,7 @@ internal class FirebaseDataConnectImpl(
141136
)
142137

143138
override suspend fun awaitAuthReady() {
144-
authProviderAvailable.first { it }
145-
}
146-
147-
init {
148-
val name = CoroutineName("DataConnectAuth isProviderAvailable pipe for $instanceId")
149-
coroutineScope.launch(name) {
150-
dataConnectAuth.providerAvailable.collect { isProviderAvailable ->
151-
logger.debug { "authProviderAvailable=$isProviderAvailable" }
152-
authProviderAvailable.value = isProviderAvailable
153-
}
154-
}
139+
dataConnectAuth.awaitTokenProvider()
155140
}
156141

157142
private val dataConnectAppCheck: DataConnectAppCheck =
@@ -163,17 +148,7 @@ internal class FirebaseDataConnectImpl(
163148
)
164149

165150
override suspend fun awaitAppCheckReady() {
166-
appCheckProviderAvailable.first { it }
167-
}
168-
169-
init {
170-
val name = CoroutineName("DataConnectAppCheck isProviderAvailable pipe for $instanceId")
171-
coroutineScope.launch(name) {
172-
dataConnectAppCheck.providerAvailable.collect { isProviderAvailable ->
173-
logger.debug { "appCheckProviderAvailable=$isProviderAvailable" }
174-
appCheckProviderAvailable.value = isProviderAvailable
175-
}
176-
}
151+
dataConnectAppCheck.awaitTokenProvider()
177152
}
178153

179154
private val lazyGrpcRPCs =

0 commit comments

Comments
 (0)