mirror of
https://github.com/unanmed/HumanBreak.git
synced 2025-01-19 20:59:37 +08:00
feat: 自定义emitter
This commit is contained in:
parent
8a8dee3176
commit
9271e25dd9
@ -15,12 +15,21 @@ interface ListenerOptions {
|
|||||||
immediate: boolean;
|
immediate: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type EmitFn<F extends (...params: any) => any> = (
|
||||||
|
events: Listener<F>[],
|
||||||
|
...params: Parameters<F>
|
||||||
|
) => any;
|
||||||
|
|
||||||
export class EventEmitter<T extends EmitableEvent = {}> {
|
export class EventEmitter<T extends EmitableEvent = {}> {
|
||||||
private events: {
|
private events: {
|
||||||
[P in keyof T]?: Listener<T[P]>[];
|
[P in keyof T]?: Listener<T[P]>[];
|
||||||
} = {};
|
} = {};
|
||||||
private emitted: (keyof T)[] = [];
|
private emitted: (keyof T)[] = [];
|
||||||
|
|
||||||
|
private emitter: {
|
||||||
|
[P in keyof T]?: EmitFn<T[P]>;
|
||||||
|
} = {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 监听某个事件
|
* 监听某个事件
|
||||||
* @param event 要监听的事件类型
|
* @param event 要监听的事件类型
|
||||||
@ -74,21 +83,40 @@ export class EventEmitter<T extends EmitableEvent = {}> {
|
|||||||
* @param event 要触发的事件类型
|
* @param event 要触发的事件类型
|
||||||
* @param params 传入的参数
|
* @param params 传入的参数
|
||||||
*/
|
*/
|
||||||
emit<K extends keyof T>(event: K, ...params: Parameters<T[K]>) {
|
emit<K extends keyof T>(
|
||||||
|
event: K,
|
||||||
|
...params: Parameters<T[K]>
|
||||||
|
): ReturnType<T[K]>[];
|
||||||
|
emit<K extends keyof T, R>(event: K, ...params: Parameters<T[K]>): R;
|
||||||
|
emit<K extends keyof T>(event: K, ...params: Parameters<T[K]>): any {
|
||||||
if (!this.emitted.includes(event)) {
|
if (!this.emitted.includes(event)) {
|
||||||
this.emitted.push(event);
|
this.emitted.push(event);
|
||||||
}
|
}
|
||||||
const events = (this.events[event] ??= []);
|
const events = (this.events[event] ??= []);
|
||||||
for (let i = 0; i < events.length; i++) {
|
if (!!this.emitter[event]) {
|
||||||
const e = events[i];
|
const returns = this.emitter[event]!(events, ...params);
|
||||||
e.fn(...(params as any));
|
this.events[event] = events.filter(v => !v.once);
|
||||||
if (e.once) {
|
return returns;
|
||||||
events.splice(i, 1);
|
} else {
|
||||||
i--;
|
const returns: ReturnType<T[K]>[] = [];
|
||||||
|
for (let i = 0; i < events.length; i++) {
|
||||||
|
const e = events[i];
|
||||||
|
returns.push(e.fn(...(params as any)));
|
||||||
}
|
}
|
||||||
|
this.events[event] = events.filter(v => !v.once);
|
||||||
|
return returns;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置一个事件的执行器(emitter)
|
||||||
|
* @param event 要设置的事件
|
||||||
|
* @param fn 事件执行器,留空以清除触发器
|
||||||
|
*/
|
||||||
|
setEmitter<K extends keyof T>(event: K, fn?: EmitFn<T[K]>) {
|
||||||
|
this.emitter[event] = fn;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 取消监听所有的事件,删除所有监听函数
|
* 取消监听所有的事件,删除所有监听函数
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user