-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathasynchronous.min.js
10 lines (10 loc) · 13.3 KB
/
asynchronous.min.js
1
2
3
4
5
6
7
8
9
10
/**
*
* Asynchronous.js
* @version: 0.5.1
*
* Simple JavaScript class to manage asynchronous, parallel, linear, sequential and interleaved tasks
* https://github.com/foo123/asynchronous.js
*
**/
!function(n,e,t){"use strict";"undefined"!=typeof Components&&"object"==typeof Components.classes&&"object"==typeof Components.classesByID&&Components.utils&&"function"==typeof Components.utils["import"]?(n.$deps=n.$deps||{})&&(n.EXPORTED_SYMBOLS=[e])&&(n[e]=n.$deps[e]=t.call(n)):"object"==typeof module&&module.exports?(module.$deps=module.$deps||{})&&(module.exports=module.$deps[e]=t.call(n)):"undefined"!=typeof System&&"function"==typeof System.register&&"function"==typeof System["import"]?System.register(e,[],function(r){r(e,t.call(n))}):"function"==typeof define&&define.amd&&"function"==typeof require&&"function"==typeof require.specified&&require.specified(e)?define(e,["module"],function(e){return t.moduleUri=e.uri,t.call(n)}):e in n||(n[e]=t.call(n)||1)&&"function"==typeof define&&define.amd&&define(function(){return n[e]})}(this,"Asynchronous",function n(e){"use strict";function t(n){var e,t=[];if(n)for(e in n)n[c](e)&&t.push(e+"="+(!0===n[e]||1===n[e]?"yes":!1===n[e]||0===n[e]?"no":n[e]));return t.join(",")}function r(n,e){n.$runmode=M,n.$running=!1}function o(n,e){var t,r=n,o=r.$queue;if(r.$runmode=x,o){for(;o.length&&(!o[0]||!o[0].canRun());)o.shift();o.length?(r.$running=!0,t=o.shift(),e?t.runWithArgs(e):t.run(),t.complete()):r.$running=!1}}function i(n,e){var t,r=n,o=r.$queue,s=0;if(r.$runmode=R,o&&o.length){for(r.$running=!0;s<o.length;)t=o[s],t&&t.canRun()?(e?t.runWithArgs(e):t.run(),t.isFinished()?(o.shift(),t.complete()):s++):o.shift();r.$running=!1,r.$timer=N(v(i,r),r.$interval)}}function s(n,e){var t,r=n,o=r.$queue;r.$runmode=O,o&&o.length&&(t=o[0],t&&t.canRun()?(r.$running=!0,e?t.runWithArgs(e):t.run(),t.isFinished()&&(o.shift(),t.complete())):o.shift(),r.$running=!1,r.$timer=N(v(s,r),r.$interval))}var u,l="prototype",c="hasOwnProperty",a=Object,f=Array,p=Function,d=p[l],h=a[l],m=f[l],$=(JSON.parse,JSON.stringify),g=function(){},v=function(n,e){return function(){return n(e)}},w=d.call.bind(m.slice),y=h.toString,b=function(n){return"function"==typeof n},S=function(n,e){return n instanceof e},N=setTimeout,k=clearTimeout,j=e,E=0,T=1,q=2,W=60,A=0,R=1,x=2,M=3,O=4,C="undefined"!=typeof Components&&"object"==typeof Components.classes&&"object"==typeof Components.classesByID&&Components.utils&&"function"==typeof Components.utils["import"],L="undefined"!=typeof global&&"[object global]"===y.call(global),I=L&&"undefined"!=typeof process.send,U=!C&&!L&&"undefined"!=typeof SharedWorkerGlobalScope&&"function"==typeof importScripts,B=!C&&!L&&"undefined"!=typeof WorkerGlobalScope&&"function"==typeof importScripts&&navigator instanceof WorkerNavigator,D=!(C||L||B||U||"undefined"==typeof navigator||"undefined"==typeof document),F=(D&&!!window.opener,D&&window.self!==window.top,"function"==typeof define&&define.amd),P=L||"function"==typeof Worker||"function"==typeof SharedWorker,_=L?this:U||B?self||this:window||this,z=L?module.$deps:U||B?self||this:window||this,G=(L?global:U||B?self||this:window||this,I||U||B),J=I&&0===process.listenerCount("message")||U&&!_.onconnect||B&&!_.onmessage,V=J?function un(n){un.handler&&un.handler(I?n:n.data)}:g,K=null,Q={},X=(L?require("os").cpus().length:4,L?null:"undefined"!=typeof _.webkitURL?_.webkitURL:"undefined"!=typeof _.URL?_.URL:null),Y=function(n,e){return n&&X?X.createObjectURL(new Blob(n.push?n:[n],e||{type:"text/javascript"})):n},H=function ln(n){var e;return L?{file:__filename,path:__dirname}:U||B?{file:e=self.location.href,path:e.split("/").slice(0,-1).join("/")}:F&&n?{file:e=n,path:e.split("/").slice(0,-1).join("/")}:D&&(e=document.getElementsByTagName("script"))&&e.length?(ln.link||(ln.link=document.createElement("a")),ln.link.href=e[e.length-1].src,e=ln.link.href,{file:e,path:e.split("/").slice(0,-1).join("/")}):{path:null,file:null}},Z=H(n.moduleUri),nn=Z.file,en=function(n,e){if(n=n||{},e)for(var t in e)e[c](t)&&(n[t]=e[t]);return n},tn=0;Q[nn]=1,L?(u=function(n){var e=this;e.process=require("child_process").fork(n),e.process.on("message",function(n){e.onmessage&&e.onmessage(n)}),e.process.on("error",function(n){e.onerror&&e.onerror(n)})},u.Shared=u,u[l]={constructor:u,process:null,onmessage:null,onerror:null,postMessage:function(n){return this.process&&this.process.send(n),this},terminate:function(){return this.process&&(this.process.kill(),this.process=null),this}}):(u=function(n){var e=this;e.process=new Worker(n),e.process.onmessage=function(n){e.onmessage&&e.onmessage(n.data)},e.process.onerror=function(n){e.onerror&&e.onerror(n)}},u.Shared=u,u[l]={constructor:u,process:null,onmessage:null,onerror:null,postMessage:function(n){return this.process&&this.process.postMessage(n),this},terminate:function(){return this.process&&(this.process.terminate(),this.process=null),this}},"function"==typeof SharedWorker&&(u.Shared=function(n){var e=this;e.process=new SharedWorker(n),e.process.port.start(),e.process.port.onmessage=function(n){e.onmessage&&e.onmessage(n.data)},e.process.port.onerror=e.process.onerror=function(n){e.onerror&&e.onerror(n)}},u.Shared[l]={constructor:u.Shared,process:null,onmessage:null,onerror:null,postMessage:function(n){return this.process&&this.process.port.postMessage(n),this},terminate:function(){return this.process&&(this.process.port.close(),this.process=null),this}})),J&&(U?(_.close=g,_.postMessage=g,_.onconnect=function(n){var e=n.ports[0];_.close=function(){e.close()},_.postMessage=function(n){e.postMessage(n)},e.addEventListener("message",V),e.start()}):B?_.onmessage=V:I&&(_.close=function(){process.exit()},_.postMessage=function(n){process.send(n)},_.importScripts=function(n){try{new Function("",require("fs").readFileSync(n).toString()).call(z)}catch(e){throw e}},process.on("message",V)));var rn=function cn(n){var e=this;return e instanceof cn?(e.$id=(++tn).toString(16),void(e.options=en({width:400,height:400,toolbar:0,location:0,directories:0,status:0,menubar:0,scrollbars:1,resizable:1},n))):new cn(n)};rn[l]={constructor:rn,options:null,$id:null,$window:null,dispose:function(){var n=this;return n.$window&&n.close(),n.$window=null,n.$id=null,n.options=null,n},close:function(){var n=this;return n.$window&&(n.$window.closed||n.$window.close(),n.$window=null),n},ready:function(n,e){var t=this,r=function o(){!t.$window||n&&!t.$window[n]?setTimeout(o,60):e()};return setTimeout(r,0),t},open:function(n){var e=this;return!e.$window&&n&&(e.$window=window.open(n.push?Y(["\ufeff"].concat(n),{type:"text/html;charset=utf-8"}):n,e.$id,t(e.options))),e},write:function(n){var e=this;return e.$window&&n&&e.$window.document.write(n),e}};var on=function an(n){if(n instanceof an)return n;if(!(this instanceof an))return new an(n);var t,r=this,o=null,i=null,s=null,u=!1,l=!1,c=!1,a=!1,f=!1,p=!1,d=null,h=0,m=1,$=null,g=null,v=null,w=e;r.queue=function(n){return arguments.length?(o=n,r):o},r.jumpNext=function(n){o&&o.jumpNext(!1,n)},r.abort=function(n){o&&(o.abort(!1),n&&(o.dispose(),o=null))},r.dispose=function(){o&&(o.dispose(),o=null)},r.task=function(n){return i=n,r},n&&r.task(n),r.run=t=function(){return i.jumpNext=r.jumpNext,i.abort=r.abort,i.dispose=r.dispose,w=i(),u=!0,i.jumpNext=null,i.abort=null,i.dispose=null,w},r.runWithArgs=function(n){return i.jumpNext=r.jumpNext,i.abort=r.abort,i.dispose=r.dispose,w=i.apply(null,n),u=!0,i.jumpNext=null,i.abort=null,i.dispose=null,w},r.canRun=function(){return i&&(!u||l||c||a||f||p)?(l||c)&&h>=$?!1:c&&!d?!1:!a&&!f||w!==g:!1},r.iif=function(n,e,t){return n?r.task(e):arguments.length>2&&r.task(t),r},r.until=function(n){return w=e,d=null,g=n,f=!0,p=!1,l=!1,c=!1,a=!1,r.run=t,r},r.untilNot=function(n){return w=e,d=null,v=n,p=!0,f=!1,l=!1,c=!1,a=!1,r.run=t,r},r.loop=function(n,t,o){return w=e,d=null,h=t||0,m=o||1,$=n,l=!0,f=!1,p=!1,c=!1,a=!1,r.run=function(){var n;return n=i(h),h+=m,w=n,u=!0,n},r},r.each=function(n){return w=e,d=n,h=0,m=1,$=n?n.length||0:0,c=!0,f=!1,p=!1,l=!1,a=!1,r.run=function(){var n;return n=i(d[h],h),h++,w=n,u=!0,n},r},r.recurse=function(n,e){return d=null,w=n,g=e,a=!0,f=!1,p=!1,l=!1,c=!1,r.run=function(){var n;return n=i(w),w=n,u=!0,n},r},r.isFinished=function(){var n=!u||p||f||l||c||a;return n&&(f||a)&&w===g&&(n=!1),n&&p&&w!==v&&(n=!1),n&&(l||c)&&h>=$&&(n=!1),!n},r.onComplete=function(n){return s=n||null,r},r.complete=function(){return s&&b(s)&&s(),r}},sn=function fn(n,e){if(S(n,on))return n;if(b(n))return new on(n);if(!S(this,fn))return new fn(n,e);var t=this;t.$interval=arguments.length?parseInt(n,10)||W:W,t.$timer=null,t.$runmode=A,t.$running=!1,t.$queue=[],J&&!1!==e&&t.initThread()};return sn.VERSION="0.5.1",sn.Thread=u,sn.Task=on,sn.BrowserWindow=rn,sn.MODE={NONE:A,INTERLEAVE:R,LINEAR:x,PARALLEL:M,SEQUENCE:O},sn.Platform={UNDEFINED:j,UNKNOWN:E,NODE:T,BROWSER:q},sn.supportsMultiThreading=function(){return P},sn.isPlatform=function(n){return T===n?L:q===n?D:e},sn.isThread=function(n,e){return e=!0===e?J:!0,T===n?e&&I:q===n?e&&(U||B):e&&G},sn.path=H,sn.blob=Y,sn.load=function(n,e,t){if(n){var r=function(){n=n.split(".");for(var e=z;n.length;)n[0]&&n[0].length&&e[n[0]]&&(e=e[n[0]]),n.shift();return e&&_!==e?b(e)?!1!==t?new e:e():e:void 0};return e&&e.length&&sn.importScripts(e.join?e.join(","):e),r()}return null},sn.listen=J?function(n){V.handler=n}:g,sn.send=J?function(n){_.postMessage(n)}:g,sn.importScripts=J?function(n){if(n&&n.length){n.split&&(n=n.split(","));for(var e=0,t=n.length;t>e;e++){var r=n[e];r&&r.length&&1!==Q[r]&&(_.importScripts(r),Q[r]=1)}}}:g,sn.close=J?function(){_.close()}:g,sn.log="undefined"!=typeof console?function(n){console.log(n||"")}:g,sn.serialize=function(n){n=n||new sn(0,!1);var e=function(e){var t=function(){var t=this,r=arguments;n.step(function(){e.apply(t,r)}),n.$running||n.run(x)};return t.free=function(){return e},t};return e.free=function(){n&&n.dispose(),n=null},e},sn[l]={constructor:sn,$interval:W,$timer:null,$queue:null,$thread:null,$events:null,$runmode:A,$running:!1,dispose:function(n){var e=this;return e.unfork(!0),e.$timer&&k(e.$timer),e.$thread=null,e.$timer=null,e.$interval=null,e.$queue=null,e.$runmode=A,e.$running=!1,J&&!0===n&&sn.close(),e},empty:function(){var n=this;return n.$timer&&k(n.$timer),n.$timer=null,n.$queue=[],n.$runmode=A,n.$running=!1,n},interval:function(n){return arguments.length?(this.$interval=parseInt(n,10)||this.$interval,this):this.$interval},fork:function(n,e,t,r){var o,i,s,l=this;if(!l.$thread){if(!P)throw l.$thread=null,new Error("Asynchronous: Multi-Threading is NOT supported!");L?(i="Asynchronous: Thread (Process): ",s="Asynchronous: Thread (Process) Error: "):(i="Asynchronous: Thread (Worker): ",s="Asynchronous: Thread (Worker) Error: "),l.$events=l.$events||{},o=l.$thread=!0===r?new u.Shared(nn):new u(nn),o.onmessage=function(n){if(n.event){var e=n.event,t=n.data||null;l.$events&&l.$events[e]?l.$events[e](t):"console.log"!==e&&"console.error"!==e||sn.log(("console.error"===e?s:i)+(t||""))}},o.onerror=function(n){if(!l.$events||!l.$events.error)throw new Error(s+n.toString());l.$events.error(n)},l.send("initThread",{component:n||null,asInstance:!1!==t,imports:e?[].concat(e).join(","):null})}return l},unfork:function(n){var e=this;return e.$thread&&(!0===n?e.$thread.terminate():e.send("dispose")),e.$thread=null,e.$events=null,e},initThread:function(){var n=this;return J&&(n.$events={},sn.listen(function(e){var t=e.event,r=e.data||null;t&&n.$events[t]?n.$events[t](r):"dispose"===t&&(n.dispose(),sn.close())})),n},listen:function(n,e){return n&&b(e)&&this.$events&&(this.$events[n]=e),this},unlisten:function(n,e){return n&&this.$events&&this.$events[n]&&(2>arguments.length||e===this.$events[n])&&delete this.$events[n],this},send:function(n,e){return n&&(J?sn.send({event:n,data:e||null}):this.$thread&&this.$thread.postMessage({event:n,data:e||null})),this},task:function(n){return S(n,on)?n:b(n)?on(n):void 0},iif:function(){var n=arguments,e=new on;return e.iif.apply(e,n)},until:function(){var n=w(arguments),e=new on(n.pop());return e.until.apply(e,n)},untilNot:function(){var n=w(arguments),e=new on(n.pop());return e.untilNot.apply(e,n)},loop:function(){var n=w(arguments),e=new on(n.pop());return e.loop.apply(e,n)},each:function(){var n=w(arguments),e=new on(n.pop());return e.each.apply(e,n)},recurse:function(){var n=w(arguments),e=new on(n.pop());return e.recurse.apply(e,n)},step:function(n){var e=this;return n&&e.$queue.push(e.task(n).queue(e)),e},steps:function(){var n,e,t=this,r=arguments;for(e=r.length,n=0;e>n;n++)t.step(r[n]);return t},jumpNext:function(n,e){var t=this,r=t.$queue;return e=e||0,!1!==n?function(){return e<r.length&&(e>0&&r.splice(0,e),t.run(t.$runmode)),t}:(e<r.length&&(e>0&&r.splice(0,e),t.run(t.$runmode)),t)},jumpNextWithArgs:function(n,e,t){var r=this,o=r.$queue;return e=e||0,!1!==n?function(){return e<o.length&&(e>0&&o.splice(0,e),r.run(r.$runmode,arguments)),r}:(e<o.length&&(e>0&&o.splice(0,e),r.run(r.$runmode,t)),r)},abort:function(n,e){var t=this;return!1!==n?function(){return e&&e>0?N(function(){t.empty()},e):t.empty(),t}:(e&&e>0?N(function(){t.empty()},e):t.empty(),t)},run:function(n,e){var t=this;return arguments.length?t.$runmode=n:n=t.$runmode,e=e||null,O===n?s(t,e):R===n?i(t,e):x===n?o(t,e):M===n&&r(t,e),t}},J&&(sn.log=function(n){sn.send({event:"console.log",data:"string"!=typeof n?$(n):n})},sn.listen(function(n){var e=n.event,t=n.data||null;switch(e){case"initThread":t&&t.component&&(K&&(b(K.dispose)&&K.dispose(),K=null),K=sn.load(t.component,t.imports,t.asInstance));break;case"dispose":K&&(b(K.dispose)&&K.dispose(),K=null),sn.close()}})),sn});