You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
123 lines
2.8 KiB
TypeScript
123 lines
2.8 KiB
TypeScript
|
3 years ago
|
|
||
|
|
/**
|
||
|
|
* class Emitter.
|
||
|
|
*
|
||
|
|
*/
|
||
|
|
export class LocalEmitter {
|
||
|
|
public static _instance: LocalEmitter = null;
|
||
|
|
public static get inst() {
|
||
|
|
if (LocalEmitter._instance == null) {
|
||
|
|
LocalEmitter._instance = new LocalEmitter();
|
||
|
|
}
|
||
|
|
|
||
|
|
return LocalEmitter._instance;
|
||
|
|
}
|
||
|
|
|
||
|
|
private static _g_comp_id: number = 1;
|
||
|
|
|
||
|
|
public static newCompId() {
|
||
|
|
return LocalEmitter._g_comp_id++;
|
||
|
|
}
|
||
|
|
/**
|
||
|
|
* callbacks for event.
|
||
|
|
*/
|
||
|
|
_callbacks = {};
|
||
|
|
_onceCallbacks = {};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Listen on the given `event` with `fn`.
|
||
|
|
*/
|
||
|
|
on(tag: number, event: string, fn: Function) {
|
||
|
|
let callbacks = this._callbacks[event];
|
||
|
|
if (null == callbacks) {
|
||
|
|
callbacks = this._callbacks[event] = {};
|
||
|
|
}
|
||
|
|
let calls = callbacks[tag];
|
||
|
|
if (null == calls) {
|
||
|
|
calls = callbacks[tag] = [];
|
||
|
|
}
|
||
|
|
calls.push(fn);
|
||
|
|
|
||
|
|
return this;
|
||
|
|
};
|
||
|
|
|
||
|
|
once(event: string, fn: Function) {
|
||
|
|
if(event == null){
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if(this._onceCallbacks[event] == null){
|
||
|
|
this._onceCallbacks[event] = [];
|
||
|
|
}
|
||
|
|
|
||
|
|
this._onceCallbacks[event].push(fn);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Remove the given callback for `event` or all
|
||
|
|
* registered callbacks.
|
||
|
|
*/
|
||
|
|
off(tag: number = null, event: string = null, fn: Function = null) {
|
||
|
|
// all
|
||
|
|
if (null == tag) {
|
||
|
|
this._callbacks = {};
|
||
|
|
return this;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (null == event) {
|
||
|
|
for (let event in this._callbacks) {
|
||
|
|
delete this._callbacks[event][tag];
|
||
|
|
}
|
||
|
|
return this;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (null == fn) {
|
||
|
|
if (null != this._callbacks[event]) {
|
||
|
|
delete this._callbacks[event][tag];
|
||
|
|
}
|
||
|
|
return this;
|
||
|
|
}
|
||
|
|
|
||
|
|
// specific event
|
||
|
|
let callbacks = this._callbacks[event];
|
||
|
|
if (!callbacks) return this;
|
||
|
|
|
||
|
|
let calls = callbacks[tag];
|
||
|
|
if (!calls) return this;
|
||
|
|
|
||
|
|
// remove specific handler
|
||
|
|
let i = calls.indexOf(fn);
|
||
|
|
if (~i) calls.splice(i, 1);
|
||
|
|
|
||
|
|
return this;
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Emit `event` with the given args.
|
||
|
|
*
|
||
|
|
*/
|
||
|
|
emit(event: string, obj: any = null, obj2: any = null) {
|
||
|
|
|
||
|
|
let callbacks = this._callbacks[event];
|
||
|
|
let onceCallback = this._onceCallbacks[event];
|
||
|
|
|
||
|
|
if (null != callbacks) {
|
||
|
|
for (let id in callbacks) {
|
||
|
|
let calls = callbacks[id];
|
||
|
|
for (let i = 0, len = calls.length; i < len; ++i) {
|
||
|
|
calls[i].call(this, obj, obj2);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (onceCallback != null) {
|
||
|
|
onceCallback.filter((callback)=>{
|
||
|
|
callback(null, obj, obj2);
|
||
|
|
})
|
||
|
|
|
||
|
|
delete this._onceCallbacks[event];
|
||
|
|
}
|
||
|
|
|
||
|
|
return this;
|
||
|
|
};
|
||
|
|
}
|