UNPKG

breakable-promise

Version:

Project breakable-promise

2 lines (1 loc) 7.24 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).breakablePromise={})}(this,(function(e){"use strict";var t=function(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r};var n=function(e){if(Array.isArray(e))return t(e)};var r=function(e){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(e))return Array.from(e)};var i=function(e,n){if(e){if("string"==typeof e)return t(e,n);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?t(e,n):void 0}};var o=function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")};var a=function(e){return n(e)||r(e)||i(e)||o()};var u=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")};function f(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}var l=function(e,t,n){return t&&f(e.prototype,t),n&&f(e,n),e};var c=function(e,t){var n=t.get(e);if(!n)throw new TypeError("attempted to get private field on non-instance");return n.get?n.get.call(e):n.value};var s=function(e,t,n){var r=t.get(e);if(!r)throw new TypeError("attempted to set private field on non-instance");if(r.set)r.set.call(e,n);else{if(!r.writable)throw new TypeError("attempted to set read only private field");r.value=n}return n},h="function"==typeof setImmediate?setImmediate:function(e){setTimeout(e,0)},v=p,y=p;function p(e){return!!e&&("object"==typeof e||"function"==typeof e)&&"function"==typeof e.then}v.default=y;var w,d,b=function(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e},E=function(){function e(){u(this,e),b(this,"events",{}),b(this,"listenerId",Math.round(1e12*Math.random()))}return l(e,[{key:"on",value:function(e,t){return this.events[e]=this.events[e]||[],this.events[e].push(t),this}},{key:"off",value:function(e,t){if("string"!=typeof e)for(var n in this.events)this.events[n].length=0;else"function"!=typeof t?this.events[e]&&(this.events[e].length=0,delete this.events[e]):this.events[e]&&(this.events[e].splice(this.events[e].indexOf(t),1),0===this.events[e].length&&delete this.events[e]);return this}},{key:"once",value:function(e,t){var n=this;return this.on(e,(function r(){for(var i=arguments.length,o=new Array(i),a=0;a<i;a++)o[a]=arguments[a];t.apply(n,o),h((function(){n.off(e,r)}))})),this}},{key:"emit",value:function(e){for(var t=this,n=arguments.length,r=new Array(n>1?n-1:0),i=1;i<n;i++)r[i-1]=arguments[i];this.events[e]&&this.events[e].forEach((function(e){e.apply(t,r)}))}}]),e}(),m=function(){function e(t){u(this,e),b(this,"_error",null),b(this,"isCaught",!1),null!=t&&(this.error=t)}return l(e,[{key:"error",set:function(e){this._error=e},get:function(){return this.isCaught=!0,this._error}}]),e}();function g(e,t){var n;if("undefined"==typeof Symbol||null==e[Symbol.iterator]){if(Array.isArray(e)||(n=function(e,t){if(!e)return;if("string"==typeof e)return k(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);"Object"===n&&e.constructor&&(n=e.constructor.name);if("Map"===n||"Set"===n)return Array.from(e);if("Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return k(e,t)}(e))||t&&e&&"number"==typeof e.length){n&&(e=n);var r=0,i=function(){};return{s:i,n:function(){return r>=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},f:i}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o,a=!0,u=!1;return{s:function(){n=e[Symbol.iterator]()},n:function(){var e=n.next();return a=e.done,e},e:function(e){u=!0,o=e},f:function(){try{a||null==n.return||n.return()}finally{if(u)throw o}}}}function k(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}!function(e){e[e.PENDING=0]="PENDING",e[e.FULFILLED=1]="FULFILLED",e[e.REJECTED=2]="REJECTED",e[e.BROKEN=3]="BROKEN"}(w||(w={})),function(e){e.SUCCESS="promisesuccess",e.FAILED="promisefailed",e.BREAKABLE="promisebreak"}(d||(d={}));var A=new WeakMap,S=new WeakMap,L=new WeakMap,I=new WeakMap,B=new WeakMap,C=new WeakMap,D=new WeakMap,M=new WeakMap,j=new WeakMap,R=new WeakMap,T=new WeakMap,F=function(){function e(t){var n=this;u(this,e),A.set(this,{writable:!0,value:void 0}),S.set(this,{writable:!0,value:!1}),L.set(this,{writable:!0,value:null}),I.set(this,{writable:!0,value:void 0}),B.set(this,{writable:!0,value:void 0}),C.set(this,{writable:!0,value:void 0}),D.set(this,{writable:!0,value:function(){return c(n,A)===w.FULFILLED}}),M.set(this,{writable:!0,value:function(){return c(n,A)===w.PENDING}}),j.set(this,{writable:!0,value:function(e,t,r){var i;n.isBroken()||("function"==typeof r&&(i=r(c(n,D).call(n)?c(n,I):c(n,B).error)),v(i)?(i.then(e,t),c(n,D).call(n)||i.catch(t)):c(n,D).call(n)?e(i):t(void 0!==i?i:c(n,B)._error))}}),R.set(this,{writable:!0,value:function(t,r,i){return new e((function(e,o,a){if(c(n,M).call(n)){var u=function(){c(n,j).call(n,e,o,t)};c(n,C).once(d.SUCCESS,u).once(d.FAILED,(function(){c(n,C).off(d.SUCCESS,u),c(n,j).call(n,e,o,r)}))}else c(n,j).call(n,e,o,c(n,D).call(n)?t:r);"function"==typeof a&&a(i)}))}}),T.set(this,{writable:!0,value:function(t,r){return new e((function(e,i,o){c(n,M).call(n)||c(n,D).call(n)?c(n,C).once(d.FAILED,(function(){c(n,j).call(n,e,i,t)})):c(n,j).call(n,e,i,t),"function"==typeof o&&o(r)}))}}),s(this,A,w.PENDING),s(this,I,null),s(this,B,new m),s(this,C,new E);try{t.call(this,(function(e){h((function(){n.isBroken()||(s(n,I,e),s(n,A,w.FULFILLED),c(n,C).emit(d.SUCCESS),c(n,C).off(d.BREAKABLE))}))}),(function(e){h((function(){n.isBroken()||(s(n,B,new m(e)),s(n,A,w.REJECTED),c(n,C).emit(d.FAILED),c(n,C).off(d.BREAKABLE))}))}),(function(e){"function"==typeof e?(s(n,S,!0),s(n,L,e),c(n,C).emit(d.BREAKABLE,c(n,L))):c(n,C).off(d.BREAKABLE)}))}catch(e){s(this,A,w.REJECTED),s(this,B,new m(e))}}return l(e,[{key:"then",value:function(e,t,n){return c(this,R).call(this,e,t,n)}},{key:"catch",value:function(e,t){return c(this,T).call(this,e,t)}},{key:"break",value:function(){var t=this;return new e((function(e,n){c(t,S)?"function"==typeof c(t,L)?(c(t,L).call(t),e()):c(t,C).once(d.BREAKABLE,(function(t){t(),e()})):n()}))}},{key:"isBroken",value:function(){return c(this,A)===w.BROKEN}}],[{key:"resolve",value:function(t){return new e((function(e){e(t)}))}},{key:"reject",value:function(t){return new e((function(e,n){n(t)}))}},{key:"all",value:function(t,n){return new e((function(e,r,i){try{!function(){var r,o=a(t),u=[],f=g(o);try{for(f.s();!(r=f.n()).done;){r.value.then((function(e){u.push(e)})).catch((function(e){throw new m(e)}))}}catch(e){f.e(e)}finally{f.f()}e(u),"function"==typeof i&&i(n)}()}catch(e){r(e.message)}}))}}]),e}();e.BreakablePromise=F,Object.defineProperty(e,"__esModule",{value:!0})}));