feat(companion): add runtime dispose lifecycle helper

This commit is contained in:
William Valentin
2026-02-16 19:06:21 -08:00
parent d63704d436
commit fee32e8abe
4 changed files with 39 additions and 1 deletions
+20
View File
@@ -218,6 +218,26 @@ describe('CompanionRuntimeClient', () => {
expect(handlerB).not.toHaveBeenCalled();
});
it('dispose clears subscriptions and is safe to call repeatedly', () => {
const client = new CompanionRuntimeClient({
url: 'ws://127.0.0.1:1',
});
const handler = vi.fn();
client.subscribeEvents(handler);
client.dispose();
client.dispose();
(client as unknown as { handleMessage: (raw: string) => void }).handleMessage(
JSON.stringify({
id: 51,
event: 'agent.stream',
data: { token: 'after-dispose' },
}),
);
expect(handler).not.toHaveBeenCalled();
});
it('waitForEvent resolves using optional predicate filter', async () => {
const client = new CompanionRuntimeClient({
url: 'ws://127.0.0.1:1',
+5
View File
@@ -357,6 +357,11 @@ export class CompanionRuntimeClient {
ws.close(code, reason);
}
dispose(code?: number, reason?: string): void {
this.disconnect(code, reason);
this.clearEventSubscriptions();
}
subscribeEvents(handler: CompanionEventHandler): () => void {
this.eventHandlers.add(handler);
return () => {