Add BridgeEventBus and network manager
Introduce a typed per-kind BridgeEventBus and migrate away from the legacy single-callback firehose. Bridge now exposes `events` and emits via the bus; BridgeManager/OneBotManager no longer wire a global event callback. Add a focused onebot event pipeline (event-pipeline.ts) that subscribes per-kind handlers on instance construction. Replace the in-memory MediaCache with a persistent MediaStore and update all lookups accordingly. Replace the old Http/Ws/HttpPost transport implementations with a unified OneBotNetworkManager and pluggable adapters (server/client/post), and update OneBotInstance to install and manage adapters from config. Remove several legacy files (http-transport, http-post-transport, ws-transport, media-cache) and update imports across the codebase. Also add tests for the bridge event bus, event pipeline, network manager, and rename media tests to reflect MediaStore usage. The bus dispatches handlers in parallel and reports handler errors via an onError hook to avoid one faulty listener blocking others.
This commit is contained in:
1 parent
1565749105
commit
144e0fdd59
27 files changed
+2357
-1234
No files matched your search
@@ -81,8 +81,7 @@ import {
|
||||
} from './web-actions';
|
||||
import type { GroupFilesResult } from './bridge-actions';
|
||||
import type { MediaIndexNode } from './bridge-actions';
|
||||
|
||||
export type BridgeEventCallback = (event: QQEventVariant) => void;
|
||||
import { BridgeEventBus } from './event-bus';
|
||||
|
||||
type CmdParser = (pkt: PacketInfo, qqInfo: QQInfo) => QQEventVariant[];
|
||||
|
||||
@@ -116,7 +115,12 @@ export class Bridge {
|
||||
|
||||
private qqInfo_: QQInfo;
|
||||
private pids_ = new Set<number>();
|
||||
private eventCallback_: BridgeEventCallback | null = null;
|
||||
/**
|
||||
* Per-kind event subscription. Replaces the legacy single-callback
|
||||
* firehose: downstream consumers now register exactly the kinds they
|
||||
* care about and `emitEvent` fans out in parallel.
|
||||
*/
|
||||
readonly events = new BridgeEventBus();
|
||||
private cmdHandlers_ = new Map<string, CmdParser[]>();
|
||||
private packetClient_: PacketSender | null = null;
|
||||
private memberRefreshTasks_ = new Map<number, Promise<void>>();
|
||||
@@ -155,10 +159,6 @@ export class Bridge {
|
||||
this.cmdHandlers_.set(cmd, arr);
|
||||
}
|
||||
|
||||
setEventCallback(cb: BridgeEventCallback): void {
|
||||
this.eventCallback_ = cb;
|
||||
}
|
||||
|
||||
handlesCmd(cmd: string): boolean {
|
||||
return this.cmdHandlers_.has(cmd);
|
||||
}
|
||||
@@ -199,11 +199,9 @@ export class Bridge {
|
||||
}
|
||||
|
||||
private emitEvent(event: QQEventVariant): void {
|
||||
if (this.eventCallback_) {
|
||||
try { this.eventCallback_(event); } catch (e) {
|
||||
log.error('event callback error: %s', e instanceof Error ? (e.stack ?? e.message) : String(e));
|
||||
}
|
||||
}
|
||||
// Fire-and-forget: errors inside subscribers are surfaced via the bus's
|
||||
// own onError hook so one bad listener never blocks the others.
|
||||
void this.events.emit(event);
|
||||
}
|
||||
|
||||
private triggerMemberCacheRefresh(event: QQEventVariant): void {
|
||||
|
||||
Reference in new issue
Block a user