@@ -3,6 +3,8 @@ class ConnectionMonitorState {
33 static version = '' ;
44 static commit = '' ;
55 static branch = '' ;
6+ static model = '' ;
7+ static startup = '' ;
68 static online = false ;
79
810 static getModel ( ) {
@@ -11,66 +13,85 @@ class ConnectionMonitorState {
1113 }
1214
1315 static trimModelName ( name ) {
14- // remove trailing [hash], split on / or \, return last segment, trim
1516 return name . replace ( / \s * \[ .* \] \s * $ / , '' ) . split ( / [ \\ / ] / ) . pop ( ) . trim ( ) || 'unknown model' ;
1617 }
1718
18- static setData ( { online, updated , commit , branch } ) {
19+ static setData ( { online, data } ) {
1920 this . online = online ;
20- this . version = updated ;
21- this . commit = commit ;
22- this . branch = branch ;
21+ if ( data ?. version ) this . version = data . version ;
22+ if ( data ?. commit ) this . commit = data . commit ;
23+ if ( data ?. branch ) this . branch = data . branch ;
24+ if ( data ?. model ) this . model = this . trimModelName ( data . model ) ;
2325 }
2426
25- static setElement ( el ) {
26- this . element = el ;
27- }
28-
29- static toHTML ( modelOverride ) {
27+ static toHTML ( ) {
28+ if ( ! this . model ) this . model = this . getModel ( ) ;
3029 return `
3130 Version: <b>${ this . version } </b><br>
3231 Commit: <b>${ this . commit } </b><br>
3332 Branch: <b>${ this . branch } </b><br>
3433 Status: ${ this . online ? '<b style="color:lime">online</b>' : '<b style="color:darkred">offline</b>' } <br>
35- Model: <b>${ modelOverride ? this . trimModelName ( modelOverride ) : this . getModel ( ) } </b><br>
36- Since: ${ new Date ( ) . toLocaleString ( ) } <br>
34+ Model: <b>${ this . model } </b><br>
35+ Since: ${ this . startup . toLocaleString ( ) } <br>
3736 ` ;
3837 }
3938
40- static updateState ( incomingModel ) {
41- this . element . dataset . hint = this . toHTML ( incomingModel ) ;
39+ static updateState ( ) {
40+ if ( ! this . element ) {
41+ const el = document . getElementById ( 'logo_nav' ) ;
42+ if ( el ) this . element = el ;
43+ else return ;
44+ }
45+ this . element . dataset . hint = this . toHTML ( ) ;
4246 this . element . style . backgroundColor = this . online ? 'var(--sd-main-accent-color)' : 'var(--color-error)' ;
4347 }
4448}
4549
46- let monitorAutoUpdating = false ;
47-
48- async function updateIndicator ( online , data , msg ) {
49- const el = document . getElementById ( 'logo_nav' ) ;
50- if ( ! el || ! data ) return ;
51- ConnectionMonitorState . setElement ( el ) ;
52- if ( ! monitorAutoUpdating ) {
53- monitorOption ( 'sd_model_checkpoint' , ( newVal ) => { ConnectionMonitorState . updateState ( newVal ) ; } ) ; // Runs before opt actually changes
54- monitorAutoUpdating = true ;
55- }
56- ConnectionMonitorState . setData ( { online, ...data } ) ;
50+ async function updateIndicator ( online , data = { } , msg = undefined ) {
51+ ConnectionMonitorState . setData ( { online, data } ) ;
5752 ConnectionMonitorState . updateState ( ) ;
58- if ( online ) {
59- log ( 'monitorConnection: online' , data ) ;
60- } else {
61- log ( 'monitorConnection: offline' , msg ) ;
53+ if ( msg ) log ( 'monitorConnection:' , { online, data, msg } ) ;
54+ }
55+
56+ async function wsMonitorLoop ( url ) {
57+ try {
58+ const ws = new WebSocket ( `${ url } /queue/join` ) ;
59+ ws . onopen = ( ) => { } ;
60+ ws . onmessage = ( evt ) => updateIndicator ( true ) ;
61+ ws . onclose = ( ) => setTimeout ( ( ) => wsMonitorLoop ( url ) , 10000 ) ;
62+ ws . onerror = ( e ) => {
63+ updateIndicator ( false , { } , e . message ) ;
64+ setTimeout ( ( ) => monitorConnection ( url ) , 10000 ) ; // eslint-disable-line no-use-before-define
65+ } ;
66+ } catch ( e ) {
67+ updateIndicator ( false , { } , e . message ) ;
68+ setTimeout ( ( ) => monitorConnection ( url ) , 10000 ) ; // eslint-disable-line no-use-before-define
6269 }
6370}
6471
72+ let monitorActive = false ;
73+
6574async function monitorConnection ( ) {
75+ if ( ! monitorActive ) { // start monitor loop only once on startup
76+ monitorActive = true ;
77+ monitorOption ( 'sd_model_checkpoint' , ( newVal ) => { // runs before opt actually changes
78+ ConnectionMonitorState . model = newVal ;
79+ ConnectionMonitorState . updateState ( ) ;
80+ } ) ;
81+ }
82+ ConnectionMonitorState . startup = new Date ( ) ;
83+
84+ let data = { } ;
6685 try {
6786 const res = await authFetch ( `${ window . api } /version` ) ;
68- const data = await res . json ( ) ;
69- const url = res . url . split ( '/sdapi' ) [ 0 ] . replace ( 'http' , 'ws' ) ; // update global url as ws need fqdn
70- const ws = new WebSocket ( `${ url } /queue/join` ) ;
71- ws . onopen = ( ) => updateIndicator ( true , data , '' ) ;
72- ws . onclose = ( ) => updateIndicator ( false , data , '' ) ;
73- ws . onerror = ( e ) => updateIndicator ( false , data , e . message ) ;
74- ws . onmessage = ( evt ) => log ( 'monitorConnection: message' , evt . data ) ;
75- } catch { /**/ }
87+ data = await res . json ( ) ;
88+ log ( 'monitorConnection:' , { data } ) ;
89+ ConnectionMonitorState . startup = new Date ( ) ;
90+ updateIndicator ( true , data ) ;
91+ const url = res . url . split ( '/sdapi' ) [ 0 ] . replace ( 'https:' , 'wss:' ) . replace ( 'http:' , 'ws:' ) ; // update global url as ws need fqdn
92+ wsMonitorLoop ( url ) ;
93+ } catch {
94+ monitorConnection ( false , data ) ;
95+ setTimeout ( monitorConnection , 10000 ) ;
96+ }
7697}
0 commit comments