@@ -20,15 +20,16 @@ export const WorkflowManager: FC<WorkflowManagerProps> = ({
2020 options,
2121 children,
2222} ) => {
23- const connectionOpen = useRef ( false ) ;
23+ const initStarted = useRef ( false ) ;
24+ const eventsStarted = useRef ( false ) ;
2425
2526 const [ client , setClient ] = useState < MqttClient | null > ( null ) ;
2627 const [ status , setStatus ] = useState < IMqttContext [ 'status' ] > ( 'offline' ) ;
2728 const [ error , setError ] = useState < IMqttContext [ 'error' ] > ( null ) ;
2829
2930 const init = useCallback ( ( ) => {
30- if ( ! client && ! connectionOpen . current ) {
31- connectionOpen . current = true ;
31+ if ( ! client && ! initStarted . current ) {
32+ initStarted . current = true ;
3233
3334 try {
3435 const mqttInstance = connect ( brokerUrl , options ) ;
@@ -47,8 +48,10 @@ export const WorkflowManager: FC<WorkflowManagerProps> = ({
4748 }
4849 } , [ brokerUrl , options , client , status ] ) ;
4950
50- const onConnected = useCallback ( ( ) => {
51- if ( client && status !== 'connected' && ! connectionOpen . current ) {
51+ const onClientDefined = useCallback ( ( ) => {
52+ if ( client && ! eventsStarted . current ) {
53+ eventsStarted . current = true ;
54+
5255 client . on ( 'end' , ( ) => {
5356 if ( status !== 'offline' ) setStatus ( 'offline' ) ;
5457 } ) ;
@@ -74,16 +77,17 @@ export const WorkflowManager: FC<WorkflowManagerProps> = ({
7477 } , [ init ] ) ;
7578
7679 useEffect ( ( ) => {
77- onConnected ( ) ;
78- } , [ onConnected ] ) ;
80+ onClientDefined ( ) ;
81+ } , [ onClientDefined ] ) ;
7982
8083 useEffect ( ( ) => {
8184 return ( ) => {
8285 if ( client ) {
8386 client . end ( ) ;
8487 setClient ( null ) ;
8588
86- connectionOpen . current = false ;
89+ initStarted . current = false ;
90+ eventsStarted . current = false ;
8791 }
8892 } ;
8993 } , [ client ] ) ;
0 commit comments