46 lines
941 B
TypeScript
Raw Normal View History

2025-03-11 10:29:43 +08:00
import mitt from 'mitt';
type IEventbus = {
2025-05-27 13:54:28 +08:00
clear: () => void;
2025-03-11 10:29:43 +08:00
publish: (eventName: string, content: any) => void;
subscribe: (eventName: string, callback: (content: any) => void) => void;
};
const emitter = mitt();
/**
* @description:
* @param {*} eventName
* @param {*} content
*/
const publish = (eventName: string, content: any) => {
emitter.emit(eventName, content);
};
/**
* @description:
* @param {*} eventName
* @param {*} callback
*/
const subscribe = (eventName: string, callback: (content: any) => void) => {
emitter.on(eventName, (content) => callback(content));
};
2025-05-27 13:54:28 +08:00
/**
*
*/
const clear = () => {
emitter.all.clear();
};
2025-03-11 10:29:43 +08:00
/**
* @description: useEventbus
*/
export const useEventbus = (): IEventbus => {
return {
publish,
subscribe,
2025-05-27 13:54:28 +08:00
clear,
2025-03-11 10:29:43 +08:00
};
};