Skip to content

feat: Add disconnect method to event hub #239

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor

@jimmycallin jimmycallin May 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expect(disconnectCalled).toBe(true);
expect(eventHub._socketIo.disconnect).toHaveBeenCalled();

think you can do something like this, a bit more idiomatic

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I'm setting _socketIo to undefined as part of the disconnect.

Copy link
Contributor

@jimmycallin jimmycallin May 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I guess you then could do this:

const disconnectFn = vi.fn()
...
disconnect: disconnectFn,
...
expect(disconnectFn).toHaveBeenCalled()

but we can go with this approach as well

});
});

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