UNPKG

@polyv/utils

Version:

Utility functions of Polyv frontend development.

1 lines 595 B
export class EventEmitter{constructor(){this.__eventStore={}}__addOnEvent(t,e,n,o){if(!e||!n)return;let r=this.__eventStore[e];r||(r=[]),r.push({type:t,handler:n,callbackHandler:n.bind(o)}),this.__eventStore[e]=r}on(t,e,n=this){this.__addOnEvent("normal",t,e,n)}once(t,e,n=this){this.__addOnEvent("once",t,e,n)}off(t,e){let n=this.__eventStore[t];n&&(n=n.filter((t=>t.handler!==e)),this.__eventStore[t]=n)}emit(t,e){const n=this.__eventStore[t];n&&n.forEach((n=>{const{type:o,handler:r,callbackHandler:_}=n;"function"==typeof _&&_(e),"once"===o&&this.off(t,r)}))}destroy(){this.__eventStore={}}}