mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-12-18 14:47:15 +00:00
add preloadImage
This commit is contained in:
parent
a938ee1efb
commit
3cb6b73ee0
@ -0,0 +1,26 @@
|
||||
package dev.inmo.micro_utils.coroutines
|
||||
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.Job
|
||||
import org.w3c.dom.Image
|
||||
|
||||
suspend fun preloadImage(src: String): Image {
|
||||
val image = Image()
|
||||
image.src = src
|
||||
|
||||
val job = Job()
|
||||
|
||||
image.addEventListener("load", {
|
||||
runCatching { job.complete() }
|
||||
})
|
||||
|
||||
runCatchingSafely {
|
||||
job.join()
|
||||
}.onFailure {
|
||||
if (it is CancellationException) {
|
||||
image.src = ""
|
||||
}
|
||||
}.getOrThrow()
|
||||
|
||||
return image
|
||||
}
|
Loading…
Reference in New Issue
Block a user