UNPKG

djipevents

Version:

A clean and opinionated event-handling library for the browser and Node.js.

21 lines (19 loc) 4.19 kB
/** * djipevents v2.0.7 * https://github.com/djipco/djipevents * Build generated on December 12th, 2022. * * © Copyright 2019-2022, Jean-Philippe Côté. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the License * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express * or implied. See the License for the specific language governing permissions and limitations under * the License. */ var djipevents=function(e){"use strict";class t{constructor(e=!1){this.eventMap={},this.eventsSuspended=1==e}addListener(e,r,i={}){if("string"==typeof e&&e.length<1||e instanceof String&&e.length<1||"string"!=typeof e&&!(e instanceof String)&&e!==t.ANY_EVENT)throw new TypeError("The 'event' parameter must be a string or EventEmitter.ANY_EVENT.");if("function"!=typeof r)throw new TypeError("The callback must be a function.");const s=new n(e,this,r,i);return this.eventMap[e]||(this.eventMap[e]=[]),i.prepend?this.eventMap[e].unshift(s):this.eventMap[e].push(s),s}addOneTimeListener(e,t,n={}){n.remaining=1,this.addListener(e,t,n)}static get ANY_EVENT(){return Symbol.for("Any event")}hasListener(e,r){if(void 0===e)return!!(this.eventMap[t.ANY_EVENT]&&this.eventMap[t.ANY_EVENT].length>0)||Object.entries(this.eventMap).some((([,e])=>e.length>0));if(this.eventMap[e]&&this.eventMap[e].length>0){if(r instanceof n){return this.eventMap[e].filter((e=>e===r)).length>0}if("function"==typeof r){return this.eventMap[e].filter((e=>e.callback===r)).length>0}return null==r}return!1}get eventNames(){return Object.keys(this.eventMap)}getListeners(e){return this.eventMap[e]||[]}suspendEvent(e){this.getListeners(e).forEach((e=>{e.suspended=!0}))}unsuspendEvent(e){this.getListeners(e).forEach((e=>{e.suspended=!1}))}getListenerCount(e){return this.getListeners(e).length}emit(e,...n){if("string"!=typeof e&&!(e instanceof String))throw new TypeError("The 'event' parameter must be a string.");if(this.eventsSuspended)return;let r=[],i=this.eventMap[t.ANY_EVENT]||[];return this.eventMap[e]&&(i=i.concat(this.eventMap[e])),i.forEach((e=>{if(e.suspended)return;let t=[...n];Array.isArray(e.arguments)&&(t=t.concat(e.arguments)),e.remaining>0&&(r.push(e.callback.apply(e.context,t)),e.count++),--e.remaining<1&&e.remove()})),r}removeListener(e,t,n={}){if(void 0===e)return void(this.eventMap={});if(!this.eventMap[e])return;let r=this.eventMap[e].filter((e=>t&&e.callback!==t||n.remaining&&n.remaining!==e.remaining||n.context&&n.context!==e.context));r.length?this.eventMap[e]=r:delete this.eventMap[e]}async waitFor(e,t={}){return t.duration=parseInt(t.duration),(isNaN(t.duration)||t.duration<=0)&&(t.duration=1/0),new Promise(((n,r)=>{let i,s=this.addListener(e,(()=>{clearTimeout(i),n()}),{remaining:1});t.duration!==1/0&&(i=setTimeout((()=>{s.remove(),r("The duration expired before the event was emitted.")}),t.duration))}))}get eventCount(){return Object.keys(this.eventMap).length}}class n{constructor(e,n,r,i={}){if("string"!=typeof e&&!(e instanceof String)&&e!==t.ANY_EVENT)throw new TypeError("The 'event' parameter must be a string or EventEmitter.ANY_EVENT.");if(!n)throw new ReferenceError("The 'target' parameter is mandatory.");if("function"!=typeof r)throw new TypeError("The 'callback' must be a function.");void 0===i.arguments||Array.isArray(i.arguments)||(i.arguments=[i.arguments]),(i=Object.assign({context:n,remaining:1/0,arguments:void 0,duration:1/0},i)).duration!==1/0&&setTimeout((()=>this.remove()),i.duration),this.arguments=i.arguments,this.callback=r,this.context=i.context,this.count=0,this.event=e,this.remaining=parseInt(i.remaining)>=1?parseInt(i.remaining):1/0,this.suspended=!1,this.target=n}remove(){this.target.removeListener(this.event,this.callback,{context:this.context,remaining:this.remaining})}}return e.EventEmitter=t,e.Listener=n,Object.defineProperty(e,"__esModule",{value:!0}),e}({}); //# sourceMappingURL=djipevents.iife.min.js.map