This commit is contained in:
Putoo
2026-04-26 18:53:56 +08:00
parent 2c6c62e88c
commit 4463f9e810
20 changed files with 461 additions and 1035 deletions

View File

@@ -0,0 +1,34 @@
import type { EventBusHandler } from '~/extends/EventBusExtend'
/**
* 事件总线组合式封装
* 在组件作用域内订阅时会自动解绑
*/
export const useEventBus = () => {
const registerDispose = (unsubscribe: () => void) => {
if (getCurrentScope()) {
onScopeDispose(unsubscribe)
}
return unsubscribe
}
const on = <T = unknown>(event: string, handler: EventBusHandler<T>) => {
return registerDispose(EventBusExtend.on(event, handler))
}
const off = <T = unknown>(event: string, handler?: EventBusHandler<T>) => {
EventBusExtend.off(event, handler)
}
const emit = <T = unknown>(event: string, payload?: T) => {
EventBusExtend.emit(event, payload)
}
return {
on,
off,
emit,
clear: EventBusExtend.clear
}
}