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
+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()` with timeout/predicate/abort support, `clearEventSubscriptions()`).
- `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`, `dispose()`) and event helpers (`subscribeEvents()`, `subscribeEvent()`, `waitForEvent()` with timeout/predicate/abort support, `clearEventSubscriptions()`).
- `src/companion/platformClients.ts` provides platform-focused wrappers:
- `MacOSCompanionClient` (`platform: "macos"`, APNs push registration)
- `IOSCompanionClient` (`platform: "ios"`, APNs push registration)
+13
View File
@@ -447,6 +447,19 @@
],
"test_status": "pnpm test:run src/companion/heartbeatLoop.test.ts src/companion/runtimeClient.test.ts src/companion/platformClients.test.ts src/companion/platformClients.integration.test.ts + pnpm typecheck passing"
},
"companion-runtime-dispose-helper": {
"status": "completed",
"date": "2026-02-17",
"updated": "2026-02-17",
"summary": "Added `dispose()` to `CompanionRuntimeClient` as a lifecycle teardown helper that disconnects WebSocket state and clears all registered event 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/heartbeatLoop.test.ts src/companion/platformClients.test.ts src/companion/platformClients.integration.test.ts + pnpm typecheck passing"
},
"browser-tools-activation-clarity": {
"status": "completed",
"date": "2026-02-17",
+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 () => {