caf
Version:
Cancelable Async Flows: a wrapper to treat generators as cancelable async functions
3 lines (2 loc) • 5 kB
JavaScript
// From: https://github.com/mo/abortcontroller-polyfill
!function(t){"function"==typeof define&&define.amd?define(t):t()}((function(){"use strict";function _classCallCheck(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function _defineProperties(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function _createClass(t,e,r){return e&&_defineProperties(t.prototype,e),r&&_defineProperties(t,r),t}function _getPrototypeOf(t){return _getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function _getPrototypeOf(t){return t.__proto__||Object.getPrototypeOf(t)},_getPrototypeOf(t)}function _setPrototypeOf(t,e){return _setPrototypeOf=Object.setPrototypeOf||function _setPrototypeOf(t,e){return t.__proto__=e,t},_setPrototypeOf(t,e)}function _assertThisInitialized(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function _possibleConstructorReturn(t,e){return!e||"object"!=typeof e&&"function"!=typeof e?_assertThisInitialized(t):e}function _createSuper(t){var e=function _isNativeReflectConstruct(){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(t){return!1}}();return function _createSuperInternal(){var r,n=_getPrototypeOf(t);if(e){var o=_getPrototypeOf(this).constructor;r=Reflect.construct(n,arguments,o)}else r=n.apply(this,arguments);return _possibleConstructorReturn(this,r)}}function _get(t,e,r){return _get="undefined"!=typeof Reflect&&Reflect.get?Reflect.get:function _get(t,e,r){var n=function _superPropBase(t,e){for(;!Object.prototype.hasOwnProperty.call(t,e)&&null!==(t=_getPrototypeOf(t)););return t}(t,e);if(n){var o=Object.getOwnPropertyDescriptor(n,e);return o.get?o.get.call(r):o.value}},_get(t,e,r||t)}var t=function(){function Emitter(){_classCallCheck(this,Emitter),Object.defineProperty(this,"listeners",{value:{},writable:!0,configurable:!0})}return _createClass(Emitter,[{key:"addEventListener",value:function addEventListener(t,e,r){t in this.listeners||(this.listeners[t]=[]),this.listeners[t].push({callback:e,options:r})}},{key:"removeEventListener",value:function removeEventListener(t,e){if(t in this.listeners)for(var r=this.listeners[t],n=0,o=r.length;n<o;n++)if(r[n].callback===e)return void r.splice(n,1)}},{key:"dispatchEvent",value:function dispatchEvent(t){if(t.type in this.listeners){for(var e=this.listeners[t.type].slice(),r=0,n=e.length;r<n;r++){var o=e[r];try{o.callback.call(this,t)}catch(t){Promise.resolve().then((function(){throw t}))}o.options&&o.options.once&&this.removeEventListener(t.type,o.callback)}return!t.defaultPrevented}}}]),Emitter}(),e=function(e){!function _inherits(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),e&&_setPrototypeOf(t,e)}(AbortSignal,e);var r=_createSuper(AbortSignal);function AbortSignal(){var e;return _classCallCheck(this,AbortSignal),(e=r.call(this)).listeners||t.call(_assertThisInitialized(e)),Object.defineProperty(_assertThisInitialized(e),"aborted",{value:!1,writable:!0,configurable:!0}),Object.defineProperty(_assertThisInitialized(e),"onabort",{value:null,writable:!0,configurable:!0}),e}return _createClass(AbortSignal,[{key:"toString",value:function toString(){return"[object AbortSignal]"}},{key:"dispatchEvent",value:function dispatchEvent(t){"abort"===t.type&&(this.aborted=!0,"function"==typeof this.onabort&&this.onabort.call(this,t)),_get(_getPrototypeOf(AbortSignal.prototype),"dispatchEvent",this).call(this,t)}}]),AbortSignal}(t),r=function(){function AbortController(){_classCallCheck(this,AbortController),Object.defineProperty(this,"signal",{value:new e,writable:!0,configurable:!0})}return _createClass(AbortController,[{key:"abort",value:function abort(){var t;try{t=new Event("abort")}catch(e){"undefined"!=typeof document?document.createEvent?(t=document.createEvent("Event")).initEvent("abort",!1,!1):(t=document.createEventObject()).type="abort":t={type:"abort",bubbles:!1,cancelable:!1}}this.signal.dispatchEvent(t)}},{key:"toString",value:function toString(){return"[object AbortController]"}}]),AbortController}();"undefined"!=typeof Symbol&&Symbol.toStringTag&&(r.prototype[Symbol.toStringTag]="AbortController",e.prototype[Symbol.toStringTag]="AbortSignal"),function(t){(function polyfillNeeded(t){return t.__FORCE_INSTALL_ABORTCONTROLLER_POLYFILL?(console.log("__FORCE_INSTALL_ABORTCONTROLLER_POLYFILL=true is set, will force install polyfill"),!0):"function"==typeof t.Request&&!t.Request.prototype.hasOwnProperty("signal")||!t.AbortController})(t)&&(t.AbortController=r,t.AbortSignal=e)}("undefined"!=typeof self?self:global)}));