MWSE/frontend/EventTarget.ts

32 lines
737 B
TypeScript

export default class EventTarget
{
private events : {[key:string]:Function[]} = {};
public emit(eventName :string, ...args:any[])
{
if(this.events[eventName])
{
for (const callback of this.events[eventName]) {
callback(...args);
}
}
}
public on(eventName :string, callback:Function)
{
if(this.events[eventName])
{
this.events[eventName].push(callback)
}else{
this.events[eventName] = [callback];
}
}
public activeScope : boolean = false;
scope(f:Function)
{
if(this.activeScope)
{
f()
}else{
this.on('scope', f)
}
}
}