Prevent never ending loop of calls to transactionTimes
This commit is contained in:
		
							parent
							
								
									82c271267a
								
							
						
					
					
						commit
						df0f244bd1
					
				| @ -11,7 +11,9 @@ import { | |||||||
|   tap, |   tap, | ||||||
|   map, |   map, | ||||||
|   retry, |   retry, | ||||||
|   startWith |   startWith, | ||||||
|  |   repeat, | ||||||
|  |   take | ||||||
| } from 'rxjs/operators'; | } from 'rxjs/operators'; | ||||||
| import { Transaction } from '../../interfaces/electrs.interface'; | import { Transaction } from '../../interfaces/electrs.interface'; | ||||||
| import { of, merge, Subscription, Observable, Subject, from, throwError, combineLatest, BehaviorSubject } from 'rxjs'; | import { of, merge, Subscription, Observable, Subject, from, throwError, combineLatest, BehaviorSubject } from 'rxjs'; | ||||||
| @ -76,6 +78,7 @@ export class TransactionComponent implements OnInit, AfterViewInit, OnDestroy { | |||||||
|   transactionTime = -1; |   transactionTime = -1; | ||||||
|   subscription: Subscription; |   subscription: Subscription; | ||||||
|   fetchCpfpSubscription: Subscription; |   fetchCpfpSubscription: Subscription; | ||||||
|  |   transactionTimesSubscription: Subscription; | ||||||
|   fetchRbfSubscription: Subscription; |   fetchRbfSubscription: Subscription; | ||||||
|   fetchCachedTxSubscription: Subscription; |   fetchCachedTxSubscription: Subscription; | ||||||
|   fetchAccelerationSubscription: Subscription; |   fetchAccelerationSubscription: Subscription; | ||||||
| @ -106,6 +109,7 @@ export class TransactionComponent implements OnInit, AfterViewInit, OnDestroy { | |||||||
|   showCpfpDetails = false; |   showCpfpDetails = false; | ||||||
|   miningStats: MiningStats; |   miningStats: MiningStats; | ||||||
|   fetchCpfp$ = new Subject<string>(); |   fetchCpfp$ = new Subject<string>(); | ||||||
|  |   transactionTimes$ = new Subject<string>(); | ||||||
|   fetchRbfHistory$ = new Subject<string>(); |   fetchRbfHistory$ = new Subject<string>(); | ||||||
|   fetchCachedTx$ = new Subject<string>(); |   fetchCachedTx$ = new Subject<string>(); | ||||||
|   fetchAcceleration$ = new Subject<number>(); |   fetchAcceleration$ = new Subject<number>(); | ||||||
| @ -572,7 +576,7 @@ export class TransactionComponent implements OnInit, AfterViewInit, OnDestroy { | |||||||
|             if (tx.firstSeen) { |             if (tx.firstSeen) { | ||||||
|               this.transactionTime = tx.firstSeen; |               this.transactionTime = tx.firstSeen; | ||||||
|             } else { |             } else { | ||||||
|               this.getTransactionTime(); |               this.transactionTimes$.next(tx.txid); | ||||||
|             } |             } | ||||||
|           } else { |           } else { | ||||||
|             this.fetchAcceleration$.next(tx.status.block_height); |             this.fetchAcceleration$.next(tx.status.block_height); | ||||||
| @ -730,6 +734,25 @@ export class TransactionComponent implements OnInit, AfterViewInit, OnDestroy { | |||||||
|         ); |         ); | ||||||
|       }) |       }) | ||||||
|     ) |     ) | ||||||
|  | 
 | ||||||
|  |     this.transactionTimesSubscription = this.transactionTimes$.pipe( | ||||||
|  |       tap(() => { | ||||||
|  |         this.isLoadingFirstSeen = true; | ||||||
|  |       }), | ||||||
|  |       switchMap((txid) => this.apiService.getTransactionTimes$([txid]).pipe( | ||||||
|  |         retry({ count: 2, delay: 2000 }), | ||||||
|  |         // Try again until we either get a valid response, or the transaction is confirmed
 | ||||||
|  |         repeat({ delay: 2000 }), | ||||||
|  |         filter((transactionTimes) => transactionTimes?.length && transactionTimes[0] > 0 && !this.tx.status?.confirmed), | ||||||
|  |         take(1), | ||||||
|  |       )), | ||||||
|  |     ) | ||||||
|  |     .subscribe((transactionTimes) => { | ||||||
|  |       this.isLoadingFirstSeen = false; | ||||||
|  |       if (transactionTimes?.length && transactionTimes[0]) { | ||||||
|  |         this.transactionTime = transactionTimes[0]; | ||||||
|  |       } | ||||||
|  |     }); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   ngAfterViewInit(): void { |   ngAfterViewInit(): void { | ||||||
| @ -763,28 +786,6 @@ export class TransactionComponent implements OnInit, AfterViewInit, OnDestroy { | |||||||
|     return of(false); |     return of(false); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   getTransactionTime() { |  | ||||||
|     this.isLoadingFirstSeen = true; |  | ||||||
|     this.apiService |  | ||||||
|       .getTransactionTimes$([this.tx.txid]) |  | ||||||
|       .pipe( |  | ||||||
|         retry({ count: 2, delay: 2000 }), |  | ||||||
|         catchError(() => { |  | ||||||
|           this.isLoadingFirstSeen = false; |  | ||||||
|           return throwError(() => new Error('')); |  | ||||||
|         }) |  | ||||||
|       ) |  | ||||||
|       .subscribe((transactionTimes) => { |  | ||||||
|         if (transactionTimes?.length && transactionTimes[0]) { |  | ||||||
|           this.transactionTime = transactionTimes[0]; |  | ||||||
|         } else { |  | ||||||
|           setTimeout(() => { |  | ||||||
|             this.getTransactionTime(); |  | ||||||
|           }, 2000); |  | ||||||
|         } |  | ||||||
|       }); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   setCpfpInfo(cpfpInfo: CpfpInfo): void { |   setCpfpInfo(cpfpInfo: CpfpInfo): void { | ||||||
|     if (!cpfpInfo || !this.tx) { |     if (!cpfpInfo || !this.tx) { | ||||||
|       this.cpfpInfo = null; |       this.cpfpInfo = null; | ||||||
| @ -1057,6 +1058,7 @@ export class TransactionComponent implements OnInit, AfterViewInit, OnDestroy { | |||||||
|   ngOnDestroy() { |   ngOnDestroy() { | ||||||
|     this.subscription.unsubscribe(); |     this.subscription.unsubscribe(); | ||||||
|     this.fetchCpfpSubscription.unsubscribe(); |     this.fetchCpfpSubscription.unsubscribe(); | ||||||
|  |     this.transactionTimesSubscription.unsubscribe(); | ||||||
|     this.fetchRbfSubscription.unsubscribe(); |     this.fetchRbfSubscription.unsubscribe(); | ||||||
|     this.fetchCachedTxSubscription.unsubscribe(); |     this.fetchCachedTxSubscription.unsubscribe(); | ||||||
|     this.fetchAccelerationSubscription.unsubscribe(); |     this.fetchAccelerationSubscription.unsubscribe(); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user