From cd69bb4fb44f5fd332523aa849c723fc06504dc3 Mon Sep 17 00:00:00 2001 From: Il Harper Date: Tue, 5 Mar 2024 21:42:35 +0800 Subject: [PATCH] feat(api): implement emitter --- packages/engine-chronocat-api/src/emitter.ts | 24 +++++++++++++++++++ .../engine-chronocat-api/src/globalVars.ts | 3 +++ 2 files changed, 27 insertions(+) create mode 100644 packages/engine-chronocat-api/src/emitter.ts diff --git a/packages/engine-chronocat-api/src/emitter.ts b/packages/engine-chronocat-api/src/emitter.ts new file mode 100644 index 0000000..3358da9 --- /dev/null +++ b/packages/engine-chronocat-api/src/emitter.ts @@ -0,0 +1,24 @@ +/* eslint-disable @typescript-eslint/no-unsafe-declaration-merging */ + +import { EventEmitter } from 'node:events' + +type EmittedEvents = Record unknown> + +export interface TypedEventEmitter { + on(event: E, listener: ES[E]): this + once(event: E, listener: ES[E]): this + emit(event: E, ...args: Parameters): boolean +} + +// eslint-disable-next-line @typescript-eslint/no-unused-vars +export class TypedEventEmitter extends EventEmitter {} + +export type ChronoEvents = { + buddyListChange: () => void + groupListUpdate: () => void +} + +export class ChronoEventEmitter extends TypedEventEmitter { + emitBuddyListChange = () => this.emit('buddyListChange') + emitGroupListUpdate = () => this.emit('groupListUpdate') +} diff --git a/packages/engine-chronocat-api/src/globalVars.ts b/packages/engine-chronocat-api/src/globalVars.ts index 8de4865..fc1d62d 100644 --- a/packages/engine-chronocat-api/src/globalVars.ts +++ b/packages/engine-chronocat-api/src/globalVars.ts @@ -1,4 +1,5 @@ import type { Group, Profile, RedIpcData, RedIpcEvent } from '@chronocat/red' +import { ChronoEventEmitter } from './emitter' export const requestMethodMap: Record = {} export const requestCallbackMap: Record< @@ -10,3 +11,5 @@ export const groupMap: Record = {} export const roleMap: Record> = {} export const friendMap: Record = {} export const richMediaDownloadMap: Record void> = {} + +export const chronoEventEmitter = new ChronoEventEmitter()