mirror of
https://github.com/jeffvli/feishin.git
synced 2026-05-07 04:20:12 +02:00
add global event emitter
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import { ErrorHandler, EventCallback, TypedEventEmitter } from './types';
|
||||
|
||||
import { EventMap } from '/@/renderer/events/events';
|
||||
|
||||
class TypedEventEmitterImpl implements TypedEventEmitter<EventMap> {
|
||||
private errorHandler: ErrorHandler | null = null;
|
||||
private events: Map<string, EventCallback[]> = new Map();
|
||||
|
||||
emit<K extends keyof EventMap>(event: K, payload: EventMap[K]): void {
|
||||
const callbacks = this.events.get(String(event));
|
||||
if (callbacks) {
|
||||
callbacks.forEach((callback) => {
|
||||
try {
|
||||
callback(payload);
|
||||
} catch (error) {
|
||||
this.handleError(error as Error, String(event), payload);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
off<K extends keyof EventMap>(event: K, callback: EventCallback<EventMap[K]>): void {
|
||||
const callbacks = this.events.get(String(event));
|
||||
if (callbacks) {
|
||||
const index = callbacks.indexOf(callback);
|
||||
if (index > -1) {
|
||||
callbacks.splice(index, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
on<K extends keyof EventMap>(event: K, callback: EventCallback<EventMap[K]>): void {
|
||||
const eventKey = String(event);
|
||||
if (!this.events.has(eventKey)) {
|
||||
this.events.set(eventKey, []);
|
||||
}
|
||||
this.events.get(eventKey)!.push(callback);
|
||||
}
|
||||
|
||||
removeAllListeners<K extends keyof EventMap>(event?: K): void {
|
||||
if (event) {
|
||||
// Remove specific event listeners
|
||||
this.events.delete(String(event));
|
||||
} else {
|
||||
// Remove all listeners
|
||||
this.events.clear();
|
||||
}
|
||||
}
|
||||
|
||||
setErrorHandler(handler: ErrorHandler): void {
|
||||
this.errorHandler = handler;
|
||||
}
|
||||
|
||||
private handleError(error: Error, event: string, payload: any): void {
|
||||
if (this.errorHandler) {
|
||||
this.errorHandler(error, event, payload);
|
||||
} else {
|
||||
console.error(`Event emitter error for event "${event}":`, error, payload);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const eventEmitter = new TypedEventEmitterImpl();
|
||||
@@ -0,0 +1,30 @@
|
||||
import { LibraryItem } from '/@/shared/types/domain-types';
|
||||
|
||||
export type EventMap = {
|
||||
ITEM_LIST_REFRESH: ItemListRefreshEventPayload;
|
||||
ITEM_LIST_UPDATE_ITEM: ItemListUpdateItemEventPayload;
|
||||
USER_FAVORITE: UserFavoriteEventPayload;
|
||||
USER_RATING: UserRatingEventPayload;
|
||||
};
|
||||
|
||||
export type ItemListRefreshEventPayload = {
|
||||
key: string;
|
||||
};
|
||||
|
||||
export type ItemListUpdateItemEventPayload = {
|
||||
index: number;
|
||||
item: unknown;
|
||||
key: string;
|
||||
};
|
||||
|
||||
export type UserFavoriteEventPayload = {
|
||||
favorite: boolean;
|
||||
id: string[];
|
||||
itemType: LibraryItem;
|
||||
};
|
||||
|
||||
export type UserRatingEventPayload = {
|
||||
id: string[];
|
||||
itemType: LibraryItem;
|
||||
rating: null | number;
|
||||
};
|
||||
@@ -0,0 +1,15 @@
|
||||
export type ErrorHandler = (error: Error, event: string, payload: any) => void;
|
||||
|
||||
export type EventCallback<T = any> = (payload: T) => void;
|
||||
|
||||
export interface TypedEventEmitter<T extends Record<string, any>> {
|
||||
emit<K extends keyof T>(event: K, payload: T[K]): void;
|
||||
|
||||
off<K extends keyof T>(event: K, callback: EventCallback<T[K]>): void;
|
||||
|
||||
on<K extends keyof T>(event: K, callback: EventCallback<T[K]>): void;
|
||||
|
||||
removeAllListeners<K extends keyof T>(event?: K): void;
|
||||
|
||||
setErrorHandler(handler: ErrorHandler): void;
|
||||
}
|
||||
Reference in New Issue
Block a user