Skip to content

Commit

Permalink
feat: Add disconnect method to event hub (#239)
Browse files Browse the repository at this point in the history
Add disconnect method to event hub
  • Loading branch information
gismya committed May 23, 2024
1 parent e9a3310 commit 817259e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
11 changes: 10 additions & 1 deletion source/event_hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,6 @@ export class EventHub {
}
return true;
});

return hasFoundSubscriberToRemove;
}

Expand Down Expand Up @@ -612,4 +611,14 @@ export class EventHub {
});
return this.publish(replyEvent);
}

/**
* Disconnect from event server.
*/
disconnect() {
if (this._socketIo) {
this._socketIo.disconnect();
this._socketIo = null;
}
}
}
9 changes: 8 additions & 1 deletion test/event_hub.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { vi, describe, expect, beforeEach, afterEach, test } from "vitest";

describe("EventHub", () => {
let eventHub: any;

let disconnectCalled = false;
beforeEach(() => {
eventHub = new EventHub("", "", "");
disconnectCalled = false;
eventHub._socketIo = {
on: vi.fn(),
emit: vi.fn(),
socket: { connected: true },
disconnect: vi.fn(() => (disconnectCalled = true)),
};
eventHub.isConnected = vi.fn(() => true);
});
Expand Down Expand Up @@ -239,6 +241,11 @@ describe("EventHub", () => {
};
expect(EventData).toEqual(expectedEvent);
});

test("Disconnecting should disconnect the socket", () => {
eventHub.disconnect();
expect(disconnectCalled).toBe(true);
});
});

test("EventHub constructor", async () => {
Expand Down

0 comments on commit 817259e

Please sign in to comment.