Skip to content

Commit

Permalink
[*] 尝试移除依赖 events mitt
Browse files Browse the repository at this point in the history
  • Loading branch information
huankong233 committed Sep 25, 2024
1 parent 0c58ce8 commit 6ed5f82
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@
"docs:build": "pnpm -C docs run build"
},
"dependencies": {
"events": "^3.3.0",
"isomorphic-ws": "^5.0.0",
"mitt": "^3.0.1",
"nanoid": "^5.0.7",
"ws": "^8.18.0"
},
Expand Down
41 changes: 23 additions & 18 deletions src/NCEventBus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,47 @@ import {
type RequestHandler,
type WSReceiveHandler
} from './Interfaces.js'

import mitt from 'mitt'
import { logger } from './Utils.js'

export class NCEventBus {
export class NCEventBus<const T extends keyof AllHandlers> {
#events: Map<T, EventHandle<T>[]> = new Map()
debug: boolean
// @ts-ignore
#mitt = mitt()

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

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

once<T extends keyof AllHandlers>(event: T, handle: EventHandle<T>) {
const fn = (args: AllHandlers[T]) => {
this.#mitt.off(event, fn)
handle(args)
off(event: T, handler: EventHandle<T>) {
const handlers = this.#events.get(event) ?? []
const index = handlers.indexOf(handler)
if (index >= 0) {
handlers.splice(index, 1)
this.#events.set(event, handlers)
}
this.#mitt.on(event, fn)
return this
}

off<T extends keyof AllHandlers>(event: T, handle: EventHandle<T>) {
this.#mitt.off(event, handle)
once(event: T, handler: EventHandle<T>) {
this.on(event, (context) => {
handler(context)
this.off(event, handler)
})
return this
}

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

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

// 触发总类
const indexOf = type.lastIndexOf('.')
Expand All @@ -50,8 +57,6 @@ export class NCEventBus {
return this
}

post_types = ['message', 'notice', 'request', 'meta_event', 'message_sent']

parseMessage(json: WSReceiveHandler[keyof WSReceiveHandler]) {
const post_type = json['post_type']

Expand Down

0 comments on commit 6ed5f82

Please sign in to comment.