@@ -45,7 +45,7 @@ export class Subscriber extends ISubscriber {
45
45
private storagePrefix = CORE_STORAGE_PREFIX ;
46
46
private subscribeTimeout = toMiliseconds ( ONE_MINUTE ) ;
47
47
private initialSubscribeTimeout = toMiliseconds ( ONE_SECOND * 15 ) ;
48
- private restartInProgress = false ;
48
+ private restartPromise : Promise < void > | undefined ;
49
49
private clientId : string ;
50
50
private batchSubscribeTopicsLimit = 500 ;
51
51
@@ -322,7 +322,10 @@ export class Subscriber extends ISubscriber {
322
322
this . subscribeTimeout ,
323
323
"rpcBatchSubscribe failed, please try again" ,
324
324
) ;
325
- return await subscribe ;
325
+ await subscribe ;
326
+ subscriptions . forEach ( ( s ) => {
327
+ this . pending . delete ( s . topic ) ;
328
+ } ) ;
326
329
} catch ( err ) {
327
330
this . relayer . events . emit ( RELAYER_EVENTS . connection_stalled ) ;
328
331
}
@@ -448,10 +451,14 @@ export class Subscriber extends ISubscriber {
448
451
}
449
452
450
453
private restart = async ( ) => {
451
- this . restartInProgress = true ;
452
- await this . restore ( ) ;
453
- await this . onRestart ( ) ;
454
- this . restartInProgress = false ;
454
+ if ( this . restartPromise ) return ;
455
+ this . restartPromise = new Promise < void > ( async ( resolve ) => {
456
+ await this . restore ( ) ;
457
+ await this . onRestart ( ) ;
458
+ resolve ( ) ;
459
+ } ) ;
460
+ await this . restartPromise ;
461
+ this . restartPromise = undefined ;
455
462
} ;
456
463
457
464
private async persist ( ) {
@@ -470,7 +477,7 @@ export class Subscriber extends ISubscriber {
470
477
setTimeout ( async ( ) => {
471
478
await this . batchSubscribe ( batch ) ;
472
479
resolve ( ) ;
473
- } , 1000 ) ;
480
+ } , toMiliseconds ( ONE_SECOND ) ) ;
474
481
} ) ;
475
482
}
476
483
}
@@ -503,9 +510,6 @@ export class Subscriber extends ISubscriber {
503
510
subscriptions . map ( ( s ) => ( { ...s , id : this . getSubscriptionId ( s . topic ) } ) ) ,
504
511
) ;
505
512
await this . rpcBatchSubscribe ( subscriptions ) ;
506
- subscriptions . forEach ( ( s ) => {
507
- this . pending . delete ( s . topic ) ;
508
- } ) ;
509
513
}
510
514
511
515
// @ts -ignore
@@ -574,16 +578,8 @@ export class Subscriber extends ISubscriber {
574
578
if ( ! this . relayer . connected && ! this . relayer . connecting ) {
575
579
await this . relayer . transportOpen ( ) ;
576
580
}
577
- if ( ! this . restartInProgress ) return ;
578
-
579
- await new Promise < void > ( ( resolve ) => {
580
- const interval = setInterval ( ( ) => {
581
- if ( ! this . restartInProgress ) {
582
- clearInterval ( interval ) ;
583
- resolve ( ) ;
584
- }
585
- } , this . pollingInterval ) ;
586
- } ) ;
581
+ if ( ! this . restartPromise ) return ;
582
+ await this . restartPromise ;
587
583
}
588
584
589
585
private getSubscriptionId ( topic : string ) {
0 commit comments