//arrow-fx-coroutines/arrow.fx.coroutines/bracketCase
common inline suspend fun <A, B> bracketCase(crossinline acquire: suspend () -> A, use: suspend (A) -> B, crossinline release: suspend (A, ExitCase) -> Unit): B
A way to safely acquire a resource and release in the face of errors and cancellation. It uses ExitCase to distinguish between different exit cases when releasing the acquired resource.
bracketCase exists out of three stages:
suspend
effect. The key difference here is that it’s wired in such a way that releasewill always be called either on ExitCase.Cancelled, ExitCase.Failure or ExitCase.Completed. If it failed, then the resulting suspend from bracketCase will be the error; otherwise the result of use will be returned.common
acquire | is the action to acquire the resource. |
use | is the action to consume the resource and produce a result. Once the resulting suspend program terminates, either successfully, error or disposed, the release function will run to clean up the resources. |
release | is the action to release the allocated resource after use terminates.kotlin import arrow.fx.coroutines.*<br>class File(url: String) { fun open(): File = this fun close(): Unit {} }<br>suspend fun File.content(): String = "This file contains some interesting content!" suspend fun openFile(uri: String): File = File(uri).open() suspend fun closeFile(file: File): Unit = file.close()<br>suspend fun main(): Unit { //sampleStart val res = bracketCase( acquire = { openFile("data.json") }, use = { file -> file.content() }, release = { file, exitCase -> when (exitCase) { is ExitCase.Completed -> println("File closed with $exitCase") is ExitCase.Cancelled -> println("Program cancelled with $exitCase") is ExitCase.Failure -> println("Program failed with $exitCase") } closeFile(file) } ) //sampleEnd println(res) } |
Do you like Arrow?
✖