import { Dictionary } from "./Dictionary";
let eventMap: Dictionary<string, Array<any>> = new Dictionary<string, Array<any>>();
export default class EventManager {
public static AddListener(eventName: string, callback: any, target: any) {
let eventArr = eventMap.TryGetValue(eventName);
if (eventArr == null) {
eventArr = new Array<any>();
eventMap.Add(eventName, eventArr);
}
eventArr.push({ callback: callback, target: target });
}
public static TriggerListener(eventName: string, ...parameter: any) {
let eventArr = eventMap.TryGetValue(eventName);
if (eventArr == null)
return;
for (let i = 0; i < eventArr.length; ++i) {
let element: any = eventArr[i];
if (element)
element.callback.call(element.target, parameter);
}
}
public static RemoveListener(eventName: string, callback: any) {
let eventArr = eventMap.TryGetValue(eventName);
if (eventArr == null)
return;
for (let i = 0; i < eventArr.length; ++i) {
let element: any = eventArr[i];
if (element && element.callback === callback) {
eventArr[i] = undefined;
break;
}
}
}
public static clear() {
}
}
|