Skip to content

Instantly share code, notes, and snippets.

@tomkoptel
Last active April 18, 2018 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomkoptel/6563da1e3c7426d36596ed84e354f5d7 to your computer and use it in GitHub Desktop.
Save tomkoptel/6563da1e3c7426d36596ed84e354f5d7 to your computer and use it in GitHub Desktop.
Wrapper factory API with a purpose to create a single entry point hook to later delegate Job instances.
import kotlinx.coroutines.experimental.CoroutineScope
import kotlinx.coroutines.experimental.CoroutineStart
import kotlinx.coroutines.experimental.DefaultDispatcher
import kotlinx.coroutines.experimental.Job
import kotlin.coroutines.experimental.CoroutineContext
import kotlinx.coroutines.experimental.launch as coroutineLaunch
/**
* Acts as dependency provider. Later used in the test env to provide suspendable UI tests.
*/
interface CoroutinesProvider {
fun launch(
context: CoroutineContext = DefaultDispatcher,
start: CoroutineStart = CoroutineStart.DEFAULT,
parent: Job? = null,
block: suspend CoroutineScope.() -> Unit
): Job
companion object {
fun default(): CoroutinesProvider = object : CoroutinesProvider {
override fun launch(
context: CoroutineContext,
start: CoroutineStart,
parent: Job?,
block: suspend CoroutineScope.() -> Unit
): Job {
// It is an alias to the `kotlinx.coroutines.experimental.launch`
return coroutineLaunch(context, start, parent, block)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment