feat(companion): add AbortSignal support for waitForEvent
This commit is contained in:
@@ -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()`, `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`) 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)
|
||||
|
||||
@@ -421,6 +421,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-wait-for-event-abort-signal": {
|
||||
"status": "completed",
|
||||
"date": "2026-02-17",
|
||||
"updated": "2026-02-17",
|
||||
"summary": "Extended `waitForEvent()` with `AbortSignal` support so companion runtimes can cancel pending event waits deterministically (in addition to timeout-based cancellation).",
|
||||
"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",
|
||||
|
||||
@@ -260,6 +260,19 @@ describe('CompanionRuntimeClient', () => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it('waitForEvent supports AbortSignal cancellation', async () => {
|
||||
const client = new CompanionRuntimeClient({
|
||||
url: 'ws://127.0.0.1:1',
|
||||
});
|
||||
const controller = new AbortController();
|
||||
|
||||
const awaited = expect(
|
||||
client.waitForEvent('agent.stream', { signal: controller.signal, timeoutMs: 10_000 }),
|
||||
).rejects.toThrow('Aborted while waiting for event agent.stream');
|
||||
controller.abort();
|
||||
await awaited;
|
||||
});
|
||||
|
||||
it('connects and performs node registration + capability discovery', async () => {
|
||||
if (!LISTEN_ALLOWED) {
|
||||
return;
|
||||
|
||||
@@ -385,34 +385,54 @@ export class CompanionRuntimeClient {
|
||||
options?: {
|
||||
timeoutMs?: number;
|
||||
predicate?: CompanionEventPredicate<TData>;
|
||||
signal?: AbortSignal;
|
||||
},
|
||||
): Promise<TData> {
|
||||
const timeoutMs = options?.timeoutMs ?? this.requestTimeoutMs;
|
||||
const predicate = options?.predicate;
|
||||
const signal = options?.signal;
|
||||
|
||||
return new Promise<TData>((resolve, reject) => {
|
||||
let settled = false;
|
||||
const unsubscribe = this.subscribeEvent<TData>(eventName, (data) => {
|
||||
if (predicate && !predicate(data)) {
|
||||
return;
|
||||
}
|
||||
let abortCleanup: (() => void) | null = null;
|
||||
|
||||
const finish = (fn: () => void) => {
|
||||
if (settled) {
|
||||
return;
|
||||
}
|
||||
settled = true;
|
||||
clearTimeout(timeout);
|
||||
unsubscribe();
|
||||
resolve(data);
|
||||
if (abortCleanup) {
|
||||
abortCleanup();
|
||||
abortCleanup = null;
|
||||
}
|
||||
fn();
|
||||
};
|
||||
|
||||
const unsubscribe = this.subscribeEvent<TData>(eventName, (data) => {
|
||||
if (predicate && !predicate(data)) {
|
||||
return;
|
||||
}
|
||||
finish(() => resolve(data));
|
||||
});
|
||||
|
||||
const timeout = setTimeout(() => {
|
||||
if (settled) {
|
||||
return;
|
||||
}
|
||||
settled = true;
|
||||
unsubscribe();
|
||||
reject(new Error(`Timed out waiting for event ${eventName}`));
|
||||
finish(() => reject(new Error(`Timed out waiting for event ${eventName}`)));
|
||||
}, timeoutMs);
|
||||
|
||||
if (signal) {
|
||||
const onAbort = () => {
|
||||
finish(() => reject(new Error(`Aborted while waiting for event ${eventName}`)));
|
||||
};
|
||||
signal.addEventListener('abort', onAbort, { once: true });
|
||||
abortCleanup = () => {
|
||||
signal.removeEventListener('abort', onAbort);
|
||||
};
|
||||
if (signal.aborted) {
|
||||
onAbort();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user