feat: 安装 mitt 并创建 sseBus 类型化事件总线

This commit is contained in:
2026-06-22 13:46:03 +08:00
parent 3354623dd9
commit aa6f0a002e
3 changed files with 44 additions and 0 deletions

View File

@@ -15,6 +15,7 @@
"axios": "^1.17.0",
"cropperjs": "^2.1.1",
"element-plus": "^2.14.2",
"mitt": "^3.0.1",
"pinia": "^3.0.4",
"pinia-plugin-persistedstate": "^4.7.1",
"qrcode": "^1.5.4",

3
pnpm-lock.yaml generated
View File

@@ -17,6 +17,9 @@ importers:
element-plus:
specifier: ^2.14.2
version: 2.14.2(vue@3.5.35(typescript@6.0.3))
mitt:
specifier: ^3.0.1
version: 3.0.1
pinia:
specifier: ^3.0.4
version: 3.0.4(typescript@6.0.3)(vue@3.5.35(typescript@6.0.3))

40
src/stores/sseBus.ts Normal file
View File

@@ -0,0 +1,40 @@
// webgame-frontend/src/stores/sseBus.ts
import mitt from 'mitt'
import type { RoomItem } from '@/api/modules/game'
/**
* SSE 推送事件类型映射
*
* 每个事件类型对应一个数据类型,类型安全。
* connected / error 由 useSse 连接管理发出,其余由服务端 SSE 事件映射而来。
*/
type SseEvents = {
/** SSE 连接成功 */
connected: void
/** SSE 连接异常 */
error: { message: string; retryCount: number }
/** 新房间创建 */
room_created: RoomItem
/** 房间状态更新 */
room_updated: RoomItem
/** 房间移除 */
room_removed: { roomId: string }
/** 被踢出房间 */
kicked: { roomId: string }
/** 所在房间游戏开始 */
game_started: { roomId: string }
/** 余额变动 */
balance_chg: { balance: number; delta: number }
}
/**
* 全局 SSE 事件总线(发布-订阅模式)
*
* 示例:
* ```ts
* import { sseBus } from '@/stores/sseBus'
* const off = sseBus.on('room_updated', (room) => { ... })
* onUnmounted(off)
* ```
*/
export const sseBus = mitt<SseEvents>()