-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide Coroutine Scope and Dispatchers Modules for #56
- Loading branch information
1 parent
f1c0f96
commit 8ff2f84
Showing
3 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
core/common/src/main/java/garousi/dev/common/di/CoroutinesDispatchersModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package garousi.dev.common.di | ||
|
||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.Dispatchers | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object CoroutinesDispatchersModule { | ||
|
||
@DefaultDispatcher | ||
@Provides | ||
fun providesDefaultDispatcher(): CoroutineDispatcher = Dispatchers.Default | ||
|
||
@IoDispatcher | ||
@Provides | ||
fun providesIoDispatcher(): CoroutineDispatcher = Dispatchers.IO | ||
|
||
@MainDispatcher | ||
@Provides | ||
fun providesMainDispatcher(): CoroutineDispatcher = Dispatchers.Main | ||
|
||
@MainImmediateDispatcher | ||
@Provides | ||
fun providesMainImmediateDispatcher(): CoroutineDispatcher = Dispatchers.Main.immediate | ||
} |
23 changes: 23 additions & 0 deletions
23
core/common/src/main/java/garousi/dev/common/di/CoroutinesQualifiers.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package garousi.dev.common.di | ||
|
||
import javax.inject.Qualifier | ||
|
||
@Retention(AnnotationRetention.BINARY) | ||
@Qualifier | ||
annotation class DefaultDispatcher | ||
|
||
@Retention(AnnotationRetention.BINARY) | ||
@Qualifier | ||
annotation class IoDispatcher | ||
|
||
@Retention(AnnotationRetention.BINARY) | ||
@Qualifier | ||
annotation class MainDispatcher | ||
|
||
@Retention(AnnotationRetention.BINARY) | ||
@Qualifier | ||
annotation class MainImmediateDispatcher | ||
|
||
@Retention(AnnotationRetention.BINARY) | ||
@Qualifier | ||
annotation class ApplicationScope |
23 changes: 23 additions & 0 deletions
23
core/common/src/main/java/garousi/dev/common/di/CoroutinesScopesModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package garousi.dev.common.di | ||
|
||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import javax.inject.Singleton | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.SupervisorJob | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object CoroutinesScopesModule { | ||
@Singleton // Provide always the same instance | ||
@Provides | ||
fun providesCoroutineScope( | ||
@DefaultDispatcher defaultDispatcher: CoroutineDispatcher | ||
): CoroutineScope { | ||
// Run this code when providing an instance of CoroutineScope | ||
return CoroutineScope(SupervisorJob() + defaultDispatcher) | ||
} | ||
} |