Skip to content

Commit 817259e

Browse files
authored
feat: Add disconnect method to event hub (#239)
Add disconnect method to event hub
1 parent e9a3310 commit 817259e

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

source/event_hub.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,6 @@ export class EventHub {
407407
}
408408
return true;
409409
});
410-
411410
return hasFoundSubscriberToRemove;
412411
}
413412

@@ -612,4 +611,14 @@ export class EventHub {
612611
});
613612
return this.publish(replyEvent);
614613
}
614+
615+
/**
616+
* Disconnect from event server.
617+
*/
618+
disconnect() {
619+
if (this._socketIo) {
620+
this._socketIo.disconnect();
621+
this._socketIo = null;
622+
}
623+
}
615624
}

test/event_hub.test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import { vi, describe, expect, beforeEach, afterEach, test } from "vitest";
44

55
describe("EventHub", () => {
66
let eventHub: any;
7-
7+
let disconnectCalled = false;
88
beforeEach(() => {
99
eventHub = new EventHub("", "", "");
10+
disconnectCalled = false;
1011
eventHub._socketIo = {
1112
on: vi.fn(),
1213
emit: vi.fn(),
1314
socket: { connected: true },
15+
disconnect: vi.fn(() => (disconnectCalled = true)),
1416
};
1517
eventHub.isConnected = vi.fn(() => true);
1618
});
@@ -239,6 +241,11 @@ describe("EventHub", () => {
239241
};
240242
expect(EventData).toEqual(expectedEvent);
241243
});
244+
245+
test("Disconnecting should disconnect the socket", () => {
246+
eventHub.disconnect();
247+
expect(disconnectCalled).toBe(true);
248+
});
242249
});
243250

244251
test("EventHub constructor", async () => {

0 commit comments

Comments
 (0)