iwmlib/lib/imageloader.js

49 lines
1.2 KiB
JavaScript
Raw Normal View History

2019-03-21 09:57:27 +01:00
let loadQueue = []
let pendingRequests = new Map()
function onload(event) {
let url = event.target.responseURL
pendingRequests.delete(url)
postMessage({ success: true, url: url })
}
function onerror(event) {
let url = event.target.responseURL
pendingRequests.delete(url)
postMessage({ success: false, url: url })
}
function load() {
2022-10-04 10:51:35 +02:00
loadQueue.forEach((url) => {
2019-03-21 09:57:27 +01:00
let xhr = new XMLHttpRequest()
xhr.responseType = 'blob'
xhr.onload = onload
xhr.onerror = onerror
xhr.open('GET', url, true)
xhr.send()
pendingRequests.set(url, xhr)
})
}
2022-10-04 10:51:35 +02:00
self.onmessage = (event) => {
2019-03-21 09:57:27 +01:00
let msg = event.data
2019-07-18 12:26:39 +02:00
switch (msg.command) {
2019-03-21 09:57:27 +01:00
case 'load':
2019-07-18 12:26:39 +02:00
for (let url of msg.urls) {
2019-03-21 09:57:27 +01:00
console.log('Load', url)
loadQueue.push(url)
}
load()
break
case 'abort':
loadQueue = []
2019-07-18 12:26:39 +02:00
for (let xhr of pendingRequests.values()) {
2019-03-21 09:57:27 +01:00
console.log('Abort')
xhr.abort()
}
break
default:
2019-07-18 12:26:39 +02:00
console.warn('Unknown worker command: ' + msg.command)
2019-03-21 09:57:27 +01:00
}
}