HumanBreak/src/core/common/eventEmitter.ts

212 lines
6.2 KiB
TypeScript
Raw Normal View History

2024-02-02 17:10:21 +08:00
function has<T>(value: T): value is NonNullable<T> {
return value !== null && value !== undefined;
}
export type Callable = (...params: any) => any;
2023-06-04 23:06:56 +08:00
2024-01-18 16:20:50 +08:00
export interface Listener<T extends (...params: any) => any> {
2023-06-04 23:06:56 +08:00
fn: T;
once?: boolean;
2023-06-28 22:48:15 +08:00
immediate?: boolean;
2023-06-04 23:06:56 +08:00
}
2024-01-18 16:20:50 +08:00
export interface ListenerOptions {
2023-06-04 23:06:56 +08:00
once: boolean;
2023-06-28 22:48:15 +08:00
immediate: boolean;
2023-06-04 23:06:56 +08:00
}
2023-11-14 22:20:30 +08:00
type EmitFn<F extends (...params: any) => any> = (
events: Listener<F>[],
...params: Parameters<F>
) => any;
type Key = number | string | symbol;
export class EventEmitter<T extends Record<keyof T, Callable>> {
2024-01-18 16:20:50 +08:00
protected events: {
[x: Key]: Listener<Callable>[];
2023-06-04 23:06:56 +08:00
} = {};
private emitted: Set<string> = new Set();
2023-06-04 23:06:56 +08:00
2024-01-18 16:20:50 +08:00
protected emitter: {
[x: Key]: EmitFn<Callable> | undefined;
2023-11-14 22:20:30 +08:00
} = {};
2023-06-04 23:06:56 +08:00
/**
*
* @param event
* @param fn
* @param options
*/
on<K extends keyof T>(
event: K,
fn: T[K],
options?: Partial<ListenerOptions>
): void;
on(event: string, fn: Callable, options?: Partial<ListenerOptions>): void;
on(event: string, fn: Callable, options?: Partial<ListenerOptions>): void {
if (options?.immediate && this.emitted.has(event)) {
2023-06-28 22:48:15 +08:00
fn();
if (!options.once) {
this.events[event] ??= [];
this.events[event]?.push({
fn
});
}
return;
}
2023-06-04 23:06:56 +08:00
this.events[event] ??= [];
this.events[event]?.push({
fn,
once: options?.once
});
}
/**
*
* @param event
* @param fn
*/
off<K extends keyof T>(event: K, fn: T[K]): void;
off(event: string, fn: Callable): void;
off(event: string, fn: Callable): void {
2023-06-04 23:06:56 +08:00
const index = this.events[event]?.findIndex(v => v.fn === fn);
if (index === -1 || index === void 0) return;
this.events[event]?.splice(index, 1);
}
/**
*
* @param event
* @param fn
*/
once<K extends keyof T>(event: K, fn: T[K]): void;
once(event: string, fn: Callable): void;
once(event: string, fn: Callable): void {
2023-06-04 23:06:56 +08:00
this.on(event, fn, { once: true });
}
/**
*
* @param event
* @param params
*/
2023-11-14 22:20:30 +08:00
emit<K extends keyof T>(
event: K,
...params: Parameters<T[K]>
): ReturnType<T[K]>[];
emit(event: string, ...params: any[]): any[];
2023-11-14 22:20:30 +08:00
emit<K extends keyof T, R>(event: K, ...params: Parameters<T[K]>): R;
emit<R>(event: string, ...params: any[]): R;
emit(event: string, ...params: any[]): any[] {
this.emitted.add(event);
2023-06-04 23:06:56 +08:00
const events = (this.events[event] ??= []);
2023-11-14 22:20:30 +08:00
if (!!this.emitter[event]) {
const returns = this.emitter[event]!(events, ...params);
this.events[event] = events.filter(v => !v.once);
return returns;
} else {
const returns: ReturnType<Callable>[] = [];
2023-11-14 22:20:30 +08:00
for (let i = 0; i < events.length; i++) {
const e = events[i];
returns.push(e.fn(...(params as any)));
2023-06-04 23:06:56 +08:00
}
2023-11-14 22:20:30 +08:00
this.events[event] = events.filter(v => !v.once);
return returns;
2023-06-04 23:06:56 +08:00
}
}
2023-11-14 22:20:30 +08:00
/**
* (emitter)
* @param event
* @param fn
*/
// @ts-ignore
setEmitter<K extends keyof T>(event: K, fn?: EmitFn<T[K]>): void;
setEmitter(event: string, fn?: EmitFn<Callable>): void;
setEmitter(event: string, fn?: EmitFn<Callable>): void {
2023-11-14 22:20:30 +08:00
this.emitter[event] = fn;
}
/**
*
*/
removeAllListeners(): void;
/**
*
* @param event
*/
removeAllListeners(event: keyof T): void;
/**
*
* @param event
*/
removeAllListeners(event: string): void;
removeAllListeners(event?: string | keyof T) {
if (has(event)) this.events[event] = [];
else this.events = {};
}
}
type IndexedSymbol = number | string | symbol;
export class IndexedEventEmitter<
T extends Record<keyof T, Callable>
> extends EventEmitter<T> {
private fnMap: {
[P in keyof T]?: Map<IndexedSymbol, T[P]>;
} = {};
/**
*
* @param event
* @param symbol
* @param fn
* @param options
*/
onIndex<K extends keyof T>(
event: K,
symbol: IndexedSymbol,
fn: T[K],
options: Partial<ListenerOptions>
) {
const map = this.ensureMap(event);
if (map.has(symbol)) {
console.warn(
`监听${String(event)}出错:已存在标识符为${String(
symbol
)}`
);
this.offIndex(event, symbol);
}
map.set(symbol, fn);
this.on(event, fn, options);
}
/**
*
* @param event
* @param symbol
* @param fn
*/
onceIndex<K extends keyof T>(event: K, symbol: IndexedSymbol, fn: T[K]) {
this.onIndex(event, symbol, fn, { once: true });
}
/**
*
* @param event
* @param symbol
*/
offIndex<K extends keyof T>(event: K, symbol: IndexedSymbol) {
const map = this.ensureMap(event);
const fn = map.get(symbol);
if (!fn) return;
this.off(event, fn);
}
private ensureMap<K extends keyof T>(event: K) {
return this.fnMap[event] ?? new Map<IndexedSymbol, T[K]>();
}
2023-06-04 23:06:56 +08:00
}