-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworkerframe.min.js
More file actions
9 lines (9 loc) · 2.62 KB
/
workerframe.min.js
File metadata and controls
9 lines (9 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
/*
workerframe (c) 2015 KNOWLEDGECODE | MIT
*/
(function(h){var l=h.URL||h.webkitURL,m=function(){var f=h.BlobBuilder||h.WebKitBlobBuilder||h.MSBlobBuilder;return f?function(g,a){var b=new f;b.append(g[0]);return b.getBlob(a.type)}:h.Blob}(),k=function(f){var g,a=this;if(this===window||"function"!==typeof f)throw new TypeError("Failed to construct 'WorkerFrame'.");this._uid=Math.random().toString(36).slice(2);g=l.createObjectURL(new m(["(%s('%s', '%s', %s));".replace("%s",function(a,d,e){var c=self,f={},g={},h=Array.prototype.slice,k=function(a,
b,c){var d=a[b]||[];0>d.indexOf(c)&&(d.push(c),a[b]=d);return c},l=function(a,b,c){var d=a[b]||[];c?(a=d.indexOf(c),0<=a&&d.splice(a,1)):delete a[b]};c.origin=a;c.message=function(a,b,c){postMessage({_type:a,_data:b},c)};c.console={log:function(){c.message("_l",h.call(arguments))}};c.on=function(a,b){return k(f,a,b)};c.one=function(a,b){return k(g,a,b)};c.off=function(a,b){l(f,a,b);l(g,a,b)};c.addEventListener("message",function(a){var b=a.data._type,e=a.data._data;a=function(a){a(e,function(a){c.message(b,
{_uid:d,_ok:!0,_data:a})},function(a){c.message(b,{_uid:d,_ok:!1,_data:a})})};b&&((f[b]||[]).map(a),(g[b]||[]).map(a),delete g[b],"close"===b&&(c.close(),c.message("_c")))});e()}.toString()).replace("%s",location.origin.replace("null","file://")).replace("%s",this._uid).replace("%s",f.toString())],{type:"text/javascript"}));this._l={};this._o={};this._p={};this._w=new Worker(g);this._w.addEventListener("message",function(b){var d=(b.data||{})._type||"";b=(b.data||{})._data;switch(d){case "_l":console.log.apply(console,
b);break;case "_c":l.revokeObjectURL(g);a._l=a._o=a._p=a._w=void 0;break;case "":break;default:a._d(d,b)}});this._w.addEventListener("error",function(b){a._d("error",b)})};k.prototype=new function(){var f=function(a,b,d){var e=a[b]||[];0>e.indexOf(d)&&(e.push(d),a[b]=e);return d},g=function(a,b,d){var e=a[b]||[];d?(a=e.indexOf(d),0<=a&&e.splice(a,1)):delete a[b]};this.message=function(a,b,d){var e=this,c=new Promise(function(b,c){f(e._p,a,function(a){(a||{})._uid===e._uid&&(a._ok?b(a._data):c(a._data))})});
this._w.postMessage({_type:a,_data:b},d);return c};this.close=function(){this.message("close")};this.on=function(a,b){return f(this._l,a,b)};this.one=function(a,b){return f(this._o,a,b)};this.off=function(a,b){g(this._l,a,b);g(this._o,a,b)};this._d=function(a,b){var d=this,e=function(a){(b||{})._uid===d._uid?a(b._data):a(b)};(this._l[a]||[]).map(e);(this._o[a]||[]).map(e);delete this._o[a];(this._p[a]||[]).map(function(a){a(b)});delete this._p[a]}};k.prototype.constructor=k;"function"===typeof define&&
define.amd?define([],function(){return k}):h.WorkerFrame=k})(this);