Skip to content

Commit

Permalink
Remove failed conn. attempts from closed state
Browse files Browse the repository at this point in the history
It turns out we don't need it here after all
  • Loading branch information
justuswilhelm committed Apr 20, 2024
1 parent 568b9de commit 074c714
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 18 deletions.
1 change: 0 additions & 1 deletion __tests__/index/retryConnectionDelay.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ describe("Exponential backoff delay", () => {
instance.onclose(new CloseEvent("close"));
expect(sarus.state).toStrictEqual({
kind: "closed",
failedConnectionAttempts: 0,
});

let cb: Sarus["connect"];
Expand Down
1 change: 0 additions & 1 deletion __tests__/index/state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ describe("state machine", () => {
await server.closed;
expect(sarus.state).toStrictEqual({
kind: "closed",
failedConnectionAttempts: 0,
});

// We wait a while, and the status is "connecting" again
Expand Down
20 changes: 4 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,7 @@ export default class Sarus {
| { kind: "connecting"; failedConnectionAttempts: number }
| { kind: "connected" }
| { kind: "disconnected" }
/**
* The closed state carries of the number of failed connection attempts
*/
| { kind: "closed"; failedConnectionAttempts: number } = {
| { kind: "closed" } = {
kind: "connecting",
failedConnectionAttempts: 0,
};
Expand Down Expand Up @@ -383,18 +380,9 @@ export default class Sarus {
* Connects the WebSocket client, and attaches event listeners
*/
connect() {
if (this.state.kind === "closed") {
this.state = {
kind: "connecting",
failedConnectionAttempts: this.state.failedConnectionAttempts,
};
} else if (
this.state.kind === "connected" ||
this.state.kind === "disconnected"
) {
// If we aren't already connecting, we are now
if (this.state.kind !== "connecting") {
this.state = { kind: "connecting", failedConnectionAttempts: 0 };
} else {
// This is a NOOP, we are already connecting
}
this.ws = new WebSocket(this.url, this.protocols);
this.setBinaryType();
Expand Down Expand Up @@ -586,7 +574,7 @@ export default class Sarus {
} else {
// If we were in a different state, we assume that our connection
// freshly closed and have not made any failed connection attempts.
self.state = { kind: "closed", failedConnectionAttempts: 0 };
self.state = { kind: "closed" };
}
self.removeEventListeners();
self.reconnect();
Expand Down

0 comments on commit 074c714

Please sign in to comment.