feat(shell): add emitter svc

This commit is contained in:
Il Harper 2024-03-05 00:27:48 +08:00
parent 0cde7ff772
commit 6d314503d1
No known key found for this signature in database
GPG Key ID: 4B71FCA698E7E8EC
2 changed files with 14 additions and 5 deletions

View File

@ -6,17 +6,16 @@ import { api } from './services/api'
import { getAuthData } from './services/authData'
import { baseDir } from './services/baseDir'
import { getConfig } from './services/config'
import { emitter } from './services/emitter'
import { l } from './services/logger'
import { validate } from './services/validate'
import type { ChronocatContext, DispatchMessage, Engine } from './types'
import type { ChronocatContext, Engine } from './types'
declare const __DEFINE_CHRONO_VERSION__: string
export const chronocat = async () => {
l.info(`Chronocat v${__DEFINE_CHRONO_VERSION__}`)
let emitImpl = (_: DispatchMessage) => {}
let ready: () => void
const readyPromise = new Promise<void>((res) => {
ready = res
@ -26,7 +25,7 @@ export const chronocat = async () => {
chronocat: {
api,
baseDir,
emit: (message: DispatchMessage) => emitImpl(message),
emit: emitter.emit,
getAuthData,
getConfig,
l,
@ -96,7 +95,7 @@ export const chronocat = async () => {
return
}
emitImpl = (await initServers()).emit
emitter.register((await initServers()).emit)
l.info('中身はあんまりないよ~ (v0.x)', { code: 560 })

View File

@ -0,0 +1,10 @@
import type { DispatchMessage } from '../types'
export type Emit = (message: DispatchMessage) => unknown
const handlers: Emit[] = []
export const emitter = {
register: (e: Emit) => handlers.push(e),
emit: (message: DispatchMessage) => handlers.forEach((e) => e(message)),
}