feat(companion): add runtime dispose lifecycle helper
This commit is contained in:
@@ -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',
|
||||
|
||||
@@ -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 () => {
|
||||
|
||||
Reference in New Issue
Block a user