@@ -13,9 +13,12 @@ const propTypes = {
1313}
1414
1515function Value ( props ) {
16- const [ pvValue , setPVValue ] = useState ( "" ) ;
16+ const [ pvValue , setPVValue ] = useState ( null ) ;
1717 const [ pvSeverity , setPVSeverity ] = useState ( "" ) ;
1818 const [ pvUnit , setPVUnit ] = useState ( "" ) ;
19+ // debounce the value display to avoid flashing "Disconnected" since web socket always sends message without value on connect
20+ const [ show , setShow ] = useState ( false ) ;
21+ const [ startTimer , setStartTimer ] = useState ( false ) ;
1922
2023 // https://github.com/robtaussig/react-use-websocket/issues/40#issuecomment-616676102
2124 // pattern for sharing web socket among components
@@ -24,19 +27,38 @@ function Value(props) {
2427 filter : message => JSON . parse ( message . data ) . pv === props . pvName ,
2528 } ) ;
2629
30+
31+ useEffect ( ( ) => {
32+ // wait to start timer until we get first message for this PV
33+ if ( ! startTimer ) {
34+ return ;
35+ }
36+ // the debouncing is only useful if we are showing "Disconnected" for non-reachable PVs
37+ if ( import . meta. env . REACT_APP_SHOW_DISCONNECTED !== "true" || show ) {
38+ return ;
39+ }
40+ const timeout = setTimeout ( ( ) => {
41+ setShow ( true )
42+ } , 500 )
43+
44+ return ( ) => clearTimeout ( timeout ) ;
45+ } , [ show , startTimer ] )
46+
47+
2748 // parse web socket message. filter on useWebSocket above means we only parse messages for this PV
2849 useEffect ( ( ) => {
2950 const jsonMessage = api . PARSE_WEBSOCKET_MSG ( lastJsonMessage , 2 ) ; // fix precision to 2 on the PV table
3051 if ( jsonMessage === null ) {
3152 return ; // unable to parse, could be invalid message type, no PV name, null lastJsonMessage
3253 }
54+ setStartTimer ( true ) ;
3355 if ( "severity" in jsonMessage ) {
3456 setPVSeverity ( jsonMessage . severity ) ;
3557 }
3658 if ( "units" in jsonMessage ) {
3759 setPVUnit ( jsonMessage . units ) ;
3860 }
39- if ( "pv_value" in jsonMessage ) {
61+ if ( jsonMessage . pv_value !== null && "pv_value" in jsonMessage ) {
4062 setPVValue ( jsonMessage . pv_value ) ;
4163 }
4264 } , [ lastJsonMessage ] ) ;
@@ -59,7 +81,7 @@ function Value(props) {
5981 }
6082
6183 const severityName = pvSeverity === "UNDEFINED" || pvSeverity === "INVALID" ? ` (${ pvSeverity } )` : null
62- if ( pvValue !== undefined ) {
84+ if ( pvValue !== undefined && pvValue !== null ) {
6385 if ( Array . isArray ( pvValue ) ) {
6486 return (
6587 < div style = { { color : textColor } } > { `[ ${ pvValue . join ( ', ' ) } ] ${ pvUnit } ` } { severityName } </ div >
@@ -70,11 +92,23 @@ function Value(props) {
7092 < div style = { { color : textColor } } > { `${ pvValue } ${ pvUnit } ` } { severityName } </ div >
7193 ) ;
7294 }
73- }
74- else {
75- return (
76- null
77- ) ;
95+ } else {
96+ if ( import . meta. env . REACT_APP_SHOW_DISCONNECTED !== "true" ) {
97+ return (
98+ < div > </ div >
99+ ) ;
100+ } else {
101+ // debounce showing "Disconnected" to avoid flashing for PVs that are actually connected
102+ if ( ! show ) {
103+ return (
104+ < div > </ div >
105+ ) ;
106+ }
107+ textColor = colors . SEV_COLORS [ "UNDEFINED" ] ;
108+ return (
109+ < div style = { { color : textColor } } > (DISCONNECTED)</ div >
110+ ) ;
111+ }
78112 }
79113 }
80114}
0 commit comments