import { removeArrayItem } from '../../util/util';
export var ImgLoader = (function () {
    function ImgLoader() {
        this.callbacks = [];
        this.ids = 0;
    }
    ImgLoader.prototype.load = function (src, cache, callback) {
        if (src) {
            callback.id = this.ids++;
            this.callbacks.push(callback);
            this.worker().postMessage(JSON.stringify({
                id: callback.id,
                src: src,
                cache: cache
            }));
        }
    };
    ImgLoader.prototype.cancelLoad = function (callback) {
        removeArrayItem(this.callbacks, callback);
    };
    ImgLoader.prototype.abort = function (src) {
        if (src) {
            this.worker().postMessage(JSON.stringify({
                src: src,
                type: 'abort'
            }));
        }
    };
    ImgLoader.prototype.worker = function () {
        var _this = this;
        if (!this.wkr) {
            var workerBlob = new Blob([INLINE_WORKER]);
            var blobURL = window.URL.createObjectURL(workerBlob);
            this.wkr = new Worker(blobURL);
            this.wkr.onmessage = function (ev) {
                var msg = JSON.parse(ev.data);
                var callback = _this.callbacks.find(function (cb) { return cb.id === msg.id; });
                if (callback) {
                    callback(msg);
                    removeArrayItem(_this.callbacks, callback);
                }
            };
            this.wkr.onerror = function (ev) {
                console.error("ImgLoader, worker " + ev.type + " " + (ev.message ? ev.message : ''));
                _this.callbacks.length = 0;
                _this.wkr.terminate();
                _this.wkr = null;
            };
        }
        return this.wkr;
    };
    return ImgLoader;
}());
var INLINE_WORKER = "!function(){function t(t,e,n,d){var i={id:t,src:e,status:d.target.status,data:null,len:0};if(200===d.target.status&&(a(i,d.target.getResponseHeader(\"Content-Type\"),d.target.response),i.len=i.data.length),postMessage(JSON.stringify(i)),200===n.x.status&&n.c){n.d=i.data,n.l=i.len;for(var o=0,f=s.length-1;f>=0;f--)o+=s[f].l,o>r&&s.splice(f,1)}}function e(t,e,a,s){postMessage(JSON.stringify({id:t,src:e,status:510,msg:s.message+\"\"+s.stack})),a.x=null}function a(t,e,a){t.data=\"data:\"+e+\";base64,\";var s,r,d,i,o,f,u=new Uint8Array(a),c=u.byteLength,l=c%3,g=c-l;for(s=0;s<g;s+=3)f=u[s]<<16|u[s+1]<<8|u[s+2],r=(16515072&f)>>18,d=(258048&f)>>12,i=(4032&f)>>6,o=63&f,t.data+=n[r]+n[d]+n[i]+n[o];1===l?(f=u[g],r=(252&f)>>2,d=(3&f)<<4,t.data+=n[r]+n[d]+\"==\"):2===l&&(f=u[g]<<8|u[g+1],r=(64512&f)>>10,d=(1008&f)>>4,i=(15&f)<<2,t.data+=n[r]+n[d]+n[i]+\"=\")}var s=[],n=\"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\",r=13818550;onmessage=function(a){for(var n,r=JSON.parse(a.data),d=r.id,i=r.src,o=0;o<s.length;o++)if(s[o].s===i){n=s[o];break}if(\"abort\"===r.type)n&&n.x&&(n.x.abort(),n.x=null);else if(r.cache&&n&&n.d)postMessage(JSON.stringify({id:d,src:i,status:200,data:n.d,len:n.l}));else{if(n&&n.x&&4!==n.x.readyState)return n.x.addEventListener(\"load\",function(e){t(d,i,n,e)}),void n.x.addEventListener(\"error\",function(t){e(d,i,n,t)});n||(n={s:i,c:r.cache},s.push(n)),n.x=new XMLHttpRequest,n.x.open(\"GET\",i,!0),n.x.responseType=\"arraybuffer\",n.x.addEventListener(\"load\",function(e){t(d,i,n,e)}),n.x.addEventListener(\"error\",function(t){e(d,i,n,t)}),n.x.send()}}}();";
//# sourceMappingURL=img-loader.js.map