-
Notifications
You must be signed in to change notification settings - Fork 13
/
zousan-esm-min.js
1 lines (1 loc) · 3.14 KB
/
zousan-esm-min.js
1
const _undefined=void 0,STATE_PENDING=_undefined,STATE_FULFILLED="fulfilled",STATE_REJECTED="rejected",_undefinedString="undefined",soon=(()=>{const e=[],t=1024;let n=0;function o(){for(;e.length-n;){try{e[n]()}catch(e){Zousan.error(e)}e[n++]=_undefined,n==t&&(e.splice(0,t),n=0)}}const s=(()=>{if("undefined"!=typeof MutationObserver){const e=document.createElement("div");return new MutationObserver(o).observe(e,{attributes:!0}),function(){e.setAttribute("a",0)}}return"undefined"!=typeof process&&"function"==typeof process.nextTick?function(){process.nextTick(o)}:"undefined"!=typeof setImmediate?function(){setImmediate(o)}:function(){setTimeout(o,0)}})();return t=>{e.push(t),e.length-n==1&&s()}})();function Zousan(e){if(!(this instanceof Zousan))throw new TypeError("Zousan must be created with the new keyword");if("function"==typeof e){const t=this;try{e(e=>t.resolve(e),e=>t.reject(e))}catch(e){t.reject(e)}}else if(arguments.length>0)throw new TypeError("Zousan resolver "+e+" is not a function")}function resolveClient(e,t){if("function"==typeof e.y)try{const n=e.y.call(_undefined,t);e.p.resolve(n)}catch(t){e.p.reject(t)}else e.p.resolve(t)}function rejectClient(e,t){if("function"==typeof e.n)try{const n=e.n.call(_undefined,t);e.p.resolve(n)}catch(t){e.p.reject(t)}else e.p.reject(t)}Zousan.prototype={resolve:function(e){if(this.state!==STATE_PENDING)return;if(e===this)return this.reject(new TypeError("Attempt to resolve promise with self"));const t=this;if(e&&("function"==typeof e||"object"==typeof e)){let n=!0;try{const o=e.then;if("function"==typeof o)return void o.call(e,function(e){n&&(n=!1,t.resolve(e))},function(e){n&&(n=!1,t.reject(e))})}catch(e){return void(n&&this.reject(e))}}this.state="fulfilled",this.v=e,t.c&&soon(function(){for(let n=0,o=t.c.length;n<o;n++)resolveClient(t.c[n],e)})},reject:function(e){if(this.state!==STATE_PENDING)return;const t=this;this.state="rejected",this.v=e;const n=this.c;soon(n?function(){for(let t=0,o=n.length;t<o;t++)rejectClient(n[t],e)}:function(){t.handled||Zousan.suppressUncaughtRejectionError||Zousan.warn("You upset Zousan. Please catch rejections: ",e,e?e.stack:null)})},then:function(e,t){const n=new Zousan,o={y:e,n:t,p:n};if(this.state===STATE_PENDING)this.c?this.c.push(o):this.c=[o];else{const e=this.state,t=this.v;this.handled=!0,soon(function(){"fulfilled"===e?resolveClient(o,t):rejectClient(o,t)})}return n},catch:function(e){return this.then(null,e)},finally:function(e){return this.then(e,e)},timeout:function(e,t){t=t||"Timeout";const n=this;return new Zousan(function(o,s){setTimeout(function(){s(Error(t))},e),n.then(function(e){o(e)},function(e){s(e)})})}},Zousan.resolve=(e=>new Zousan(t=>t(e))),Zousan.reject=function(e){const t=new Zousan;return t.c=[],t.reject(e),t},Zousan.all=function(e){const t=[],n=new Zousan;let o=0;function s(s,i){s&&"function"==typeof s.then||(s=Zousan.resolve(s)),s.then(function(s){t[i]=s,++o==e.length&&n.resolve(t)},function(e){n.reject(e)})}for(let t=0;t<e.length;t++)s(e[t],t);return e.length||n.resolve(t),n};const nop=()=>{};Zousan.warn="undefined"!=typeof console?console.warn:nop,Zousan.error="undefined"!=typeof console?console.error:nop,Zousan.soon=soon;export default Zousan;