spand
11/28/2019, 9:31 AMFileReader
to suspend. Does this look good?
• Should I cancel the continuation when the underlying resource is aborted (ie. by the browser) ?
• Is this sequence safe ? continuation.invokeOnCancellation -> abort() -> onabort -> continuation.cancel()
suspend fun Blob.readAsDataUrl2(): String {
val fileReader = FileReader()
return suspendCancellableCoroutine { continuation ->
fileReader.onload = {
val dataUrl = fileReader.result as String
continuation.resume(dataUrl)
}
fileReader.onerror = {
continuation.resumeWithException(DomExceptionThrowable(fileReader.error as DOMException))
}
fileReader.onabort = {
continuation.cancel()
}
continuation.invokeOnCancellation { fileReader.abort() }
fileReader.readAsDataURL(this) // <-- Async work starts here
}
}
elizarov
11/28/2019, 10:03 AMcancel
from invokeOnCancellation
whould not do anything. It is already cancelled.fileReader.onabort
. But if you do, then you might considering doing continuation.resumeWithException(IWasAborted(…))
there to distinguish cases when a coroutine was cancelled and when something else aborted your file reader.spand
11/28/2019, 10:09 AM