import { EventDispatcher } from "three";

class EventManager extends EventDispatcher{
    forwardEvents(object){
        this.emits?.forEach(e=>{
            this.addEventListener(e, object.dispatchEvent.bind(object))
        })
    }
    once(type, listener){
        let wrapper = function(event){
            listener.call(this, event);
            this.removeEventListener(event.type, wrapper);
        }
        this.addEventListener(type, wrapper);
    }
    removeAllListenersOfType(type){
        this._listeners?.[ type ]?.splice(0, this._listeners[ type ].length)
    }
    removeAllListeners(){
        this._listeners && Object.keys(this._listeners).forEach(k=>this.removeAllListenersOfType(k));
    }
    dispose(){
        this.removeAllListeners();
    }
}

export { EventManager }