Skip to content

Commit

Permalink
fix: EventKey map types
Browse files Browse the repository at this point in the history
  • Loading branch information
YunYouJun committed Sep 25, 2024
1 parent 6ed5f82 commit 5e553d9
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 34 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"dev": "nodemon",
"test": "tsx test/index.ts",
"docs:dev": "pnpm -C docs run dev",
"docs:build": "pnpm -C docs run build"
"docs:build": "pnpm -C docs run build",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"isomorphic-ws": "^5.0.0",
Expand Down
12 changes: 0 additions & 12 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 13 additions & 3 deletions src/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,21 +492,31 @@ export interface NoticeHandler {

// =====================================================================================

export type AllHandlers = SocketHandler &
export type AllHandlers = |
SocketHandler &
ApiHandler &
MessageHandler &
MessageSentHandler &
MetaEventHandler &
RequestHandler &
NoticeHandler

export type WSReceiveHandler = MessageHandler &

export type WSReceiveHandler = |
MessageHandler &
MessageSentHandler &
MetaEventHandler &
RequestHandler &
NoticeHandler

export type EventHandle<T extends keyof AllHandlers> = (context: AllHandlers[T]) => any
export type EventKey = keyof AllHandlers
export type HandlerResMap = {
[K in EventKey]: AllHandlers[K]
}
export type EventHandleMap = {
[K in EventKey]: (context: HandlerResMap[K]) => void
}


// =====================================================================================

Expand Down
27 changes: 15 additions & 12 deletions src/NCEventBus.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {
EventHandleMap,
EventKey,
HandlerResMap,
type AllHandlers,
type EventHandle,
type MessageHandler,
type MessageSentHandler,
type MetaEventHandler,
Expand All @@ -10,22 +12,22 @@ import {
} from './Interfaces.js'
import { logger } from './Utils.js'

export class NCEventBus<const T extends keyof AllHandlers> {
#events: Map<T, EventHandle<T>[]> = new Map()
export class NCEventBus {
#events = new Map<EventKey, EventHandleMap[EventKey][]>()
debug: boolean

constructor(debug = false) {
this.debug = debug
}

on(event: T, handler: EventHandle<T>) {
on<T extends EventKey>(event: T, handler: EventHandleMap[T]) {
const handlers = this.#events.get(event) ?? []
handlers.push(handler)
this.#events.set(event, handlers)
return this
}

off(event: T, handler: EventHandle<T>) {
off<T extends EventKey>(event: T, handler: EventHandleMap[T]) {
const handlers = this.#events.get(event) ?? []
const index = handlers.indexOf(handler)
if (index >= 0) {
Expand All @@ -35,24 +37,25 @@ export class NCEventBus<const T extends keyof AllHandlers> {
return this
}

once(event: T, handler: EventHandle<T>) {
this.on(event, (context) => {
once<T extends EventKey>(event: T, handler: EventHandleMap[T]) {
const onceHandler = (context: HandlerResMap[T]) => {
handler(context)
this.off(event, handler)
})
this.off(event, onceHandler as EventHandleMap[T])
}
this.on(event, onceHandler as EventHandleMap[T])
return this
}

emit(type: T, context: AllHandlers[T]): this {
const handlers = this.#events.get(type) ?? []
emit<T extends EventKey>(type: T, context: HandlerResMap[T]): this {
const handlers = this.#events.get(type) as EventHandleMap[T][] ?? []

for (const handler of handlers) {
handler(context)
}

// 触发总类
const indexOf = type.lastIndexOf('.')
if (indexOf > 0) return this.emit(type.slice(0, indexOf) as T, context)
if (indexOf > 0) return this.emit(type.slice(0, indexOf) as EventKey, context)

return this
}
Expand Down
2 changes: 1 addition & 1 deletion src/NCWebsocketApi.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WSSendParam } from './Interfaces.js'
import { EventKey, WSSendParam } from './Interfaces.js'
import { NCWebsocketBase } from './NCWebsocketBase.js'

export class NCWebsocketApi extends NCWebsocketBase {
Expand Down
11 changes: 6 additions & 5 deletions src/NCWebsocketBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { nanoid } from 'nanoid'
import type {
AllHandlers,
APIRequest,
EventHandle,
EventHandleMap,
EventKey,
NCWebsocketOptions,
ResponseHandler,
WSReconnection,
Expand Down Expand Up @@ -254,8 +255,8 @@ export class NCWebsocketBase {
* @param event
* @param handle
*/
on<T extends keyof AllHandlers>(event: T, handle: EventHandle<T>) {
this.#eventBus.on(event, handle)
on<T extends EventKey>(event: T, handle: EventHandleMap[T]) {
this.#eventBus.on<T>(event, handle)
return this
}

Expand All @@ -264,7 +265,7 @@ export class NCWebsocketBase {
* @param event
* @param handle
*/
once<T extends keyof AllHandlers>(event: T, handle: EventHandle<T>) {
once<T extends EventKey>(event: T, handle: EventHandleMap[T]) {
this.#eventBus.once(event, handle)
return this
}
Expand All @@ -274,7 +275,7 @@ export class NCWebsocketBase {
* @param event
* @param handle
*/
off<T extends keyof AllHandlers>(event: T, handle: EventHandle<T>) {
off<T extends keyof AllHandlers>(event: T, handle: EventHandleMap[T]) {
this.#eventBus.off(event, handle)
return this
}
Expand Down

0 comments on commit 5e553d9

Please sign in to comment.