11import { HubConnection , HubConnectionBuilder , LogLevel } from '@microsoft/signalr' ;
2+ import Server from './ApiClient/Server' ;
3+ import ServerController from './ApiClient/ServerController' ;
24
35export default class HubConnectionManager {
46
57 private _connection : HubConnection ;
68 connected : boolean = false ;
79 private connectedCallbacks : ( ( ) => void ) [ ] = [ ] ;
810 started : boolean = false ;
9- error : Error | null = null ;
11+ error : Error | null = null ;
1012
1113 constructor ( url : string ) {
1214 this . _connection = new HubConnectionBuilder ( ) . withUrl ( url , { logMessageContent : true } ) . configureLogging ( LogLevel . Trace ) . build ( ) ;
@@ -16,6 +18,7 @@ export default class HubConnectionManager {
1618 connectedCallback ( ) ;
1719 }
1820 } ) ;
21+ this . _connection . on ( "serverchanged" , this . fireServerChanged . bind ( this ) ) ;
1922 }
2023
2124 async addOnConnectedCallback ( connectedCallback : ( ) => void ) {
@@ -39,10 +42,12 @@ export default class HubConnectionManager {
3942 await this . _connection . start ( ) ;
4043 this . connected = true ;
4144
45+ this . fireServerChanged ( ) ;
4246 for ( const connectedCallback of this . connectedCallbacks ) {
4347 connectedCallback ( ) ;
4448 }
45- } catch ( e : any ) {
49+
50+ } catch ( e : any ) {
4651 this . error = e ;
4752 }
4853 }
@@ -52,7 +57,7 @@ export default class HubConnectionManager {
5257 await this . _connection . stop ( ) ;
5358 }
5459
55- onConnectionClosed ( e : Error | undefined ) {
60+ onConnectionClosed ( e : Error | undefined ) {
5661 this . connected = false ;
5762 this . started = false ;
5863 this . error = e || null ;
@@ -63,6 +68,31 @@ export default class HubConnectionManager {
6368 this . _connection . on ( eventName , handler ) ;
6469 }
6570
71+ fireServerChanged ( ...args : any [ ] ) {
72+ this . serverPromise = null ;
6673
74+ for ( let handler of this . onServerChangedHandlers ) {
75+ handler . call ( this ) ;
76+ }
77+ }
78+
79+ onServerChanged ( handler : ( ...args : any [ ] ) => void ) {
80+ this . onServerChangedHandlers . push ( handler ) ;
81+ }
82+
83+ private onServerChangedHandlers : ( ( ...args : any [ ] ) => void ) [ ] = [ ]
84+
85+ async getServer ( ) {
86+ if ( ! this . serverPromise ) {
87+ this . serverPromise = new ServerController ( ) . getServer ( ) ;
88+ }
89+
90+ return this . serverPromise ;
91+ }
92+
93+ serverPromise : Promise < Server > | null = null ;
94+
95+
96+
97+ }
6798
68- }
0 commit comments