Skip to content

Commit

Permalink
isExpiredFlow() should emit false until the AccessToken is actually e…
Browse files Browse the repository at this point in the history
…xpired
  • Loading branch information
frett committed Jan 9, 2025
1 parent 27aead8 commit 74f817d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import kotlinx.coroutines.flow.flow

fun AccessToken.isExpiredFlow() = flow {
while (!isExpired) {
emit(true)
emit(false)
delay((expires.time - System.currentTimeMillis()).coerceAtLeast(1))
}
emit(false)
emit(true)
}.distinctUntilChanged()
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map

fun AccessTokenManager.currentAccessTokenFlow() = callbackFlow {
val tracker = object : AccessTokenTracker() {
Expand All @@ -28,7 +29,7 @@ fun AccessTokenManager.currentAccessTokenFlow() = callbackFlow {
}.conflate()

@Deprecated("Since v4.2.0, use isExpiredFlow() instead.", ReplaceWith("isExpiredFlow()"))
fun AccessTokenManager.isAuthenticatedFlow() = isExpiredFlow()
fun AccessTokenManager.isAuthenticatedFlow() = isExpiredFlow().map { !it }

@OptIn(ExperimentalCoroutinesApi::class)
fun AccessTokenManager.isExpiredFlow() = currentAccessTokenFlow()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ class AccessTokenManagerTest {
assertFalse(awaitItem())
accessTokenManager.currentAccessToken = token
executePendingBroadcasts()
assertTrue(awaitItem())
expectNoEvents()

every { token.isExpired } returns true
advanceUntilIdle()
assertFalse(awaitItem())
assertTrue(awaitItem())
}
}
// endregion isExpiredFlow()
Expand Down

0 comments on commit 74f817d

Please sign in to comment.