iwmlib/lib/pixi/deepzoom/tileloader.js

52 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-03-21 09:57:27 +01:00
let loadQueue = []
let pendingRequests = new Map()
const batchSize = 8
const debug = false
function load() {
2019-08-06 15:34:57 +02:00
while (loadQueue.length > 0 && pendingRequests.size < batchSize) {
2019-03-21 09:57:27 +01:00
let tile = loadQueue.shift()
let [col, row, url] = tile
let xhr = new XMLHttpRequest()
2019-07-04 09:17:43 +02:00
xhr.responseType = 'arraybuffer'
2019-08-06 15:34:57 +02:00
xhr.onload = event => {
2019-03-21 09:57:27 +01:00
pendingRequests.delete(url)
let buffer = xhr.response
2019-08-06 15:34:57 +02:00
postMessage({ success: true, url, col, row, buffer }, [buffer])
2019-03-21 09:57:27 +01:00
}
2019-08-06 15:34:57 +02:00
xhr.onerror = event => {
2019-03-21 09:57:27 +01:00
pendingRequests.delete(url)
let buffer = null
2019-08-06 15:34:57 +02:00
postMessage({ success: false, url, col, row, buffer })
2019-03-21 09:57:27 +01:00
}
xhr.open('GET', url, true)
xhr.send()
pendingRequests.set(url, xhr)
}
2019-08-06 15:34:57 +02:00
if (loadQueue.length > 0) setTimeout(load, 1000 / 120)
2019-03-21 09:57:27 +01:00
else {
2019-07-04 09:17:43 +02:00
if (debug) console.log('Ready')
2019-03-21 09:57:27 +01:00
}
}
2019-08-06 15:34:57 +02:00
self.onmessage = event => {
2019-03-21 09:57:27 +01:00
let msg = event.data
2019-08-06 15:34:57 +02:00
switch (msg.command) {
2019-03-21 09:57:27 +01:00
case 'load':
2019-08-06 15:34:57 +02:00
for (let tile of msg.tiles) {
2019-03-21 09:57:27 +01:00
loadQueue.push(tile)
}
load()
break
case 'abort':
loadQueue = []
2019-08-06 15:34:57 +02:00
for (let xhr of pendingRequests.values()) {
2019-03-21 09:57:27 +01:00
xhr.abort()
}
if (debug) console.log('Abort')
break
default:
2019-08-06 15:34:57 +02:00
console.warn('Unknown worker command: ' + msg.command)
2019-03-21 09:57:27 +01:00
}
}