@@ -16,6 +16,8 @@ import ConnectionStatus from '@/components/ConnectionStatus';
1616
1717type Status = 'success' | 'in-progress' | 'error' ;
1818
19+
20+
1921interface WakuStatus {
2022 filter : Status ;
2123 store : Status ;
@@ -30,6 +32,9 @@ function App() {
3032 filter : 'in-progress' ,
3133 store : 'in-progress' ,
3234 } ) ;
35+
36+ ( global . window as any ) . waku = node ;
37+
3338 const [ telemetryOptIn , setTelemetryOptIn ] = useState < boolean | null > ( null ) ;
3439 const [ isLoadingChains , setIsLoadingChains ] = useState ( true ) ;
3540
@@ -41,10 +46,22 @@ function App() {
4146 } , [ ] ) ;
4247
4348 useEffect ( ( ) => {
44- if ( isWakuLoading || ! node || node . libp2p . getConnections ( ) . length <= 1 || chainsData . length > 0 || isListening ) return ;
49+ if ( isWakuLoading || ! node || node . libp2p . getConnections ( ) . length === 0 || chainsData . length > 0 || isListening ) {
50+ console . log ( "not starting message listening" ) ;
51+ console . log ( {
52+ isWakuLoading,
53+ node,
54+ connections : node ?. libp2p . getConnections ( ) . length ,
55+ chainsData,
56+ isListening
57+ } )
58+ return ;
59+ }
4560 setIsListening ( true ) ;
4661 console . log ( "connections" , node . libp2p . getConnections ( ) . length )
47- startMessageListening ( ) ;
62+ setTimeout ( ( ) => {
63+ startMessageListening ( ) ;
64+ } , 2000 ) ;
4865 } , [ node , isWakuLoading , wakuStatus ] )
4966
5067 const handleTelemetryOptIn = ( optIn : boolean ) => {
@@ -69,25 +86,36 @@ function App() {
6986 const startMessageListening = async ( ) => {
7087 console . log ( "Starting message listening" )
7188 console . log ( "connections" , node . libp2p . getConnections ( ) . length )
89+
90+ // Add timeout for store query
91+ const STORE_TIMEOUT = 30000 ; // 30 seconds
92+ const storeTimeout = new Promise ( ( _ , reject ) => {
93+ setTimeout ( ( ) => reject ( new Error ( 'Store query timeout' ) ) , STORE_TIMEOUT ) ;
94+ } ) ;
95+
7296 try {
7397 setWakuStatus ( prev => ( { ...prev , store : 'in-progress' } ) ) ;
7498 setIsLoadingChains ( true ) ;
7599 const messageGenerator = getMessagesFromStore ( node as LightNode ) ;
76100
77101 try {
78- for await ( const message of messageGenerator ) {
79- setChainsData ( prevChains => {
80- const blockExists = prevChains . some ( block => block . blockUUID === message . blockUUID ) ;
81- if ( blockExists ) return prevChains ;
82- return [ ...prevChains , message ] ;
83- } ) ;
84- }
102+ // Race between store query and timeout
103+ await Promise . race ( [
104+ ( async ( ) => {
105+ for await ( const message of messageGenerator ) {
106+ setChainsData ( prevChains => {
107+ const blockExists = prevChains . some ( block => block . blockUUID === message . blockUUID ) ;
108+ if ( blockExists ) return prevChains ;
109+ return [ ...prevChains , message ] ;
110+ } ) ;
111+ }
112+ } ) ( ) ,
113+ storeTimeout
114+ ] ) ;
85115 setWakuStatus ( prev => ( { ...prev , store : 'success' } ) ) ;
86116 } catch ( error ) {
87117 console . error ( "Error processing message:" , error ) ;
88- // Update store status to error when query fails
89118 setWakuStatus ( prev => ( { ...prev , store : 'error' } ) ) ;
90- // Continue processing other messages
91119 }
92120 } catch ( error ) {
93121 console . error ( "Error fetching messages from store:" , error ) ;
@@ -96,11 +124,21 @@ function App() {
96124 setIsLoadingChains ( false ) ;
97125 }
98126
127+ // Add timeout for filter subscription
128+ const FILTER_TIMEOUT = 15000 ; // 15 seconds
99129 try {
100130 setWakuStatus ( prev => ( { ...prev , filter : 'in-progress' } ) ) ;
101- await subscribeToFilter ( node as LightNode , ( message ) => {
102- handleChainUpdate ( message ) ; // Use the same function for both updates
103- } )
131+ const filterPromise = subscribeToFilter ( node as LightNode , ( message ) => {
132+ handleChainUpdate ( message ) ;
133+ } ) ;
134+
135+ await Promise . race ( [
136+ filterPromise ,
137+ new Promise ( ( _ , reject ) =>
138+ setTimeout ( ( ) => reject ( new Error ( 'Filter subscription timeout' ) ) , FILTER_TIMEOUT )
139+ )
140+ ] ) ;
141+
104142 setWakuStatus ( prev => ( { ...prev , filter : 'success' } ) ) ;
105143 } catch ( error ) {
106144 console . error ( "Error subscribing to filter:" , error ) ;
0 commit comments