46 lines
941 B
TypeScript
Raw Normal View History

2025-05-27 19:31:37 +08:00
import mitt from 'mitt';
type IEventbus = {
clear: () => void;
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));
};
/**
*
*/
const clear = () => {
emitter.all.clear();
};
/**
* @description: useEventbus
*/
export const useEventbus = (): IEventbus => {
return {
publish,
subscribe,
clear,
};
};