CLOVIS
09/21/2023, 7:30 AMprintln(done()) // Done
println(loading()) // Loading
println(loading(0.2)) // Loading(20%)
Custom implementations can be created to add any other kind of information (ETAs, bandwidth, transfer speed…). Progress is meant to provide a common standard, allowing its use both in UI libraries and HTTP/API libraries to seamlessly share information without being aware of each other.
Progress information can be reported to the caller in a variety of ways; for example, with context receivers…
context(ProgressReporter)
fun foo() {
report(loading(0.0)) // reports 0% loading to the caller…
doSomeOperation()
report(loading(0.5))
otherOperation()
report(done())
}
fun main() {
val reporter = ProgressReporter(::println) // do anything you want: log it, update the UI…
with(reporter) { foo() }
}
…or via the coroutine context:
suspend fun foo() {
report(loading(0.0))
doSomeOperation()
report(loading(0.5))
otherOperation()
report(done())
}
suspend fun main() {
val reporter = ProgressReporter(::println)
withContext(reporter.asCoroutineContext()) { foo() }
}
Through implicit embedding in the coroutine context, it is possible to introduce this library without changing function signatures, directly in the functions that would benefit the most. Since the caller must opt-in to event generation by providing a reporter, it can be used in libraries safely.
Learn more: Documentation • Maven Central • Repository • or ask questions in this thread!