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