Stylianos Gakis
08/23/2021, 3:23 PMStylianos Gakis
08/23/2021, 3:24 PMoverride fun intercept(chain: Interceptor.Chain): Response {
val currentToken = runBlocking {
sessionManager.getCurrentToken()
}
return chain.proceed(
chain.request().withAuthenticationToken(currentToken)
)
}
Or I try and do something different and go with an approach that might look like this
class ExistingTokenAppendingInterceptor(
sessionManager: SessionManager,
) : Interceptor {
private var currentToken: String? = null
init {
sessionManager.getTokenFlow()
.onEach { token ->
currentToken = token
}
.launchIn(GlobalScope) // TODO Pass appropriate scope? Handle this differently?
}
override fun intercept(chain: Interceptor.Chain): Response {
return chain.proceed(
currentToken?.let { token ->
chain.request().withAuthenticationToken(token)
} ?: chain.request()
)
}
}
Which also doesn’t look that nice since I am observing the Datastore on the GlobalScope, even the linter doesn’t like that.
Maybe this solution would look nice if I provide some sort of application-wide coroutine scope instead to launch the flow listening on it? Not sure how I would do that either.
Any ideas would be appreciated.louiscad
08/23/2021, 6:47 PMOleksandr Balan
08/23/2021, 8:06 PMintercept
method is called on the worker thread. So as for me it is a valid example to use runBlocking
here.
It is even in it's doc:
It is designed to bridge regular blocking code to libraries that are written in suspending style
Stylianos Gakis
08/23/2021, 9:25 PMStylianos Gakis
08/23/2021, 9:27 PMlouiscad
08/23/2021, 9:40 PMStylianos Gakis
08/23/2021, 10:29 PMval tokenState: StateFlow<String?> = getTokenFlow()
.stateIn(
scope = ??, // Note here
started = SharingStarted.Eagerly,
initialValue = null
)
override fun intercept(chain: Interceptor.Chain): Response {
val currentToken = sessionManager.tokenState.value
return if (currentToken == null) {
// etc.
The part I am struggling with is what should that StateFlow be scoped to. All I see on the official documentations are about viewmodel scopes etc, but in this case I would need a global one, but GlobalScope
can’t be it right?louiscad
08/24/2021, 6:34 AMAppScope
or alike.