feat(api): implement event handlers

This commit is contained in:
Il Harper 2024-03-05 21:43:16 +08:00
parent cd69bb4fb4
commit e0b7ab6d3c
No known key found for this signature in database
GPG Key ID: 4B71FCA698E7E8EC
2 changed files with 67 additions and 2 deletions

View File

@ -1,4 +1,10 @@
import type { Group, Profile, RedIpcData, RedIpcEvent } from '@chronocat/red'
import type {
Group,
Profile,
RedIpcData,
RedIpcEvent,
RedMessage,
} from '@chronocat/red'
import { ChronoEventEmitter } from './emitter'
export const requestMethodMap: Record<string, string> = {}
@ -12,4 +18,7 @@ export const roleMap: Record<string, Record<string, number>> = {}
export const friendMap: Record<string, Profile> = {}
export const richMediaDownloadMap: Record<string, (path: string) => void> = {}
export const sendQueue: ((msg: RedMessage) => void)[] = []
export const sendCallbackMap: Record<string, (msg: RedMessage) => void> = {}
export const chronoEventEmitter = new ChronoEventEmitter()

View File

@ -1,4 +1,8 @@
import type {
OnAddSendMsg,
OnBuddyListChange,
OnGroupListUpdate,
OnMsgInfoListUpdate,
OnRichMediaDownloadComplete,
RedIpcArgs,
RedIpcDataEvent,
@ -8,7 +12,15 @@ import type { ChronocatContext } from '@chronocat/shell'
import type { IpcManData } from 'ipcman'
import { ipcMan } from 'ipcman'
import { buildAssetsGet } from './api/internal/assets/get'
import { requestMethodMap, richMediaDownloadMap } from './globalVars'
import {
chronoEventEmitter,
friendMap,
groupMap,
requestMethodMap,
richMediaDownloadMap,
sendCallbackMap,
sendQueue,
} from './globalVars'
declare const __DEFINE_CHRONO_VERSION__: string
@ -27,6 +39,50 @@ export const apply = async (ctx: ChronocatContext) => {
richMediaDownloadMap[downloadId]!(notifyInfo.filePath)
delete richMediaDownloadMap[downloadId]
}
return
}
case 'onGroupListUpdate':
case 'nodeIKernelGroupListener/onGroupListUpdate': {
const { groupList } = payload as OnGroupListUpdate
for (const group of groupList) groupMap[group.groupCode] = group
chronoEventEmitter.emitGroupListUpdate()
return
}
case 'onBuddyListChange':
case 'nodeIKernelBuddyListener/onBuddyListChange': {
const { data } = payload as OnBuddyListChange
for (const category of data) {
for (const buddy of category.buddyList) {
// buddy.category = category.categoryName
friendMap[buddy.uin] = buddy
}
}
return
}
case 'nodeIKernelMsgListener/onAddSendMsg': {
const { msgRecord } = payload as OnAddSendMsg
sendCallbackMap[msgRecord.msgId] = sendQueue.shift()!
return
}
case 'nodeIKernelMsgListener/onMsgInfoListUpdate': {
const { msgList } = payload as OnMsgInfoListUpdate
for (const msg of msgList) {
if (msg.sendStatus > 1) {
// sendCallbackMap[msg.msgId]?.(await uixCache.preprocessObject(msg))
delete sendCallbackMap[msg.msgId]
}
}
return
}
}