26 包 monorepo(TS 全源码): - snowcat-core: NT 内核 API 封装 + listener(含 9.9.37 六回调适配)+ Packet 发包 - snowcat-onebot: OneBot v11 全动作 + MCP Streamable HTTP 适配器 + 稳定线 - snowcat-webui-frontend/backend: 雪猫控制台(React+HeroUI) - snowcat-shell: 单文件 bundle 出口(vite inlineDynamicImports) - snowcat-native: napi2native / MoeHoo / ffmpeg / pty 运行时 - 56 项 SnowLuma 独门动作移植(qzone 全家桶/群相册/装扮/闪照/收藏) - 工具件: 解包器/启动器/便携制作器/doctor GitHub 关联已全量指向 DandanLLab/SnowCatQQ
113 lines
3.7 KiB
TypeScript
113 lines
3.7 KiB
TypeScript
import { SnowCatProtocolEvent } from '@/snowcat-protocol/event/SnowCatProtocolEvent';
|
|
import { SnowCatProtocolMessage } from '@/snowcat-protocol/types';
|
|
import { NetworkAdapterConfig } from '@/snowcat-protocol/config/config';
|
|
import { INapCatProtocolNetworkAdapter } from '@/snowcat-protocol/network/adapter';
|
|
|
|
export type SnowCatProtocolEmitEventContent = SnowCatProtocolEvent | SnowCatProtocolMessage;
|
|
|
|
export enum SnowCatProtocolNetworkReloadType {
|
|
Normal = 0,
|
|
ConfigChange = 1,
|
|
NetWorkReload = 2,
|
|
NetWorkClose = 3,
|
|
NetWorkOpen = 4,
|
|
}
|
|
|
|
export class SnowCatProtocolNetworkManager {
|
|
adapters: Map<string, INapCatProtocolNetworkAdapter<NetworkAdapterConfig>> = new Map();
|
|
|
|
async openAllAdapters () {
|
|
return Promise.all(Array.from(this.adapters.values()).map(adapter => adapter.open()));
|
|
}
|
|
|
|
async emitEvent (event: SnowCatProtocolEmitEventContent) {
|
|
return Promise.all(Array.from(this.adapters.values()).map(async adapter => {
|
|
if (adapter.isActive) {
|
|
return await adapter.onEvent(event);
|
|
}
|
|
}));
|
|
}
|
|
|
|
async emitEvents (events: SnowCatProtocolEmitEventContent[]) {
|
|
return Promise.all(events.map(event => this.emitEvent(event)));
|
|
}
|
|
|
|
async emitEventByName (names: string[], event: SnowCatProtocolEmitEventContent) {
|
|
return Promise.all(names.map(async name => {
|
|
const adapter = this.adapters.get(name);
|
|
if (adapter && adapter.isActive) {
|
|
return await adapter.onEvent(event);
|
|
}
|
|
}));
|
|
}
|
|
|
|
async emitEventByNames (map: Map<string, SnowCatProtocolEmitEventContent>) {
|
|
return Promise.all(Array.from(map.entries()).map(async ([name, event]) => {
|
|
const adapter = this.adapters.get(name);
|
|
if (adapter && adapter.isActive) {
|
|
return await adapter.onEvent(event);
|
|
}
|
|
}));
|
|
}
|
|
|
|
registerAdapter<CT extends NetworkAdapterConfig> (adapter: INapCatProtocolNetworkAdapter<CT>) {
|
|
this.adapters.set(adapter.name, adapter);
|
|
}
|
|
|
|
async registerAdapterAndOpen<CT extends NetworkAdapterConfig> (adapter: INapCatProtocolNetworkAdapter<CT>) {
|
|
this.registerAdapter(adapter);
|
|
await adapter.open();
|
|
}
|
|
|
|
async closeSomeAdapters<CT extends NetworkAdapterConfig> (adaptersToClose: INapCatProtocolNetworkAdapter<CT>[]) {
|
|
for (const adapter of adaptersToClose) {
|
|
this.adapters.delete(adapter.name);
|
|
await adapter.close();
|
|
}
|
|
}
|
|
|
|
async closeSomeAdapterWhenOpen<CT extends NetworkAdapterConfig> (adaptersToClose: INapCatProtocolNetworkAdapter<CT>[]) {
|
|
for (const adapter of adaptersToClose) {
|
|
this.adapters.delete(adapter.name);
|
|
if (adapter.isEnable) {
|
|
await adapter.close();
|
|
}
|
|
}
|
|
}
|
|
|
|
findSomeAdapter (name: string) {
|
|
return this.adapters.get(name);
|
|
}
|
|
|
|
async closeAdapterByPredicate (closeFilter: (adapter: INapCatProtocolNetworkAdapter<NetworkAdapterConfig>) => boolean) {
|
|
const adaptersToClose = Array.from(this.adapters.values()).filter(closeFilter);
|
|
await this.closeSomeAdapters(adaptersToClose);
|
|
}
|
|
|
|
async closeAllAdapters () {
|
|
await Promise.all(Array.from(this.adapters.values()).map(adapter => adapter.close()));
|
|
this.adapters.clear();
|
|
}
|
|
|
|
async reloadAdapter<T> (name: string, config: T) {
|
|
const adapter = this.adapters.get(name);
|
|
if (adapter) {
|
|
await adapter.reload(config);
|
|
}
|
|
}
|
|
|
|
async reloadSomeAdapters<T> (configMap: Map<string, T>) {
|
|
await Promise.all(Array.from(configMap.entries()).map(([name, config]) => this.reloadAdapter(name, config)));
|
|
}
|
|
|
|
hasActiveAdapters (): boolean {
|
|
return Array.from(this.adapters.values()).some(adapter => adapter.isActive);
|
|
}
|
|
|
|
async getAllConfig () {
|
|
return Array.from(this.adapters.values()).map(adapter => adapter.config);
|
|
}
|
|
}
|
|
|
|
export * from './adapter';
|