-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImageBitmapLoader-worker.js
More file actions
54 lines (50 loc) · 1.56 KB
/
ImageBitmapLoader-worker.js
File metadata and controls
54 lines (50 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'use strict';
// var log = self.console.log.bind(self.console);
function ImageHandler(workerContext) {
this.maxCount = 16;
this.loading = 0;
this.queue = [];
this.workerContext = workerContext;
}
ImageHandler.prototype = {
enqueue: function(evt) {
var toEnqueue = evt.data;
if (this.queue.indexOf(toEnqueue) < 0) {
this.queue.push(toEnqueue);
this.processQueue();
}
},
processQueue: function() {
if (this.queue.length > 0 && this.loading < this.maxCount) {
var queue = this.queue.shift(),
url = queue.src,
options = queue.options || {},
out = {url: url, load: true};
this.loading++;
return fetch(url, options) // Fetch the image.
.then(function(response) {
out.load = true;
this.loading--;
// this.workerContext.postMessage(out);
if (response.status !== 200) {
out.error = 'Unable to load resource with url ' + url;
//log('status !== 200', out);
return this.workerContext.postMessage(out);
}
return response.blob();
}.bind(this))
.then(createImageBitmap) // Turn it into an ImageBitmap.
.then(function(imageBitmap) { // Post it back to main thread.
out.imageBitmap = imageBitmap;
this.workerContext.postMessage(out, [imageBitmap]);
}.bind(this), function(err) {
out.error = err.toString();
this.workerContext.postMessage(out);
}.bind(this))
.then(this.processQueue.bind(this)) // Check the queue.
.catch(this.processQueue.bind(this))
}
}
};
var handler = new ImageHandler(self);
self.onmessage = handler.enqueue.bind(handler);