quick-event
Version:
quick-event is a TypeScript event library that provides tools that enable your application components to communicate with each other by dispatching events and listening for them. With eventpp you can easily implement signal/slot mechanism, or observer pat
6 lines • 14 kB
JavaScript
function _inherits(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&_setPrototypeOf(e,t)}function _setPrototypeOf(e,t){return(_setPrototypeOf=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function _createSuper(e){var t=_isNativeReflectConstruct();return function(){var n,r=_getPrototypeOf(e);if(t){var a=_getPrototypeOf(this).constructor;n=Reflect.construct(r,arguments,a)}else n=r.apply(this,arguments);return _possibleConstructorReturn(this,n)}}function _possibleConstructorReturn(e,t){return!t||"object"!==_typeof(t)&&"function"!=typeof t?_assertThisInitialized(e):t}function _assertThisInitialized(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return 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 Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(e){return!1}}function _getPrototypeOf(e){return(_getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function _toConsumableArray(e){return _arrayWithoutHoles(e)||_iterableToArray(e)||_unsupportedIterableToArray(e)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _iterableToArray(e){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(e))return Array.from(e)}function _arrayWithoutHoles(e){if(Array.isArray(e))return _arrayLikeToArray(e)}function _createForOfIteratorHelper(e,t){var n;if("undefined"==typeof Symbol||null==e[Symbol.iterator]){if(Array.isArray(e)||(n=_unsupportedIterableToArray(e))||t&&e&&"number"==typeof e.length){n&&(e=n);var r=0,a=function(){};return{s:a,n:function(){return r>=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},f:a}}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,o=!0,u=!1;return{s:function(){n=e[Symbol.iterator]()},n:function(){var e=n.next();return o=e.done,e},e:function(e){u=!0,i=e},f:function(){try{o||null==n.return||n.return()}finally{if(u)throw i}}}}function _unsupportedIterableToArray(e,t){if(e){if("string"==typeof e)return _arrayLikeToArray(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?_arrayLikeToArray(e,t):void 0}}function _arrayLikeToArray(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 _defineProperties(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 _createClass(e,t,n){return t&&_defineProperties(e.prototype,t),n&&_defineProperties(e,n),e}function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}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})(e)}
/*!
* quick-event v0.1.4
* https://github.com/ArcherGu/quick-event.git
* @license MIT
*/!function(e,t){"object"===("undefined"==typeof exports?"undefined":_typeof(exports))&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).QuickEvent={})}(this,(function(e){"use strict";var t,n=function e(t,n){_classCallCheck(this,e),this.callback=t,this.counter=n,this.previous=null,this.next=null},r=function(){function e(t){_classCallCheck(this,e),this._head=null,this._tail=null,this._currentCounter=0,t=t||{},this._canContinueInvoking=t.hasOwnProperty("canContinueInvoking")?t.canContinueInvoking:null,this._argumentsAsArray=!!t.hasOwnProperty("argumentsAsArray")&&!!t.argumentsAsArray,this._argumentsAsArray?(this._dispatch=this._dispatchArgumentsAsArray,this._applyDispatch=this._applyDispatchArgumentsAsArray):(this._dispatch=this._dispatchNotArgumentsAsArray,this._applyDispatch=this._applyDispatchNotArgumentsAsArray)}return _createClass(e,[{key:"head",get:function(){return this._head}},{key:"tail",get:function(){return this._tail}},{key:"append",value:function(e){var t=new n(e,this._getNextCounter());return this._tail?(t.previous=this._tail,this._tail.next=t,this._tail=t):(this._head=t,this._tail=t),t}},{key:"prepend",value:function(e){var t=new n(e,this._getNextCounter());return this._head?(t.next=this._head,this._head.previous=t,this._head=t):(this._head=t,this._tail=t),t}},{key:"insert",value:function(e,t){var r=this._doFindNode(t);if(!r)return this.append(e);var a=new n(e,this._getNextCounter());return a.previous=r.previous,a.next=r,r.previous&&(r.previous.next=a),r.previous=a,r===this._head&&(this._head=a),a}},{key:"remove",value:function(e){var t=this._doFindNode(e);return!!t&&(t.next&&(t.next.previous=t.previous),t.previous&&(t.previous.next=t.next),this._head===t&&(this._head=t.next),this._tail===t&&(this._tail=t.previous),t.counter=0,!0)}},{key:"empty",value:function(){return!this._head}},{key:"has",value:function(e){return!!this._doFindNode(e)}},{key:"hasAny",value:function(){return!!this._head}},{key:"forEach",value:function(e){for(var t=this._head,n=this._currentCounter;t;)0!==t.counter&&n>=t.counter&&e(t.callback),t=t.next}},{key:"forEachIf",value:function(e){for(var t=this._head,n=this._currentCounter;t;){if(0!==t.counter&&n>=t.counter&&!e(t.callback))return!1;t=t.next}return!0}},{key:"dispatch",value:function(){this._dispatch.apply(this,arguments)}},{key:"applyDispatch",value:function(){this._applyDispatch.apply(this,arguments)}},{key:"_getNextCounter",value:function(){var e=++this._currentCounter;if(0===e){for(var t=this._head;t;)t.counter=1,t=t.next;e=++this._currentCounter}return e}},{key:"_doFindNode",value:function(e){for(var t=this._head;t;){if(t===e||t.callback===e)return t;t=t.next}return null}},{key:"_dispatchArgumentsAsArray",value:function(){for(var e=this._currentCounter,t=this._head,n=this._canContinueInvoking,r=arguments.length,a=new Array(r),i=0;i<r;i++)a[i]=arguments[i];for(;t&&!(0!==t.counter&&e>=t.counter&&(t.callback.call(this,a),n&&!n.call(this,a)));)t=t.next}},{key:"_dispatchNotArgumentsAsArray",value:function(){for(var e=this._currentCounter,t=this._head,n=this._canContinueInvoking,r=arguments.length,a=new Array(r),i=0;i<r;i++)a[i]=arguments[i];for(;t&&!(0!==t.counter&&e>=t.counter&&(t.callback.apply(this,a),n&&!n.apply(this,a)));)t=t.next}},{key:"_applyDispatchArgumentsAsArray",value:function(){for(var e=this._currentCounter,t=this._head,n=this._canContinueInvoking,r=arguments.length,a=new Array(r),i=0;i<r;i++)a[i]=arguments[i];for(;t&&!(0!==t.counter&&e>=t.counter&&(t.callback.call(this,a),n&&!n.call(this,a)));)t=t.next}},{key:"_applyDispatchNotArgumentsAsArray",value:function(){for(var e=this._currentCounter,t=this._head,n=this._canContinueInvoking,r=arguments.length,a=new Array(r),i=0;i<r;i++)a[i]=arguments[i];for(;t&&!(0!==t.counter&&e>=t.counter&&(t.callback.apply(this,a),n&&!n.apply(this,a)));)t=t.next}}]),e}(),a=function(){function e(t){_classCallCheck(this,e),this.filterList=new r(t)}return _createClass(e,[{key:"appendFilter",value:function(e){return this.filterList.append(e)}},{key:"removeFilter",value:function(e){return this.filterList.remove(e)}},{key:"mixinBeforeDispatch",value:function(e){return!(!this.filterList.empty()&&!this.filterList.forEachIf((function(t){return t.call(t,e)})))}}]),e}(),i=function(){function e(){_classCallCheck(this,e)}return _createClass(e,null,[{key:"getOwnEnumerables",value:function(e){return this._getPropertyNames(e,!0,!1,this._enumerable)}},{key:"getOwnNonenumerables",value:function(e){return this._getPropertyNames(e,!0,!1,this._notEnumerable)}},{key:"getOwnEnumerablesAndNonenumerables",value:function(e){return this._getPropertyNames(e,!0,!1,this._enumerableAndNotEnumerable)}},{key:"getPrototypeEnumerables",value:function(e){return this._getPropertyNames(e,!1,!0,this._enumerable)}},{key:"getPrototypeNonenumerables",value:function(e){return this._getPropertyNames(e,!1,!0,this._notEnumerable)}},{key:"getPrototypeEnumerablesAndNonenumerables",value:function(e){return this._getPropertyNames(e,!1,!0,this._enumerableAndNotEnumerable)}},{key:"getOwnAndPrototypeEnumerables",value:function(e){return this._getPropertyNames(e,!0,!0,this._enumerable)}},{key:"getOwnAndPrototypeNonenumerables",value:function(e){return this._getPropertyNames(e,!0,!0,this._notEnumerable)}},{key:"getOwnAndPrototypeEnumerablesAndNonenumerables",value:function(e){return this._getPropertyNames(e,!0,!0,this._enumerableAndNotEnumerable)}},{key:"_enumerable",value:function(e,t){return e.propertyIsEnumerable(t)}},{key:"_notEnumerable",value:function(e,t){return!e.propertyIsEnumerable(t)}},{key:"_enumerableAndNotEnumerable",value:function(e,t){return!0}},{key:"_getPropertyNames",value:function(e,t,n,r){var a=[];do{if(t&&Object.getOwnPropertyNames(e).forEach((function(t){-1===a.indexOf(t)&&r(e,t)&&a.push(t)})),!n)break;t=!0}while(e=Object.getPrototypeOf(e));return a}}]),e}();function o(e,t){var n,r=i.getOwnAndPrototypeEnumerablesAndNonenumerables(t),a=i.getOwnAndPrototypeEnumerablesAndNonenumerables(new Object),o=_createForOfIteratorHelper(r.filter((function(e){return-1===a.indexOf(e)})));try{for(o.s();!(n=o.n()).done;){var u=n.value;e[u]=t[u]}}catch(e){o.e(e)}finally{o.f()}return e}e.ArgumentPassingMode=void 0,(t=e.ArgumentPassingMode||(e.ArgumentPassingMode={}))[t.IncludeEvent=1]="IncludeEvent",t[t.ExcludeEvent=2]="ExcludeEvent";var u=function(){function e(t){_classCallCheck(this,e),this._eventCallbackListMap={},t=t||{},this._params=t,this._getEvent="function"==typeof t.getEvent?t.getEvent:null,this._argumentPassingMode=t.argumentPassingMode?t.argumentPassingMode:e.defaultArgumentPassingMode,this.argumentsAsArray=!!t.argumentsAsArray&&!!t.argumentsAsArray,this._mixins=t.mixins?t.mixins:[];var n,r=_createForOfIteratorHelper(this._mixins);try{for(r.s();!(n=r.n()).done;){o(this,n.value)}}catch(e){r.e(e)}finally{r.f()}}return _createClass(e,[{key:"appendListener",value:function(e,t){return this._doGetCallbackList(e,!0).append(t)}},{key:"prependListener",value:function(e,t){return this._doGetCallbackList(e,!0).prepend(t)}},{key:"insertListener",value:function(e,t,n){return this._doGetCallbackList(e,!0).insert(t,n)}},{key:"removeListener",value:function(e,t){var n=this._doGetCallbackList(e,!1);return!!n&&n.remove(t)}},{key:"hasListener",value:function(e,t){var n=this._doGetCallbackList(e,!1);return!!n&&n.has(t)}},{key:"hasAnyListener",value:function(e){var t=this._doGetCallbackList(e,!1);return!!t&&t.hasAny()}},{key:"dispatch",value:function(){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];this.applyDispatch(t)}},{key:"applyDispatch",value:function(t){var n,r=_createForOfIteratorHelper(this._mixins);try{for(r.s();!(n=r.n()).done;){var a=n.value;if(a.mixinBeforeDispatch&&!a.mixinBeforeDispatch.call(this,t))return}}catch(e){r.e(e)}finally{r.f()}if(this._getEvent){var i;i=this.argumentsAsArray?this._getEvent.call(this,t):this._getEvent.apply(this,t);var o=this._doGetCallbackList(i,!1);o&&(this._argumentPassingMode===e.argumentPassingIncludeEvent&&(t=[i].concat(_toConsumableArray(t))),o.dispatch.apply(o,t))}else{var u=this._doGetCallbackList(t[0],!1);u&&(this._argumentPassingMode===e.argumentPassingExcludeEvent&&(t=Array.prototype.slice.call(t,1)),u.dispatch.apply(u,t))}}},{key:"forEach",value:function(e,t){var n=this._doGetCallbackList(e,!1);n&&n.forEach(t)}},{key:"forEachIf",value:function(e,t){var n=this._doGetCallbackList(e,!1);return!n||n.forEachIf(t)}},{key:"_doGetCallbackList",value:function(e,t){if(this._eventCallbackListMap.hasOwnProperty(e))return this._eventCallbackListMap[e];if(t){var n=new r(this._params);return this._eventCallbackListMap[e]=n,n}return null}}]),e}();u.argumentPassingIncludeEvent=e.ArgumentPassingMode.IncludeEvent,u.argumentPassingExcludeEvent=e.ArgumentPassingMode.ExcludeEvent,u.defaultArgumentPassingMode=e.ArgumentPassingMode.ExcludeEvent;var s=function(e){_inherits(n,e);var t=_createSuper(n);function n(e){var r;return _classCallCheck(this,n),(r=t.call(this,e))._queueList=[],r}return _createClass(n,[{key:"enqueue",value:function(){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];this._queueList.push(t)}},{key:"process",value:function(){var e=this._queueList;this._queueList=[];var t,n=_createForOfIteratorHelper(e);try{for(n.s();!(t=n.n()).done;){var r=t.value;this.applyDispatch(r)}}catch(e){n.e(e)}finally{n.f()}}},{key:"processOne",value:function(){this._queueList.length>0&&this.applyDispatch(this._queueList.shift())}},{key:"processIf",value:function(e){var t=this._queueList;this._queueList=[];var n,r=[],a=_createForOfIteratorHelper(t);try{for(a.s();!(n=a.n()).done;){var i=n.value;(this.argumentsAsArray?e.call(this,i):e.apply(this,i))?this.applyDispatch(i):r.push(i)}}catch(e){a.e(e)}finally{a.f()}r.length>0&&(this._queueList=this._queueList.concat(r))}},{key:"empty",value:function(){return 0===this._queueList.length}},{key:"clearEvents",value:function(){this._queueList.length=0}},{key:"peekEvent",value:function(){return this._queueList[0]}},{key:"takeEvent",value:function(){return this._queueList.length>0?this._queueList.shift():null}}]),n}(u);e.CallbackList=r,e.CallbackNode=n,e.EventDispatcher=u,e.EventQueue=s,e.MixinFilter=a,Object.defineProperty(e,"__esModule",{value:!0})}));