ursus
04/21/2021, 12:04 AMsuspend fun BarcodeScanner.processSuspending(image: ImageProxy): Barcode? {
return suspendCancelableCoroutine { continuation ->
continuation.invokeOnCancelation {
this.clear()
}
this.process(image)
.addOnSuccessListener { barcodes ->
val barcode = barcodes.someTransformation()
continuation.resume(barcode)
}
.addOnFailure { throwable ->
continutaion.resumeWithException(throwable)
}
}
}
Would somebody mind looking at this if I do wrap a 3rd party callback library correctly? Its googles mlkit barcode scanner. Its my first time
Also, what is the best pragmatic name of providing a suspend function if the "native" is called process()
? processSuspending()
? or some sort of await
variant like awaitProcess()
?
Also2, I don't need to apply dispatcher, since it provides callbacks, meaning it most likely has its own thread inside, right?Erik
04/21/2021, 5:41 AMthis.clear()
is the correct way to clear all resources involved in the barcode scannerawaitX
, because awaiting usually means using a Deferred<T>
when working with coroutines.process
too, because nobody needs to know the original blocking function with the same nameprocessSuspending
is fine, or maybe processAsync
if that's applicableursus
04/22/2021, 9:12 PM