Skip to content

Commit

Permalink
fix: Fix constructor types
Browse files Browse the repository at this point in the history
  • Loading branch information
neet committed Jul 27, 2023
1 parent a25b80c commit 2864d17
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 20 deletions.
4 changes: 3 additions & 1 deletion src/adapters/action/dispatcher-ws.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import { WebSocketActionDispatcher } from "./dispatcher-ws";
describe("DispatcherWs", () => {
it("throws for unknown action type", async () => {
const dispatcher = new WebSocketActionDispatcher(
new WebSocketConnectorImpl(["wss://example.com"]),
new WebSocketConnectorImpl({
constructorParameters: ["wss://example.com"],
}),
new SerializerNativeImpl(),
createLogger("error"),
);
Expand Down
11 changes: 8 additions & 3 deletions src/adapters/clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,15 @@ export function createStreamingClient(
const config = new WebSocketConfigImpl(props, serializer);
const logger = createLogger(props.logLevel);
const connector = new WebSocketConnectorImpl(
[config.resolvePath("/api/v1/streaming"), config.getProtocols()],
{
constructorParameters: [
config.resolvePath("/api/v1/streaming"),
config.getProtocols(),
],
implementation: props.implementation,
maxAttempts: config.getMaxAttempts(),
},
logger,
props.implementation,
config.getMaxAttempts(),
);
const actionDispatcher = new WebSocketActionDispatcher(
connector,
Expand Down
17 changes: 10 additions & 7 deletions src/adapters/ws/web-socket-connector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ describe("WebSocketConnector", () => {
it("returns existing connection if it exists", async () => {
const port = await getPort();
const server = new WebSocketServer({ port });
const connector = new WebSocketConnectorImpl([`ws://localhost:${port}`]);
const connector = new WebSocketConnectorImpl({
constructorParameters: [`ws://localhost:${port}`],
});

const ws1 = await connector.acquire();
const ws2 = await connector.acquire();
Expand All @@ -20,19 +22,20 @@ describe("WebSocketConnector", () => {
});

it("rejects if WebSocket closes", async () => {
const connector = new WebSocketConnectorImpl([`ws://localhost:0`]);
const connector = new WebSocketConnectorImpl({
constructorParameters: [`ws://localhost:0`],
});
const promise = connector.acquire();
connector.close();

await expect(promise).rejects.toBeInstanceOf(MastoWebSocketError);
});

it("rejects if it reaches max attempts", async () => {
const connector = new WebSocketConnectorImpl(
[`ws://localhost:0`],
undefined,
1,
);
const connector = new WebSocketConnectorImpl({
constructorParameters: [`ws://localhost:0`],
maxAttempts: 1,
});

const promise = connector.acquire();
await expect(promise).rejects.toBeInstanceOf(MastoWebSocketError);
Expand Down
19 changes: 12 additions & 7 deletions src/adapters/ws/web-socket-connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import {
import { MastoWebSocketError } from "../errors";
import { waitForClose, waitForOpen } from "./wait-for-events";

interface WebSocketConnectorImplProps {
readonly constructorParameters: ConstructorParameters<typeof WebSocket>;
readonly implementation?: unknown;
readonly maxAttempts?: number;
}

export class WebSocketConnectorImpl implements WebSocketConnector {
private ws?: WebSocket;

Expand All @@ -19,13 +25,11 @@ export class WebSocketConnectorImpl implements WebSocketConnector {
private initialized = false;

constructor(
private readonly params: ConstructorParameters<typeof WebSocket>,
private readonly props: WebSocketConnectorImplProps,
private readonly logger?: Logger,
private readonly implementation?: unknown,
private readonly maxAttempts?: number,
) {
this.backoff = new ExponentialBackoff({
maxAttempts: this.maxAttempts,
maxAttempts: this.props.maxAttempts,
});
}

Expand Down Expand Up @@ -70,8 +74,9 @@ export class WebSocketConnectorImpl implements WebSocketConnector {
try {
this.logger?.info("Connecting to WebSocket...");
{
const ctor = (this.implementation ?? WebSocket) as typeof WebSocket;
const ws = new ctor(...this.params);
const ctor = (this.props.implementation ??
WebSocket) as typeof WebSocket;
const ws = new ctor(...this.props.constructorParameters);
await waitForOpen(ws);
this.ws = ws;
}
Expand All @@ -97,7 +102,7 @@ export class WebSocketConnectorImpl implements WebSocketConnector {
for (const { reject } of this.queue) {
reject(
new MastoWebSocketError(
`Failed to connect to WebSocket after ${this.maxAttempts} attempts`,
`Failed to connect to WebSocket after ${this.props.maxAttempts} attempts`,
),
);
}
Expand Down
7 changes: 5 additions & 2 deletions src/adapters/ws/web-socket-subscription.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ describe("WebSocketSubscription", () => {
const logger = createLogger();

const subscription = new WebSocketSubscription(
new WebSocketConnectorImpl(["ws://localhost:0"], logger),
new WebSocketConnectorImpl(
{ constructorParameters: ["ws://localhost:0"] },
logger,
),
new SerializerNativeImpl(),
"public",
logger,
Expand Down Expand Up @@ -39,7 +42,7 @@ describe("WebSocketSubscription", () => {
});

const connection = new WebSocketConnectorImpl(
[`ws://localhost:${port}`],
{ constructorParameters: [`ws://localhost:${port}`] },
logger,
);
const subscription = new WebSocketSubscription(
Expand Down

0 comments on commit 2864d17

Please sign in to comment.