-
Notifications
You must be signed in to change notification settings - Fork 1
/
macrotask.min.js
1 lines (1 loc) · 5.25 KB
/
macrotask.min.js
1
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(e.macrotask=e.macrotask||{})}(this,function(e){"use strict";function t(){if(!c.postMessage||c.importScripts)return!1;if(c.setImmediate)return!1;var e=!0,t=c.onmessage;return c.onmessage=function(){e=!1},c.postMessage("","*"),c.onmessage=t,e}function n(e){function t(t){t.source===c&&t.data===n&&e(new p)}var n="macrotask"+Math.random();return c.addEventListener?c.addEventListener("message",t,!1):c.attachEvent("onmessage",t),function(){c.postMessage(n,"*")}}function r(){return!c.setImmediate&&"undefined"!=typeof c.MessageChannel}function i(e){var t=new c.MessageChannel;return t.port1.onmessage=function(){return e(new p)},function(){t.port2.postMessage(0)}}function o(e){return function(){setTimeout(function(){e(new p)},0)}}function a(e){if(!S.length)return void(T=!1);var t=S.shift();if(S.length?A():T=!1,t.run(),e)for(;e.timeRemaining()>0&&S.length;){var n=S.shift();n.run()}}function s(e){for(var t=arguments.length,n=Array(t>1?t-1:0),r=1;r<t;r++)n[r-1]=arguments[r];var i=S.push(e,n);return T?i:(A||(A=O(a)),T=!0,A(),i)}function u(e){return S.cancel(e)}var c="undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{},l=!0,f=(function(){function e(e){this.value=e}function t(t){function n(e,t){return new Promise(function(n,i){var s={key:e,arg:t,resolve:n,reject:i,next:null};a?a=a.next=s:(o=a=s,r(e,t))})}function r(n,o){try{var a=t[n](o),s=a.value;s instanceof e?Promise.resolve(s.value).then(function(e){r("next",e)},function(e){r("throw",e)}):i(a.done?"return":"normal",a.value)}catch(e){i("throw",e)}}function i(e,t){switch(e){case"return":o.resolve({value:t,done:!0});break;case"throw":o.reject(t);break;default:o.resolve({value:t,done:!1})}o=o.next,o?r(o.key,o.arg):a=null}var o,a;this._invoke=n,"function"!=typeof t.return&&(this.return=void 0)}return"function"==typeof Symbol&&Symbol.asyncIterator&&(t.prototype[Symbol.asyncIterator]=function(){return this}),t.prototype.next=function(e){return this._invoke("next",e)},t.prototype.throw=function(e){return this._invoke("throw",e)},t.prototype.return=function(e){return this._invoke("return",e)},{wrap:function(e){return function(){return new t(e.apply(this,arguments))}},await:function(t){return new e(t)}}}(),function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}),h=void 0,p=function(){function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:10;f(this,e),this.limit=t,this.performance=e.getPerformance(),this.now=this.getTime()}return e.prototype.getTime=function(){return this.performance?c.performance.now():Date.now()},e.prototype.timeRemaining=function(){var e=this.limit-(this.getTime()-this.now);return e>0?e:0},e.getPerformance=function(){return void 0===h&&(h=!(!c.performance||!c.performance.now||"function"!=typeof c.performance.now)),h},e}(),d=function(){return!l&&c&&"function"==typeof c.setImmediate},v=function(e){return function(){return c.setImmediate(function(){return e(new p)})}},m=Object.freeze({test:d,install:v}),y=Object.freeze({test:t,install:n}),g=Object.freeze({test:r,install:i}),w=function(){return"document"in c&&"onreadystatechange"in c.document.createElement("script")},b=function(e){return function(){var t=c.document.createElement("script");return t.onreadystatechange=function(){e(new p),t.onreadystatechange=null,t.parentNode.removeChild(t),t=null},c.document.documentElement.appendChild(t),e}},k=Object.freeze({test:w,install:b}),x=function(){return!0},j=Object.freeze({test:x,install:o}),M=function(){return"function"==typeof c.requestIdleCallback},I=function(e){return function(){return c.requestIdleCallback(e)}},C=Object.freeze({test:M,install:I}),E=[m,C,y,g,k,j],O=function(e){for(var t=E,n=Array.isArray(t),r=0,t=n?t:t[Symbol.iterator]();;){var i;if(n){if(r>=t.length)break;i=t[r++]}else{if(r=t.next(),r.done)break;i=r.value}var o=i;if(o.test())return o.install(e)}},z=function e(){f(this,e)},_=function(){function e(t,n,r){f(this,e),this.fun=t,this.array=n,this.token=new z,this.prev=null,this.next=null,this.list=r}return e.prototype.run=function(){var e=this.fun,t=this.array;switch(t.length){case 0:e();break;case 1:e(t[0]);break;case 2:e(t[0],t[1]);break;case 3:e(t[0],t[1],t[2]);break;default:e.apply(void 0,t)}},e.prototype.cancel=function(){var e=this.next,t=this.prev;if(null===e)if(null===t){if(this.list.head!==this||1!==this.list.length)return;this.list.head=this.list.tail=null}else t.next=null,this.list.tail=t;else null===t?(e.prev=null,this.list.head=e):(t.next=e,e.prev=t);this.list.length--},e}(),P=function(){function e(){f(this,e),this.length=0,this.head=null,this.tail=null,this.cache=new WeakMap}return e.prototype.push=function(e,t){var n=new _(e,t,this);if(this.length>0){var r=this.tail;r.next=n,n.prev=r,this.tail=n}else this.head=this.tail=n;return this.length++,this.cache.set(n.token,n),n.token},e.prototype.shift=function(){if(!(this.length<1)){var e=this.head;if(1===this.length)this.head=this.tail=null;else{var t=e.next;t.prev=null,this.head=t}return this.length--,this.cache.delete(e.token),e}},e.prototype.cancel=function(e){var t=this.cache.get(e);return!!t&&(this.cache.delete(e),t.cancel(),!0)},e}(),S=new P,T=!1,A=void 0;e.run=s,e.clear=u,Object.defineProperty(e,"__esModule",{value:!0})});