Skip to content

Commit 6870df2

Browse files
committed
fix: Refactor WebSocketSubscription and remove unused code
1 parent 409d2fb commit 6870df2

3 files changed

Lines changed: 12 additions & 20 deletions

File tree

src/adapters/ws/web-socket-subscription.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ export class WebSocketSubscription implements mastodon.streaming.Subscription {
2626
while (this.connector.canAcquire()) {
2727
this.connection = await this.connector.acquire();
2828

29-
const messages = toAsyncIterable(this.connection);
30-
3129
const data = this.serializer.serialize("json", {
3230
type: "subscribe",
3331
stream: this.stream,
@@ -37,8 +35,15 @@ export class WebSocketSubscription implements mastodon.streaming.Subscription {
3735
this.logger?.log("debug", "↑ WEBSOCKET", data);
3836
this.connection.send(data);
3937

40-
for await (const event of this.transformIntoEvents(messages)) {
41-
if (!this.matches(event)) continue;
38+
const messages = toAsyncIterable(this.connection);
39+
40+
for await (const message of messages) {
41+
const event = await this.parseMessage(message.data as string);
42+
43+
if (!this.test(event)) {
44+
continue;
45+
}
46+
4247
this.logger?.log("debug", "↓ WEBSOCKET", event);
4348
yield event;
4449
}
@@ -70,7 +75,7 @@ export class WebSocketSubscription implements mastodon.streaming.Subscription {
7075
this.unsubscribe();
7176
}
7277

73-
private matches(event: mastodon.streaming.Event): boolean {
78+
private test(event: mastodon.streaming.Event): boolean {
7479
// subscribe("hashtag", { tag: "foo" }) -> ["hashtag", "foo"]
7580
// subscribe("list", { list: "foo" }) -> ["list", "foo"]
7681
const params = this.params ?? {};
@@ -79,15 +84,6 @@ export class WebSocketSubscription implements mastodon.streaming.Subscription {
7984
return stream.every((s) => event.stream.includes(s));
8085
}
8186

82-
private async *transformIntoEvents(
83-
messages: AsyncIterable<WebSocket.MessageEvent>,
84-
) {
85-
for await (const message of messages) {
86-
const event = await this.parseMessage(message.data as string);
87-
yield event;
88-
}
89-
}
90-
9187
private async parseMessage(
9288
rawEvent: string,
9389
): Promise<mastodon.streaming.Event> {

src/utils/exponential-backoff.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class ExponentialBackoff {
2525
throw new ExponentialBackoffError(this.attempts);
2626
}
2727

28-
await sleep(this.getTimeout());
28+
await sleep(this.timeout);
2929
this.attempts++;
3030
}
3131

@@ -45,7 +45,7 @@ export class ExponentialBackoff {
4545
return this.props.maxAttempts ?? Number.POSITIVE_INFINITY;
4646
}
4747

48-
getTimeout(): number {
48+
private get timeout(): number {
4949
return this.factor * this.base ** this.attempts;
5050
}
5151
}

src/utils/noop.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
export function noop(): void {
22
//
33
}
4-
5-
export async function noopAsync(): Promise<void> {
6-
//
7-
}

0 commit comments

Comments
 (0)