interface IEventData{
// 事件唯一ID
uuid:number;
callback:(data:any)=>void;
};
export default class Event {
static _eventUuid:number = 0;
static _eventList : {[eventName:string]:IEventData[]} = {};
static register(eventName:string, callback:(data:any)=>void) : number{
this._eventUuid = this._eventUuid + 1;
if (this._eventList[eventName] == undefined){
this._eventList[eventName] = [];
}
this._eventList[eventName][this._eventUuid] = {uuid:this._eventUuid, callback:callback};
;
return this._eventUuid;
};
static unregister(eventName:string, tag:number){
if (tag == null){
this._eventList[eventName] = undefined;
}else{
if(this._eventList[eventName] != undefined && this._eventList[eventName][tag] != undefined){
this._eventList[eventName][tag] = undefined;
}
}
};
static dispatch(eventName:string, data:any){
var eventList:IEventData[] = this._eventList[eventName];
if(eventList != undefined){
for(var key in eventList){
var eventData:IEventData = eventList[key];
if (eventData.callback != undefined){
eventData.callback(data);
};
}
};
};
}
typescript简单消息分发
最新推荐文章于 2024-09-18 08:42:56 发布