Skip to content

Instantly share code, notes, and snippets.

@AndroidPoet
Created May 28, 2022 06:53
Show Gist options
  • Save AndroidPoet/f783280d925392ec45315c00a9572395 to your computer and use it in GitHub Desktop.
Save AndroidPoet/f783280d925392ec45315c00a9572395 to your computer and use it in GitHub Desktop.
//interface
interface CoroutineDispatcherProvider {
val main: CoroutineDispatcher
val io: CoroutineDispatcher
val default: CoroutineDispatcher
val unconfirmed: CoroutineDispatcher
}
//initialization using "by lazy"
open class RealCoroutineDispatcherProvider : CoroutineDispatcherProvider {
override val main: CoroutineDispatcher by lazy { Dispatchers.Main }
override val io: CoroutineDispatcher by lazy { Dispatchers.IO }
override val default: CoroutineDispatcher by lazy { Dispatchers.Default }
override val unconfirmed: CoroutineDispatcher by lazy { Dispatchers.Unconfined }
}
//DI Module "
@InstallIn(SingletonComponent::class)
@Module
class DispatcherModule {
@Provides
@Singleton
fun providesCoroutineDispatcher(): CoroutineDispatcherProvider {
return RealCoroutineDispatcherProvider()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment