1
- import { BasicPubSubClient , SingleUserPubSubClient } from "@twurple/pubsub" ;
1
+ import { BasicPubSubClient , PubSubClient } from "@twurple/pubsub" ;
2
2
import { createAuthProvider , TwitchServiceConfig } from "nodecg-io-twitch-auth" ;
3
- import { AuthProvider } from "@twurple/auth " ;
3
+ import { PubSubClientConfig } from "@twurple/pubsub/lib/PubSubClient " ;
4
4
5
- export class TwitchPubSubServiceClient extends SingleUserPubSubClient {
5
+ export class TwitchPubSubServiceClient extends PubSubClient {
6
6
private basicClient : BasicPubSubClient ;
7
- constructor ( auth : AuthProvider , basicClient : BasicPubSubClient ) {
8
- super ( { authProvider : auth , pubSubClient : basicClient } ) ;
9
- this . basicClient = basicClient ;
7
+
8
+ constructor ( config : PubSubClientConfig ) {
9
+ super ( config ) ;
10
+
11
+ // Get reference to underlying client.
12
+ // Very ugly but not possible differently.
13
+ // This is a private field and may change but we need it
14
+ // to add listeners for disconnect/connection failures
15
+ // to the underlying basic client and force connection to
16
+ // ensure valid credentials.
17
+ //@ts -expect-error private field
18
+ this . basicClient = this . _basicClient ;
10
19
}
11
20
12
21
/**
@@ -17,14 +26,23 @@ export class TwitchPubSubServiceClient extends SingleUserPubSubClient {
17
26
const authProvider = await createAuthProvider ( cfg ) ;
18
27
19
28
// Create the actual pubsub client and connect
20
- const basicClient = new BasicPubSubClient ( ) ;
21
- const pubSubClient = new TwitchPubSubServiceClient ( authProvider , basicClient ) ;
29
+ const pubSubClient = new TwitchPubSubServiceClient ( { authProvider } ) ;
22
30
23
- await basicClient . connect ( ) ;
31
+ pubSubClient . basicClient . connect ( ) ;
32
+ await new Promise ( ( resolve , reject ) => {
33
+ pubSubClient . basicClient . onConnect ( ( ) => resolve ( null ) ) ;
34
+ // 15 second timeout
35
+ setTimeout ( ( ) => reject ( "Timeout for PubSub connection was exceeded" ) , 15000 ) ;
36
+ } ) ;
24
37
return pubSubClient ;
25
38
}
26
39
27
- disconnect ( ) : Promise < void > {
28
- return this . basicClient . disconnect ( ) ;
40
+ async disconnect ( ) : Promise < void > {
41
+ this . basicClient . disconnect ( ) ;
42
+ await new Promise ( ( resolve , reject ) => {
43
+ this . basicClient . onDisconnect ( ( ) => resolve ( null ) ) ;
44
+ // 15 second timeout
45
+ setTimeout ( ( ) => reject ( "Timeout for PubSub disconnection was exceeded" ) , 15000 ) ;
46
+ } ) ;
29
47
}
30
48
}
0 commit comments