feat(companion): add clearEventSubscriptions helper

This commit is contained in:
William Valentin
2026-02-16 18:49:20 -08:00
parent f67362bf3b
commit fd59d88c0c
4 changed files with 40 additions and 1 deletions
+1 -1
View File
@@ -1190,7 +1190,7 @@ Methods:
- `system.capabilities` returns gateway protocol and node policy snapshot.
Companion runtime helper:
- `src/companion/runtimeClient.ts` provides a typed Node/WebSocket client for companion runtimes (macOS/iOS/Android workers) with wrappers for `node.register`, `node.capabilities.get`, `node.location.set/get`, `node.status.set`, `node.push_token.set`, `system.capabilities`, `system.nodes`, and canvas artifact RPCs (`canvas.put/get/list/delete/clear`), plus convenience helpers (`bootstrapNode`, optional `autoConnect`) and event helpers (`subscribeEvents()`, `subscribeEvent()`, `waitForEvent()`).
- `src/companion/runtimeClient.ts` provides a typed Node/WebSocket client for companion runtimes (macOS/iOS/Android workers) with wrappers for `node.register`, `node.capabilities.get`, `node.location.set/get`, `node.status.set`, `node.push_token.set`, `system.capabilities`, `system.nodes`, and canvas artifact RPCs (`canvas.put/get/list/delete/clear`), plus convenience helpers (`bootstrapNode`, optional `autoConnect`) and event helpers (`subscribeEvents()`, `subscribeEvent()`, `waitForEvent()`, `clearEventSubscriptions()`).
- `src/companion/platformClients.ts` provides platform-focused wrappers:
- `MacOSCompanionClient` (`platform: "macos"`, APNs push registration)
- `IOSCompanionClient` (`platform: "ios"`, APNs push registration)
+13
View File
@@ -395,6 +395,19 @@
],
"test_status": "pnpm test:run src/companion/platformClients.test.ts src/companion/heartbeatLoop.test.ts src/companion/runtimeClient.test.ts src/companion/platformClients.integration.test.ts + pnpm typecheck passing"
},
"companion-runtime-clear-event-subscriptions": {
"status": "completed",
"date": "2026-02-17",
"updated": "2026-02-17",
"summary": "Added `clearEventSubscriptions()` to `CompanionRuntimeClient` for explicit teardown of all registered event handlers, with test coverage for generic + filtered subscriptions.",
"files_modified": [
"src/companion/runtimeClient.ts",
"src/companion/runtimeClient.test.ts",
"README.md",
"docs/plans/state.json"
],
"test_status": "pnpm test:run src/companion/runtimeClient.test.ts src/companion/platformClients.test.ts src/companion/platformClients.integration.test.ts src/companion/heartbeatLoop.test.ts + pnpm typecheck passing"
},
"browser-tools-activation-clarity": {
"status": "completed",
"date": "2026-02-17",
+22
View File
@@ -196,6 +196,28 @@ describe('CompanionRuntimeClient', () => {
expect(streamHandler).toHaveBeenCalledTimes(1);
});
it('clears all event subscriptions', () => {
const client = new CompanionRuntimeClient({
url: 'ws://127.0.0.1:1',
});
const handlerA = vi.fn();
const handlerB = vi.fn();
client.subscribeEvents(handlerA);
client.subscribeEvent('agent.stream', handlerB);
client.clearEventSubscriptions();
(client as unknown as { handleMessage: (raw: string) => void }).handleMessage(
JSON.stringify({
id: 50,
event: 'agent.stream',
data: { token: 'cleared' },
}),
);
expect(handlerA).not.toHaveBeenCalled();
expect(handlerB).not.toHaveBeenCalled();
});
it('waitForEvent resolves using optional predicate filter', async () => {
const client = new CompanionRuntimeClient({
url: 'ws://127.0.0.1:1',
+4
View File
@@ -364,6 +364,10 @@ export class CompanionRuntimeClient {
};
}
clearEventSubscriptions(): void {
this.eventHandlers.clear();
}
subscribeEvent<TData = unknown>(
eventName: string,
handler: CompanionTypedEventHandler<TData>,