Fix coroutine scope (#4820)

* Inject the session scope instead of the application scope where it's possible.

* Create AppCoroutineScope annotation to let developers explicitly choose the appropriate CoroutineScope when injecting one.
This commit is contained in:
Benoit Marty 2025-06-04 17:33:51 +02:00 committed by GitHub
parent 36c7c7ab9b
commit 5f191d9f9c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
58 changed files with 172 additions and 72 deletions

View file

@ -13,6 +13,7 @@ import io.element.android.libraries.core.coroutine.CoroutineDispatchers
import io.element.android.libraries.core.extensions.runCatchingExceptions
import io.element.android.libraries.di.AppScope
import io.element.android.libraries.di.CacheDirectory
import io.element.android.libraries.di.annotations.AppCoroutineScope
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import timber.log.Timber
@ -24,7 +25,8 @@ import javax.inject.Inject
*/
@ContributesBinding(AppScope::class)
class DefaultCacheCleaner @Inject constructor(
private val scope: CoroutineScope,
@AppCoroutineScope
private val coroutineScope: CoroutineScope,
private val dispatchers: CoroutineDispatchers,
@CacheDirectory private val cacheDir: File,
) : CacheCleaner {
@ -33,7 +35,7 @@ class DefaultCacheCleaner @Inject constructor(
}
override fun clearCache() {
scope.launch(dispatchers.io) {
coroutineScope.launch(dispatchers.io) {
runCatchingExceptions {
SUBDIRS_TO_CLEANUP.forEach {
File(cacheDir.path, it).apply {

View file

@ -55,7 +55,7 @@ class DefaultCacheCleanerTest {
}
private fun TestScope.aCacheCleaner() = DefaultCacheCleaner(
scope = this,
coroutineScope = this,
dispatchers = this.testCoroutineDispatchers(true),
cacheDir = temporaryFolder.root,
)