https://kotlinlang.org logo
#coroutines
Title
# coroutines
u

ursus

04/21/2021, 12:04 AM
Copy code
suspend 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?
e

Erik

04/21/2021, 5:41 AM
Looks alright, assuming that
this.clear()
is the correct way to clear all resources involved in the barcode scanner
I wouldn't call the function
awaitX
, because awaiting usually means using a
Deferred<T>
when working with coroutines.
If you can abstract all barcode scanner domain away, then you could simply name the suspending function
process
too, because nobody needs to know the original blocking function with the same name
Otherwise
processSuspending
is fine, or maybe
processAsync
if that's applicable
u

ursus

04/22/2021, 9:12 PM
thanks!
10 Views