添加广播逻辑
This commit is contained in:
50
src/models/EventManager.ts
Normal file
50
src/models/EventManager.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
|
||||
|
||||
|
||||
export const EVENT_TYPES = {
|
||||
NOT_UPDATE_MISTAKE: "NOT_UPDATE_MISTAKE",//更新分数
|
||||
}
|
||||
|
||||
|
||||
export class EventManager {
|
||||
|
||||
private static instance: EventManager
|
||||
public static getInstance(): EventManager {
|
||||
if (!EventManager.instance) {
|
||||
EventManager.instance = new EventManager()
|
||||
}
|
||||
return EventManager.instance
|
||||
}
|
||||
|
||||
private listeners: Map<string, Array<any>> = new Map()
|
||||
|
||||
public RegisterEvent(eventID: string, func: any): void {
|
||||
if (this.listeners.has(eventID) == false) {
|
||||
this.listeners.set(eventID, new Array())
|
||||
}
|
||||
var list: Array<any> = this.listeners.get(eventID)
|
||||
var find = list.indexOf(func)
|
||||
if (find < 0) {
|
||||
list.push(func)
|
||||
}
|
||||
}
|
||||
|
||||
public UnregisterEvent(eventID: string, func: any): void {
|
||||
if (this.listeners.has(eventID)) {
|
||||
var list: Array<any> = this.listeners.get(eventID)
|
||||
var find = list.indexOf(func)
|
||||
if (find >= 0) {
|
||||
list.splice(find, 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public DispatchEvent(eventID: string, arg: any): void {
|
||||
if (this.listeners.has(eventID)) {
|
||||
var list: Array<any> = this.listeners.get(eventID)
|
||||
for (var i=0; i<list.length; i++) {
|
||||
list[i](arg)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
3
src/models/EventManager.ts.meta
Normal file
3
src/models/EventManager.ts.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"uuid": "1784429c-7fbe-47bc-8281-9a34510da81b"
|
||||
}
|
||||
Reference in New Issue
Block a user