mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-23 08:45:50 +00:00
style: 完成所有文件的lint
This commit is contained in:
@@ -1,116 +1,116 @@
|
||||
import { log, error as logError } from "../logger";
|
||||
import { log, error as logError } from '../logger'
|
||||
|
||||
export interface SessionEvent {
|
||||
id: string;
|
||||
sessionId: string;
|
||||
type: string;
|
||||
payload: unknown;
|
||||
direction: "inbound" | "outbound";
|
||||
seqNum: number;
|
||||
createdAt: number;
|
||||
id: string
|
||||
sessionId: string
|
||||
type: string
|
||||
payload: unknown
|
||||
direction: 'inbound' | 'outbound'
|
||||
seqNum: number
|
||||
createdAt: number
|
||||
}
|
||||
|
||||
type Subscriber = (event: SessionEvent) => void;
|
||||
type Subscriber = (event: SessionEvent) => void
|
||||
|
||||
const MAX_EVENTS_PER_BUS = 5000;
|
||||
const MAX_EVENTS_PER_BUS = 5000
|
||||
|
||||
export class EventBus {
|
||||
private subscribers = new Set<Subscriber>();
|
||||
private events: SessionEvent[] = [];
|
||||
private seqNum = 0;
|
||||
private closed = false;
|
||||
private subscribers = new Set<Subscriber>()
|
||||
private events: SessionEvent[] = []
|
||||
private seqNum = 0
|
||||
private closed = false
|
||||
|
||||
subscribe(callback: Subscriber): () => void {
|
||||
this.subscribers.add(callback);
|
||||
return () => this.subscribers.delete(callback);
|
||||
this.subscribers.add(callback)
|
||||
return () => this.subscribers.delete(callback)
|
||||
}
|
||||
|
||||
subscriberCount(): number {
|
||||
return this.subscribers.size;
|
||||
return this.subscribers.size
|
||||
}
|
||||
|
||||
publish(event: Omit<SessionEvent, "seqNum" | "createdAt">): SessionEvent {
|
||||
if (this.closed) throw new Error("EventBus is closed");
|
||||
publish(event: Omit<SessionEvent, 'seqNum' | 'createdAt'>): SessionEvent {
|
||||
if (this.closed) throw new Error('EventBus is closed')
|
||||
const full: SessionEvent = {
|
||||
...event,
|
||||
seqNum: ++this.seqNum,
|
||||
createdAt: Date.now(),
|
||||
};
|
||||
this.events.push(full);
|
||||
}
|
||||
this.events.push(full)
|
||||
// Evict oldest events when exceeding limit
|
||||
if (this.events.length > MAX_EVENTS_PER_BUS) {
|
||||
this.events = this.events.slice(-Math.floor(MAX_EVENTS_PER_BUS / 2));
|
||||
this.events = this.events.slice(-Math.floor(MAX_EVENTS_PER_BUS / 2))
|
||||
}
|
||||
log(
|
||||
`[RC-DEBUG] bus publish: sessionId=${event.sessionId} type=${event.type} dir=${event.direction} seq=${full.seqNum} subscribers=${this.subscribers.size}`,
|
||||
event.type === "error" ? `payload=${JSON.stringify(event.payload)}` : "",
|
||||
);
|
||||
event.type === 'error' ? `payload=${JSON.stringify(event.payload)}` : '',
|
||||
)
|
||||
for (const cb of this.subscribers) {
|
||||
try {
|
||||
cb(full);
|
||||
cb(full)
|
||||
} catch (err) {
|
||||
logError(`[RC-DEBUG] bus subscriber error:`, err);
|
||||
logError(`[RC-DEBUG] bus subscriber error:`, err)
|
||||
}
|
||||
}
|
||||
return full;
|
||||
return full
|
||||
}
|
||||
|
||||
getLastSeqNum(): number {
|
||||
return this.seqNum;
|
||||
return this.seqNum
|
||||
}
|
||||
|
||||
getEventsSince(seqNum: number): SessionEvent[] {
|
||||
const idx = this.events.findIndex((e) => e.seqNum > seqNum);
|
||||
if (idx === -1) return [];
|
||||
return this.events.slice(idx);
|
||||
const idx = this.events.findIndex(e => e.seqNum > seqNum)
|
||||
if (idx === -1) return []
|
||||
return this.events.slice(idx)
|
||||
}
|
||||
|
||||
close() {
|
||||
this.closed = true;
|
||||
this.subscribers.clear();
|
||||
this.closed = true
|
||||
this.subscribers.clear()
|
||||
}
|
||||
}
|
||||
|
||||
/** Global registry of per-session event buses */
|
||||
const buses = new Map<string, EventBus>();
|
||||
const buses = new Map<string, EventBus>()
|
||||
|
||||
export function getEventBus(sessionId: string): EventBus {
|
||||
let bus = buses.get(sessionId);
|
||||
let bus = buses.get(sessionId)
|
||||
if (!bus) {
|
||||
bus = new EventBus();
|
||||
buses.set(sessionId, bus);
|
||||
bus = new EventBus()
|
||||
buses.set(sessionId, bus)
|
||||
}
|
||||
return bus;
|
||||
return bus
|
||||
}
|
||||
|
||||
export function removeEventBus(sessionId: string) {
|
||||
const bus = buses.get(sessionId);
|
||||
const bus = buses.get(sessionId)
|
||||
if (bus) {
|
||||
bus.close();
|
||||
buses.delete(sessionId);
|
||||
bus.close()
|
||||
buses.delete(sessionId)
|
||||
}
|
||||
}
|
||||
|
||||
export function getAllEventBuses(): Map<string, EventBus> {
|
||||
return buses;
|
||||
return buses
|
||||
}
|
||||
|
||||
/** Global registry of per-channel-group ACP event buses */
|
||||
const acpBuses = new Map<string, EventBus>();
|
||||
const acpBuses = new Map<string, EventBus>()
|
||||
|
||||
export function getAcpEventBus(channelGroupId: string): EventBus {
|
||||
let bus = acpBuses.get(channelGroupId);
|
||||
let bus = acpBuses.get(channelGroupId)
|
||||
if (!bus) {
|
||||
bus = new EventBus();
|
||||
acpBuses.set(channelGroupId, bus);
|
||||
bus = new EventBus()
|
||||
acpBuses.set(channelGroupId, bus)
|
||||
}
|
||||
return bus;
|
||||
return bus
|
||||
}
|
||||
|
||||
export function removeAcpEventBus(channelGroupId: string) {
|
||||
const bus = acpBuses.get(channelGroupId);
|
||||
const bus = acpBuses.get(channelGroupId)
|
||||
if (bus) {
|
||||
bus.close();
|
||||
acpBuses.delete(channelGroupId);
|
||||
bus.close()
|
||||
acpBuses.delete(channelGroupId)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user