Skip to content

Commit

Permalink
connection: Rename disconect() to _closeSocket()
Browse files Browse the repository at this point in the history
Users keep using it expecting it to be a public method even though it is not documented

* #1013
* b25e478
* #974

I guess that's fair, I'll create a new `disconnect` method that is safe to use.
  • Loading branch information
sonnyp committed Jan 8, 2025
1 parent fb5ed94 commit 2877a7b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
4 changes: 2 additions & 2 deletions packages/connection/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ class Connection extends EventEmitter {
} catch {}

try {
await this.disconnect();
await this._closeSocket();
} catch (err) {
this.#onSocketClosed(true, err);
}
Expand Down Expand Up @@ -262,7 +262,7 @@ class Connection extends EventEmitter {
* https://xmpp.org/rfcs/rfc6120.html#streams-close
* https://tools.ietf.org/html/rfc7395#section-3.6
*/
async disconnect(timeout = this.timeout) {
async _closeSocket(timeout = this.timeout) {
if (!this.socket) return;

this._status("disconnecting");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ test("rejects with TimeoutError if socket doesn't close", (done) => {
const conn = new Connection();
conn.socket = new EventEmitter();
conn.socket.end = () => {};
conn.disconnect().catch((err) => {
conn._closeSocket().catch((err) => {
expect(err.name).toBe("TimeoutError");
done();
});
Expand All @@ -21,7 +21,7 @@ test("resolves", (done) => {
sock.emit("connect");
sock.end = () => {};
// eslint-disable-next-line promise/catch-or-return
conn.disconnect().then(() => {
conn._closeSocket().then(() => {
expect(conn.status).toBe("disconnect");
return done();
});
Expand All @@ -41,7 +41,7 @@ test("rejects if socket.end throws", (done) => {
throw error;
};

conn.disconnect().catch((err) => {
conn._closeSocket().catch((err) => {
expect(err).toBe(error);
done();
});
Expand All @@ -51,5 +51,5 @@ test("resolves if socket is absent", async () => {
const conn = new Connection();
conn.socket = null;

await expect(conn.disconnect()).toResolve();
await expect(conn._closeSocket()).toResolve();
});
20 changes: 10 additions & 10 deletions packages/connection/test/end.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ test("#_end", async () => {
const conn = new Connection();

const spy_close = jest.spyOn(conn, "close");
const spy_disconnect = jest.spyOn(conn, "disconnect");
const spy_closeSocket = jest.spyOn(conn, "_closeSocket");

await conn._end();

expect(spy_close).toHaveBeenCalledTimes(1);
expect(spy_disconnect).toHaveBeenCalledTimes(1);
expect(spy_closeSocket).toHaveBeenCalledTimes(1);
});

test("#_end with close rejection", async () => {
Expand All @@ -18,28 +18,28 @@ test("#_end with close rejection", async () => {
const spy_close = jest.spyOn(conn, "close").mockImplementation(() => {
return Promise.reject();
});
const spy_disconnect = jest.spyOn(conn, "disconnect");
const spy_closeSocket = jest.spyOn(conn, "_closeSocket");

await conn._end();

expect(spy_close).toHaveBeenCalledTimes(1);
expect(spy_disconnect).toHaveBeenCalledTimes(1);
expect(spy_closeSocket).toHaveBeenCalledTimes(1);
});

test("#_end with disconnect rejection", async () => {
const conn = new Connection();

const spy_close = jest.spyOn(conn, "close");
const spy_disconnect = jest
.spyOn(conn, "disconnect")
const spy_closeSocket = jest
.spyOn(conn, "_closeSocket")
.mockImplementation(() => {
return Promise.reject();
});

await conn._end();

expect(spy_close).toHaveBeenCalledTimes(1);
expect(spy_disconnect).toHaveBeenCalledTimes(1);
expect(spy_closeSocket).toHaveBeenCalledTimes(1);
});

test("#_end with close and disconnect rejection", async () => {
Expand All @@ -48,14 +48,14 @@ test("#_end with close and disconnect rejection", async () => {
const spy_close = jest.spyOn(conn, "close").mockImplementation(() => {
return Promise.reject();
});
const spy_disconnect = jest
.spyOn(conn, "disconnect")
const spy_closeSocket = jest
.spyOn(conn, "_closeSocket")
.mockImplementation(() => {
return Promise.reject();
});

await conn._end();

expect(spy_close).toHaveBeenCalledTimes(1);
expect(spy_disconnect).toHaveBeenCalledTimes(1);
expect(spy_closeSocket).toHaveBeenCalledTimes(1);
});
2 changes: 1 addition & 1 deletion test/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ test("statuses", async () => {
]);

// trigger reconnect
await xmpp.disconnect();
await xmpp._closeSocket();

statuses = [xmpp.status];
await promise(xmpp, "open");
Expand Down

0 comments on commit 2877a7b

Please sign in to comment.