ResourceScope
Computation block for the Resource type. The Resource allows us to describe resources as immutable values, and compose them together in simple ways. This way you can split the logic of what a Resource
is and how it should be closed from how you use them.
Using and composing Resource
import arrow.fx.coroutines.continuations.resource
import arrow.fx.coroutines.release
class UserProcessor {
fun start(): Unit = println("Creating UserProcessor")
fun shutdown(): Unit = println("Shutting down UserProcessor")
fun process(ds: DataSource): List<String> =
ds.users().map { "Processed $it" }
}
class DataSource {
fun connect(): Unit = println("Connecting dataSource")
fun users(): List<String> = listOf("User-1", "User-2", "User-3")
fun close(): Unit = println("Closed dataSource")
}
class Service(val db: DataSource, val userProcessor: UserProcessor) {
suspend fun processData(): List<String> = userProcessor.process(db)
}
//sampleStart
val userProcessor = resource {
UserProcessor().also(UserProcessor::start)
} release UserProcessor::shutdown
val dataSource = resource {
DataSource().also { it.connect() }
} release DataSource::close
suspend fun main(): Unit {
resource<Service> {
Service(dataSource.bind(), userProcessor.bind())
}.use { service -> service.processData() }
}
//sampleEnd
Functions
Creates a Resource from an AutoCloseable, which uses AutoCloseable.close for releasing.
Creates a Resource from an Closeable, which uses Closeable.close for releasing.
Creates a single threaded CoroutineContext as a Resource. Upon release an orderly shutdown of the ExecutorService takes place in which previously submitted tasks are executed, but no new tasks will be accepted.
Creates a single threaded CoroutineContext as a Resource. Upon release an orderly shutdown of the ExecutorService takes place in which previously submitted tasks are executed, but no new tasks will be accepted.
Install A into the ResourceScope. It's release function will be called with the appropriate ExitCase if this ResourceScope finishes. It results either in ExitCase.Completed, ExitCase.Cancelled or ExitCase.Failure depending on the terminal state of Resource lambda.
Composes a releaseCase action to a Resource value before binding.
Creates a single threaded CoroutineContext as a Resource. Upon release an orderly shutdown of the ExecutorService takes place in which previously submitted tasks are executed, but no new tasks will be accepted.