cancelable-promise
Version:
A simple cancelable promise
2 lines • 6.48 kB
JavaScript
function _typeof(e){return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},_typeof(e)}!function(e,t){if("function"==typeof define&&define.amd)define(["exports"],t);else if("undefined"!=typeof exports)t(exports);else{var n={exports:{}};t(n.exports),e.CancelablePromise=n.exports}}("undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:this,(function(e){"use strict";function t(e,n){return t=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},t(e,n)}function n(e){var t=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(e){return!1}}();return function(){var n,i=o(e);if(t){var u=o(this).constructor;n=Reflect.construct(i,arguments,u)}else n=i.apply(this,arguments);return r(this,n)}}function r(e,t){if(t&&("object"===_typeof(t)||"function"==typeof t))return t;if(void 0!==t)throw new TypeError("Derived constructors may only return object or undefined");return function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(e)}function o(e){return o=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(e){return e.__proto__||Object.getPrototypeOf(e)},o(e)}function i(e,t){var n="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!n){if(Array.isArray(e)||(n=function(e,t){if(!e)return;if("string"==typeof e)return u(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 u(e,t)}(e))||t&&e&&"number"==typeof e.length){n&&(e=n);var r=0,o=function(){};return{s:o,n:function(){return r>=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},f:o}}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 i,c=!0,a=!1;return{s:function(){n=n.call(e)},n:function(){var e=n.next();return c=e.done,e},e:function(e){a=!0,i=e},f:function(){try{c||null==n.return||n.return()}finally{if(a)throw i}}}}function u(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 c(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function a(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)}}function f(e,t,n){return t&&a(e.prototype,t),n&&a(e,n),Object.defineProperty(e,"prototype",{writable:!1}),e}function l(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function s(e,t,n){!function(e,t){if(t.has(e))throw new TypeError("Cannot initialize the same private elements twice on an object")}(e,t),t.set(e,n)}function y(e,t){return function(e,t){if(t.get)return t.get.call(e);return t.value}(e,h(e,t,"get"))}function p(e,t,n){return function(e,t,n){if(t.set)t.set.call(e,n);else{if(!t.writable)throw new TypeError("attempted to set read only private field");t.value=n}}(e,h(e,t,"set"),n),n}function h(e,t,n){if(!t.has(e))throw new TypeError("attempted to "+n+" private field on non-instance");return t.get(e)}Object.defineProperty(e,"__esModule",{value:!0}),e.CancelablePromise=void 0,e.cancelable=P,e.default=void 0,e.isCancelablePromise=g;var d="undefined"!=typeof Symbol?Symbol.toStringTag:"@@toStringTag",b=new WeakMap,v=new WeakMap,m=function(){function e(t){var n=t.executor,r=void 0===n?function(){}:n,o=t.internals,i=void 0===o?{isCanceled:!1,onCancelList:[]}:o,u=t.promise,a=void 0===u?new Promise((function(e,t){return r(e,t,(function(e){i.onCancelList.push(e)}))})):u;c(this,e),s(this,b,{writable:!0,value:void 0}),s(this,v,{writable:!0,value:void 0}),l(this,d,"CancelablePromise"),this.cancel=this.cancel.bind(this),p(this,b,i),p(this,v,a||new Promise((function(e,t){return r(e,t,(function(e){i.onCancelList.push(e)}))})))}return f(e,[{key:"then",value:function(e,t){return O(y(this,v).then(j(e,y(this,b)),j(t,y(this,b))),y(this,b))}},{key:"catch",value:function(e){return O(y(this,v).catch(j(e,y(this,b))),y(this,b))}},{key:"finally",value:function(e,t){var n=this;return t&&y(this,b).onCancelList.push(e),O(y(this,v).finally(j((function(){if(e)return t&&(y(n,b).onCancelList=y(n,b).onCancelList.filter((function(t){return t!==e}))),e()}),y(this,b))),y(this,b))}},{key:"cancel",value:function(){y(this,b).isCanceled=!0;var e=y(this,b).onCancelList;y(this,b).onCancelList=[];var t,n=i(e);try{for(n.s();!(t=n.n()).done;){var r=t.value;if("function"==typeof r)try{r()}catch(e){console.error(e)}}}catch(e){n.e(e)}finally{n.f()}}},{key:"isCanceled",value:function(){return!0===y(this,b).isCanceled}}]),e}(),w=function(e){!function(e,n){if("function"!=typeof n&&null!==n)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(n&&n.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),Object.defineProperty(e,"prototype",{writable:!1}),n&&t(e,n)}(o,e);var r=n(o);function o(e){return c(this,o),r.call(this,{executor:e})}return f(o)}(m);e.CancelablePromise=w,l(w,"all",(function(e){return S(e,Promise.all(e))})),l(w,"allSettled",(function(e){return S(e,Promise.allSettled(e))})),l(w,"any",(function(e){return S(e,Promise.any(e))})),l(w,"race",(function(e){return S(e,Promise.race(e))})),l(w,"resolve",(function(e){return P(Promise.resolve(e))})),l(w,"reject",(function(e){return P(Promise.reject(e))})),l(w,"isCancelable",g);var C=w;function P(e){return O(e,{isCanceled:!1,onCancelList:[]})}function g(e){return e instanceof w||e instanceof m}function j(e,t){if(e)return function(n){if(!t.isCanceled){var r=e(n);return g(r)&&t.onCancelList.push(r.cancel),r}return n}}function O(e,t){return new m({internals:t,promise:e})}function S(e,t){var n={isCanceled:!1,onCancelList:[]};return n.onCancelList.push((function(){var t,n=i(e);try{for(n.s();!(t=n.n()).done;){var r=t.value;g(r)&&r.cancel()}}catch(e){n.e(e)}finally{n.f()}})),new m({internals:n,promise:t})}e.default=C}));
//# sourceMappingURL=CancelablePromise.min.js.map