Files
chad/server/plugins/event-bus.ts

31 lines
820 B
TypeScript

import type { FastifyPluginAsync } from 'fastify'
import type { Type } from 'typebox'
import type { Channel } from '../prisma/generated-client/client.ts'
import type { UserSchema } from './schemas/auth.ts'
import type { ChatMessageSchema } from './schemas/chat.ts'
import { EventEmitter } from 'node:events'
import fp from 'fastify-plugin'
declare module 'fastify' {
interface FastifyInstance {
bus: EventEmitter
}
}
interface EventMap {
'chat:new-message': [Type.Static<typeof ChatMessageSchema>]
'user:profile-updated': [Type.Static<typeof UserSchema>]
'channel:created': [Channel]
'channel:removed': [Channel]
}
const plugin: FastifyPluginAsync = fp(async (fastify) => {
const bus = new EventEmitter<EventMap>()
fastify.decorate('bus', bus)
}, {
name: 'event-bus',
})
export default plugin