ResourceScope

Functions

Link copied to clipboard
open fun <A> autoClose(acquire: () -> A, release: (A, Throwable?) -> Unit): A
Link copied to clipboard
suspend fun <A : AutoCloseable> ResourceScope.autoCloseable(closingDispatcher: CoroutineDispatcher = IODispatcher, autoCloseable: suspend () -> A): A

Creates a Resource from an AutoCloseable, which uses AutoCloseable.close for releasing.

Link copied to clipboard
open suspend fun <A> Resource<A>.bind(): A

Compose another Resource program into this ResourceScope. All release functions installed into the Resource lambda will be installed in this ResourceScope while respecting the FIFO order.

Link copied to clipboard
suspend fun <A : Closeable> ResourceScope.closeable(closingDispatcher: CoroutineDispatcher = Dispatchers.IO, closeable: suspend () -> A): A

Creates a Resource from an Closeable, which uses Closeable.close for releasing.

Link copied to clipboard
suspend fun ResourceScope.executor(timeout: Duration = Duration.INFINITE, closingDispatcher: CoroutineDispatcher = Dispatchers.IO, create: suspend () -> ExecutorService): ExecutorCoroutineDispatcher

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.

Link copied to clipboard
suspend fun ResourceScope.fixedThreadPoolContext(nThreads: Int, name: String): ExecutorCoroutineDispatcher

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.

Link copied to clipboard
open fun <A : AutoCloseable> install(autoCloseable: A): A

open suspend fun <A> install(acquire: suspend AcquireStep.() -> A, release: suspend (A, ExitCase) -> Unit): A

Install A into the ResourceScope. Its 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.

Link copied to clipboard
open override fun onClose(release: (Throwable?) -> Unit)
Link copied to clipboard
abstract infix fun onRelease(release: suspend (ExitCase) -> Unit)
Link copied to clipboard
open infix suspend fun <A> Resource<A>.release(release: suspend (A) -> Unit): A

Composes a release action to a Resource value before binding.

Link copied to clipboard
open infix suspend fun <A> Resource<A>.releaseCase(release: suspend (A, ExitCase) -> Unit): A

Composes a releaseCase action to a Resource value before binding.

Link copied to clipboard
suspend fun ResourceScope.singleThreadContext(name: String): ExecutorCoroutineDispatcher

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.