@@ -25,31 +25,35 @@ export class WebSocketSubscription implements mastodon.streaming.Subscription {
2525 async * values ( ) : AsyncIterableIterator < mastodon . streaming . Event > {
2626 this . logger ?. log ( "info" , "Subscribing to stream" , this . stream ) ;
2727
28- while ( this . connector . canAcquire ( ) ) {
29- this . connection = await this . connector . acquire ( ) ;
28+ try {
29+ for await ( const connection of this . connector ) {
30+ this . connection = connection ;
3031
31- const data = this . serializer . serialize ( "json" , {
32- type : "subscribe" ,
33- stream : this . stream ,
34- ...this . params ,
35- } ) ;
32+ const data = this . serializer . serialize ( "json" , {
33+ type : "subscribe" ,
34+ stream : this . stream ,
35+ ...this . params ,
36+ } ) ;
3637
37- this . logger ?. log ( "debug" , "↑ WEBSOCKET" , data ) ;
38- this . connection . send ( data ) ;
39- this . counter . increment ( this . stream , this . params ) ;
38+ this . logger ?. log ( "debug" , "↑ WEBSOCKET" , data ) ;
39+ this . connection . send ( data ) ;
40+ this . counter . increment ( this . stream , this . params ) ;
4041
41- const messages = toAsyncIterable ( this . connection ) ;
42+ const messages = toAsyncIterable ( this . connection ) ;
4243
43- for await ( const message of messages ) {
44- const event = await this . parseMessage ( message . data as string ) ;
44+ for await ( const message of messages ) {
45+ const event = await this . parseMessage ( message . data as string ) ;
4546
46- if ( ! this . test ( event ) ) {
47- continue ;
48- }
47+ if ( ! this . test ( event ) ) {
48+ continue ;
49+ }
4950
50- this . logger ?. log ( "debug" , "↓ WEBSOCKET" , event ) ;
51- yield event ;
51+ this . logger ?. log ( "debug" , "↓ WEBSOCKET" , event ) ;
52+ yield event ;
53+ }
5254 }
55+ } finally {
56+ this . unsubscribe ( ) ;
5357 }
5458 }
5559
0 commit comments