Skip to content

Commit 074c714

Browse files
committed
Remove failed conn. attempts from closed state
It turns out we don't need it here after all
1 parent 568b9de commit 074c714

File tree

3 files changed

+4
-18
lines changed

3 files changed

+4
-18
lines changed

__tests__/index/retryConnectionDelay.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ describe("Exponential backoff delay", () => {
119119
instance.onclose(new CloseEvent("close"));
120120
expect(sarus.state).toStrictEqual({
121121
kind: "closed",
122-
failedConnectionAttempts: 0,
123122
});
124123

125124
let cb: Sarus["connect"];

__tests__/index/state.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ describe("state machine", () => {
3131
await server.closed;
3232
expect(sarus.state).toStrictEqual({
3333
kind: "closed",
34-
failedConnectionAttempts: 0,
3534
});
3635

3736
// We wait a while, and the status is "connecting" again

src/index.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,7 @@ export default class Sarus {
182182
| { kind: "connecting"; failedConnectionAttempts: number }
183183
| { kind: "connected" }
184184
| { kind: "disconnected" }
185-
/**
186-
* The closed state carries of the number of failed connection attempts
187-
*/
188-
| { kind: "closed"; failedConnectionAttempts: number } = {
185+
| { kind: "closed" } = {
189186
kind: "connecting",
190187
failedConnectionAttempts: 0,
191188
};
@@ -383,18 +380,9 @@ export default class Sarus {
383380
* Connects the WebSocket client, and attaches event listeners
384381
*/
385382
connect() {
386-
if (this.state.kind === "closed") {
387-
this.state = {
388-
kind: "connecting",
389-
failedConnectionAttempts: this.state.failedConnectionAttempts,
390-
};
391-
} else if (
392-
this.state.kind === "connected" ||
393-
this.state.kind === "disconnected"
394-
) {
383+
// If we aren't already connecting, we are now
384+
if (this.state.kind !== "connecting") {
395385
this.state = { kind: "connecting", failedConnectionAttempts: 0 };
396-
} else {
397-
// This is a NOOP, we are already connecting
398386
}
399387
this.ws = new WebSocket(this.url, this.protocols);
400388
this.setBinaryType();
@@ -586,7 +574,7 @@ export default class Sarus {
586574
} else {
587575
// If we were in a different state, we assume that our connection
588576
// freshly closed and have not made any failed connection attempts.
589-
self.state = { kind: "closed", failedConnectionAttempts: 0 };
577+
self.state = { kind: "closed" };
590578
}
591579
self.removeEventListeners();
592580
self.reconnect();

0 commit comments

Comments
 (0)