Skip to content

Commit

Permalink
fix: Fix WebSocket to send subscribe event before unsubscribe
Browse files Browse the repository at this point in the history
  • Loading branch information
neet committed Oct 20, 2024
1 parent 7110ad0 commit 4e251b3
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions src/adapters/ws/web-socket-subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ export class WebSocketSubscription implements mastodon.streaming.Subscription {
) {}

async *values(): AsyncIterableIterator<mastodon.streaming.Event> {
try {
this.logger?.log("info", "Subscribing to stream", this.stream);
this.logger?.log("info", "Subscribing to stream", this.stream);

for await (this.connection of this.connector) {
const data = this.serializer.serialize("json", {
type: "subscribe",
stream: this.stream,
...this.params,
});
for await (this.connection of this.connector) {
const data = this.serializer.serialize("json", {
type: "subscribe",
stream: this.stream,
...this.params,
});

this.logger?.log("debug", "↑ WEBSOCKET", data);
this.connection.send(data);
this.counter.increment(this.stream, this.params);
this.logger?.log("debug", "↑ WEBSOCKET", data);
this.connection.send(data);
this.counter.increment(this.stream, this.params);

const messages = toAsyncIterable(this.connection);
const messages = toAsyncIterable(this.connection);

try {
for await (const message of messages) {
const event = await this.parseMessage(message.data as string);

Expand All @@ -49,9 +49,9 @@ export class WebSocketSubscription implements mastodon.streaming.Subscription {
this.logger?.log("debug", "↓ WEBSOCKET", event);
yield event;
}
} finally {
this.unsubscribe();
}
} finally {
this.unsubscribe();
}
}

Expand All @@ -69,6 +69,7 @@ export class WebSocketSubscription implements mastodon.streaming.Subscription {
...this.params,
});

this.logger?.log("debug", "↑ WEBSOCKET", data);
this.connection.send(data);
}

Expand Down

0 comments on commit 4e251b3

Please sign in to comment.