@@ -42,8 +42,7 @@ const ALIASES: { [index: string]: string } = {
4242
4343const RETRY_DELAY = 2_500 ;
4444
45- const TIMEOUT_S = 60 ;
46- const TIMEOUT_MS = TIMEOUT_S * 1000 ;
45+ const DEFAULT_TIMEOUT_MS = 60 * 1000 ;
4746const TIMEOUT_INTERVAL = 5_000 ;
4847
4948const MEGABYTE = 1024 * 1024 ;
@@ -111,11 +110,14 @@ export class WsProvider implements ProviderInterface {
111110
112111 #websocket: WebSocket | null ;
113112
113+ #timeout: number ;
114+
114115 /**
115116 * @param {string | string[] } endpoint The endpoint url. Usually `ws://ip:9944` or `wss://ip:9944`, may provide an array of endpoint strings.
116117 * @param {boolean } autoConnect Whether to connect automatically or not.
118+ * @param {number } [timeout] Custom timeout value
117119 */
118- constructor ( endpoint : string | string [ ] = defaults . WS_URL , autoConnectMs : number | false = RETRY_DELAY , headers : Record < string , string > = { } ) {
120+ constructor ( endpoint : string | string [ ] = defaults . WS_URL , autoConnectMs : number | false = RETRY_DELAY , headers : Record < string , string > = { } , timeout ?: number ) {
119121 const endpoints = Array . isArray ( endpoint )
120122 ? endpoint
121123 : [ endpoint ] ;
@@ -137,6 +139,7 @@ export class WsProvider implements ProviderInterface {
137139 active : { requests : 0 , subscriptions : 0 } ,
138140 total : { bytesRecv : 0 , bytesSent : 0 , cached : 0 , requests : 0 , subscriptions : 0 , timeout : 0 }
139141 } ;
142+ this . #timeout = timeout || DEFAULT_TIMEOUT_MS ;
140143
141144 if ( autoConnectMs > 0 ) {
142145 this . connectWithRetry ( ) . catch ( ( ) : void => {
@@ -210,7 +213,7 @@ export class WsProvider implements ProviderInterface {
210213 this . #websocket. onopen = this . #onSocketOpen;
211214
212215 // timeout any handlers that have not had a response
213- this . #timeoutId = setInterval ( ( ) => this . #timeout ( ) , TIMEOUT_INTERVAL ) ;
216+ this . #timeoutId = setInterval ( ( ) => this . #timeoutHandlers ( ) , TIMEOUT_INTERVAL ) ;
214217 } catch ( error ) {
215218 l . error ( error ) ;
216219
@@ -557,16 +560,16 @@ export class WsProvider implements ProviderInterface {
557560 } ) ) . catch ( l . error ) ;
558561 } ;
559562
560- #timeout = ( ) : void => {
563+ #timeoutHandlers = ( ) : void => {
561564 const now = Date . now ( ) ;
562565 const ids = Object . keys ( this . #handlers) ;
563566
564567 for ( let i = 0 ; i < ids . length ; i ++ ) {
565568 const handler = this . #handlers[ ids [ i ] ] ;
566569
567- if ( ( now - handler . start ) > TIMEOUT_MS ) {
570+ if ( ( now - handler . start ) > this . #timeout ) {
568571 try {
569- handler . callback ( new Error ( `No response received from RPC endpoint in ${ TIMEOUT_S } s` ) , undefined ) ;
572+ handler . callback ( new Error ( `No response received from RPC endpoint in ${ this . #timeout / 1000 } s` ) , undefined ) ;
570573 } catch {
571574 // ignore
572575 }
0 commit comments