Files
webgame-frontend/src/utils/eventBus.ts
T

36 lines
747 B
TypeScript
Raw Normal View History

2026-06-22 14:36:20 +08:00
import mitt from 'mitt'
/** 应用级事件类型定义 */
type AppEventsMap = {
/** 401 未授权 */
'app:unauthorized': void
/** 请求错误(业务错误 / HTTP 错误 / 网络错误) */
'app:request:error': {
type: 'business' | 'http' | 'network'
code?: number
message: string
}
}
2026-06-09 10:12:09 +08:00
/** 应用级事件常量 */
export const AppEvents = {
UNAUTHORIZED: 'app:unauthorized',
REQUEST_ERROR: 'app:request:error'
} as const
/**
2026-06-22 14:36:20 +08:00
* 应用级事件总线(基于 mitt)
2026-06-09 10:12:09 +08:00
*
* @example
* ```ts
* // 注册事件
* eventBus.on(AppEvents.UNAUTHORIZED, () => {
* router.push('/auth/login')
* })
*
* // 发布事件
* eventBus.emit(AppEvents.UNAUTHORIZED)
* ```
*/
2026-06-22 14:36:20 +08:00
export const eventBus = mitt<AppEventsMap>()