You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
746 B
30 lines
746 B
import type { EventType, Handler, WildcardHandler } from "./mitt";
|
|
|
|
import mitt from "./mitt";
|
|
|
|
const emitter = mitt();
|
|
|
|
interface EventBus {
|
|
/** 发布事件 */
|
|
publish(type: "*", event?: any): void;
|
|
/** 发布事件 */
|
|
publish<T = any>(type: EventType, event?: T): void;
|
|
|
|
/** 订阅事件 */
|
|
subscribe(type: "*", handler: WildcardHandler): void;
|
|
/** 订阅事件 */
|
|
subscribe<T = any>(type: EventType, handler: Handler<T>): void;
|
|
|
|
/** 退订事件 */
|
|
unSubscribe(type: "*", handler: WildcardHandler): void;
|
|
/** 退订事件 */
|
|
unSubscribe<T = any>(type: EventType, handler: Handler<T>): void;
|
|
}
|
|
|
|
export function useEventBus(): EventBus {
|
|
return {
|
|
publish: emitter.emit,
|
|
subscribe: emitter.on,
|
|
unSubscribe: emitter.off,
|
|
};
|
|
}
|
|
|