#State of the Web Worker (api)
I just did a rewrite of my communist library with a focus on compatability.
Opera and safari 5 don't support URL.createObjectURL while IE 10 considers them another origin meaning (i.e. can't make a worker out of it). I got around this with:
if (typeof document === "undefined") {
self.onmessage=function(e){
eval(e.data);
}
} else {
(function(){
code...
function getPath(){
var scripts = document.getElementsByTagName("script");
var len = scripts.length;
var i = 0;
while(i<len){
if(/communist(\.min)?\.js/.test(scripts[i].src)){
return scripts[i].src;
}
i++;
}
}
function makeWorker(strings){
var worker;
var script =moveImports(strings.join(""));
var _URL = window.URL || window.webkitURL;
try{
worker= new Worker(_URL.createObjectURL(new Blob([script],{type: "text/javascript"})));
}catch(e){
worker = new Worker(getPath());
worker.postMessage(script);
}finally{
return worker;
}
}
..more code
})()
}the script is calling itself this will work when called reletivly, but it won't work accross origin, ruling out cdn use for a script that impliment this.
Internet Explorer 10 fails badly when it comes to transferable objects. It expects a port port as the second argument to postMessage and flips out if you give it an array. Meaning you have to find some sort of proxy for transferable objects and make sure you only pass one argument if it isn't flagged.
With regard to typed arrays, which are related to workers somewhat, Internet Explorer 10 does not impliment ArrayBuffer.prototype.slice() nor Uint8ClampedArray. The functionallity of ArrayBuffer.prototype.slice() can be shimed with DataView
ArrayBuffer.prototype.slice = ArrayBuffer.prototype.slice||function(start,end){
end =this.byteLength;
if(!end||end>this.byteLength){
end=this.byteLength;
}else if(end=<start){
return new ArrayBuffer();
}
var view = new DataView(this,start,end);
var i = 0;
var len = view.byteLength;
var out = new Uint8Array(len);
while(i<len){
out[i]=view.getUint8(i);
i++;
}
return out.buffer;
}Uint8ClampedArray is only different from Uint8Array by the bounds check, for Uint8ClampedArray it's ~(x>255)?255:x while for Uint8Array it's ~ x%255.
Lastly a few odds and ends
- Chrome will not let a worker open a sub worker, this is not an issue in FireFox or IE10(!!!). This means if you want to divide tasks between works the foreground thread needs to act as a hub.
- Safari 5 has some weird stuff going on with typed arrays, you should probably upgrade.
Overall IE despite its recent improvements is still behind with web workers, although unlike say webGL, for (everyone except those unfortunate souls stuck on IE on XP) there is at least a version of the browser that has adequate support for workers such that they are worth using.
Hey Calvin, this was one of the first results in Google for "ie transferable objects". :)
Could you clarify that bit? Is it not supported at all, or supported through some sort of a hack? What do you mean by "proxy for transferable objects"?