feat(companion): return cancelPendingEventWaits count

This commit is contained in:
William Valentin
2026-02-16 22:19:50 -08:00
parent a4e9828592
commit 83236a4ba3
6 changed files with 32 additions and 13 deletions
+3 -2
View File
@@ -77,7 +77,7 @@ function createRuntimeMock(): {
const subscribeEvents = vi.fn(() => () => undefined);
const subscribeEvent = vi.fn(() => () => undefined);
const clearEventSubscriptions = vi.fn(() => undefined);
const cancelPendingEventWaits = vi.fn(() => undefined);
const cancelPendingEventWaits = vi.fn(() => 1);
const listKnownEventNames = vi.fn(() => ['agent.stream', 'agent.typing', 'context_warning']);
const waitForEvent = vi.fn(async () => ({ token: 'evented' }));
const waitForIdle = vi.fn(async () => undefined);
@@ -321,9 +321,10 @@ describe('platform companion clients', () => {
const mock = createRuntimeMock();
const client = new IOSCompanionClient({ runtime: mock.runtime, nodeId: 'ios-node' });
client.cancelPendingEventWaits('manual');
const cancelled = client.cancelPendingEventWaits('manual');
expect(mock.cancelPendingEventWaits).toHaveBeenCalledWith('manual');
expect(cancelled).toBe(1);
});
it('platform listKnownEventNames forwards to runtime client', async () => {
+6 -6
View File
@@ -269,8 +269,8 @@ export class MacOSCompanionClient {
this.runtime.clearEventSubscriptions();
}
cancelPendingEventWaits(reason?: string): void {
this.runtime.cancelPendingEventWaits(reason);
cancelPendingEventWaits(reason?: string): number {
return this.runtime.cancelPendingEventWaits(reason);
}
listKnownEventNames(): CompanionEventName[] {
@@ -539,8 +539,8 @@ export class IOSCompanionClient {
this.runtime.clearEventSubscriptions();
}
cancelPendingEventWaits(reason?: string): void {
this.runtime.cancelPendingEventWaits(reason);
cancelPendingEventWaits(reason?: string): number {
return this.runtime.cancelPendingEventWaits(reason);
}
listKnownEventNames(): CompanionEventName[] {
@@ -807,8 +807,8 @@ export class AndroidCompanionClient {
this.runtime.clearEventSubscriptions();
}
cancelPendingEventWaits(reason?: string): void {
this.runtime.cancelPendingEventWaits(reason);
cancelPendingEventWaits(reason?: string): number {
return this.runtime.cancelPendingEventWaits(reason);
}
listKnownEventNames(): CompanionEventName[] {
+2 -1
View File
@@ -399,7 +399,7 @@ describe('CompanionRuntimeClient', () => {
const awaited = expect(
client.waitForEvent('agent.stream', { timeoutMs: 10_000 }),
).rejects.toThrow('manually cancelled');
client.cancelPendingEventWaits('manually cancelled');
expect(client.cancelPendingEventWaits('manually cancelled')).toBe(1);
await awaited;
(client as unknown as { handleMessage: (raw: string) => void }).handleMessage(
@@ -410,6 +410,7 @@ describe('CompanionRuntimeClient', () => {
}),
);
expect(handler).toHaveBeenCalledWith('agent.stream', { token: 'still-subscribed' });
expect(client.cancelPendingEventWaits()).toBe(0);
});
it('waitForEvent rejects immediately on disconnect', async () => {
+5 -3
View File
@@ -435,8 +435,8 @@ export class CompanionRuntimeClient {
this.rejectEventWaits(new Error('Event subscriptions cleared'));
}
cancelPendingEventWaits(reason = 'Event waits cancelled'): void {
this.rejectEventWaits(new Error(reason));
cancelPendingEventWaits(reason = 'Event waits cancelled'): number {
return this.rejectEventWaits(new Error(reason));
}
subscribeEvent<TData = unknown>(
@@ -925,11 +925,13 @@ export class CompanionRuntimeClient {
this.pending.clear();
}
private rejectEventWaits(error: Error): void {
private rejectEventWaits(error: Error): number {
const cancelled = this.pendingEventWaits.size;
for (const cancel of this.pendingEventWaits) {
cancel(error);
}
this.pendingEventWaits.clear();
return cancelled;
}
}