@animech-public/playcanvas
Version:
PlayCanvas WebGL game engine
2 lines (1 loc) • 2.47 kB
JavaScript
import{EventHandle as t}from"./event-handle.js";class c{constructor(){this._callbacks=new Map,this._callbackActive=new Map}initEventHandler(){this._callbacks=new Map,this._callbackActive=new Map}_addCallback(c,l,e,a){if(this._callbacks.has(c)||this._callbacks.set(c,[]),this._callbackActive.has(c)){const t=this._callbackActive.get(c);t&&t===this._callbacks.get(c)&&this._callbackActive.set(c,t.slice())}const s=new t(this,c,l,e,a);return this._callbacks.get(c).push(s),s}on(t,c,l=this){return this._addCallback(t,c,l,!1)}once(t,c,l=this){return this._addCallback(t,c,l,!0)}off(t,c,l){if(t)this._callbackActive.has(t)&&this._callbackActive.get(t)===this._callbacks.get(t)&&this._callbackActive.set(t,this._callbackActive.get(t).slice());else for(const[t,c]of this._callbackActive)this._callbacks.has(t)&&this._callbacks.get(t)===c&&this._callbackActive.set(t,c.slice());if(t)if(c){const e=this._callbacks.get(t);if(!e)return this;for(let t=0;t<e.length;t++)e[t].callback===c&&(l&&e[t].scope!==l||(e[t].removed=!0,e.splice(t,1),t--));0===e.length&&this._callbacks.delete(t)}else{const c=this._callbacks.get(t);if(c){for(let t=0;t<c.length;t++)c[t].removed=!0;this._callbacks.delete(t)}}else{for(const t of this._callbacks.values())for(let c=0;c<t.length;c++)t[c].removed=!0;this._callbacks.clear()}return this}offByHandle(t){const c=t.name;t.removed=!0,this._callbackActive.has(c)&&this._callbackActive.get(c)===this._callbacks.get(c)&&this._callbackActive.set(c,this._callbackActive.get(c).slice());const l=this._callbacks.get(c);if(!l)return this;const e=l.indexOf(t);return-1!==e&&(l.splice(e,1),0===l.length&&this._callbacks.delete(c)),this}fire(t,c,l,e,a,s,i,h,b){if(!t)return this;const k=this._callbacks.get(t);if(!k)return this;let n;this._callbackActive.has(t)?this._callbackActive.get(t)!==k&&(n=k.slice()):this._callbackActive.set(t,k);for(let k=0;(n||this._callbackActive.get(t))&&k<(n||this._callbackActive.get(t)).length;k++){const _=(n||this._callbackActive.get(t))[k];if(_.callback&&(_.callback.call(_.scope,c,l,e,a,s,i,h,b),_._once)){const c=this._callbacks.get(t),l=c?c.indexOf(_):-1;if(-1!==l){this._callbackActive.get(t)===c&&this._callbackActive.set(t,this._callbackActive.get(t).slice());const e=this._callbacks.get(t);if(!e)continue;e[l].removed=!0,e.splice(l,1),0===e.length&&this._callbacks.delete(t)}}}return n||this._callbackActive.delete(t),this}hasEvent(t){var c;return!(null==(c=this._callbacks.get(t))||!c.length)}}export{c as EventHandler};