1- import { Component , OnInit , OnDestroy , Renderer2 , HostListener } from '@angular/core' ;
2- import { IMempoolDefaultResponse , IBlock , IProjectedBlock , ITransaction } from './interfaces' ;
3- import { retryWhen , tap } from 'rxjs/operators' ;
4- import { MemPoolService } from '../services/mem-pool.service' ;
1+ import { Component , OnInit , OnDestroy , Renderer2 } from '@angular/core' ;
2+ import { MemPoolService , ITxTracking } from '../services/mem-pool.service' ;
53import { ApiService } from '../services/api.service' ;
64import { ActivatedRoute , ParamMap } from '@angular/router' ;
5+ import { Subscription } from 'rxjs' ;
6+ import { take } from 'rxjs/operators' ;
77
88@Component ( {
99 selector : 'app-blockchain' ,
1010 templateUrl : './blockchain.component.html' ,
1111 styleUrls : [ './blockchain.component.scss' ]
1212} )
1313export class BlockchainComponent implements OnInit , OnDestroy {
14- blocks : IBlock [ ] = [ ] ;
15- projectedBlocks : IProjectedBlock [ ] = [ ] ;
16- subscription : any ;
17- socket : any ;
18- txBubbleStyle : any = { } ;
14+ txTrackingSubscription : Subscription ;
15+ blocksSubscription : Subscription ;
1916
2017 txTrackingLoading = false ;
21- txTrackingEnabled = false ;
22- txTrackingTx : ITransaction | null = null ;
23- txTrackingBlockHeight = 0 ;
2418 txShowTxNotFound = false ;
25- txBubbleArrowPosition = 'top' ;
26-
27- @HostListener ( 'window:resize' , [ '$event' ] )
28- onResize ( event : Event ) {
29- this . moveTxBubbleToPosition ( ) ;
30- }
19+ isLoading = true ;
3120
3221 constructor (
3322 private memPoolService : MemPoolService ,
@@ -37,72 +26,15 @@ export class BlockchainComponent implements OnInit, OnDestroy {
3726 ) { }
3827
3928 ngOnInit ( ) {
40-
41- this . txBubbleStyle = {
42- 'position' : 'absolute' ,
43- 'top' : '425px' ,
44- 'visibility' : 'hidden' ,
45- } ;
46- this . socket = this . apiService . websocketSubject ;
47- this . subscription = this . socket
48- . pipe (
49- retryWhen ( ( errors : any ) => errors . pipe (
50- tap ( ( ) => this . memPoolService . isOffline . next ( true ) ) ) )
51- )
52- . subscribe ( ( response : IMempoolDefaultResponse ) => {
53- this . memPoolService . isOffline . next ( false ) ;
54- if ( response . mempoolInfo && response . txPerSecond !== undefined ) {
55- this . memPoolService . loaderSubject . next ( {
56- memPoolInfo : response . mempoolInfo ,
57- txPerSecond : response . txPerSecond ,
58- vBytesPerSecond : response . vBytesPerSecond ,
59- } ) ;
60- }
61- if ( response . blocks && response . blocks . length ) {
62- this . blocks = response . blocks ;
63- this . blocks . reverse ( ) ;
64- }
65- if ( response . block ) {
66- if ( ! this . blocks . some ( ( block ) => response . block !== undefined && response . block . height === block . height ) ) {
67- this . blocks . unshift ( response . block ) ;
68- if ( this . blocks . length >= 8 ) {
69- this . blocks . pop ( ) ;
70- }
71- }
72- }
73- if ( response . conversions ) {
74- this . memPoolService . conversions . next ( response . conversions ) ;
75- }
76- if ( response . projectedBlocks ) {
77- this . projectedBlocks = response . projectedBlocks ;
78- const mempoolWeight = this . projectedBlocks . map ( ( block ) => block . blockWeight ) . reduce ( ( a , b ) => a + b ) ;
79- this . memPoolService . mempoolWeight . next ( mempoolWeight ) ;
80- }
81- if ( response [ 'track-tx' ] ) {
82- if ( response [ 'track-tx' ] . tracking ) {
83- this . txTrackingEnabled = true ;
84- this . txTrackingBlockHeight = response [ 'track-tx' ] . blockHeight ;
85- if ( response [ 'track-tx' ] . tx ) {
86- this . txTrackingTx = response [ 'track-tx' ] . tx ;
87- this . txTrackingLoading = false ;
88- }
89- } else {
90- this . txTrackingEnabled = false ;
91- this . txTrackingTx = null ;
92- this . txTrackingBlockHeight = 0 ;
93- }
94- if ( response [ 'track-tx' ] . message && response [ 'track-tx' ] . message === 'not-found' ) {
95- this . txTrackingLoading = false ;
96- this . txShowTxNotFound = true ;
97- setTimeout ( ( ) => { this . txShowTxNotFound = false ; } , 2000 ) ;
98- }
99- setTimeout ( ( ) => {
100- this . moveTxBubbleToPosition ( ) ;
101- } ) ;
29+ this . txTrackingSubscription = this . memPoolService . txTracking$
30+ . subscribe ( ( response : ITxTracking ) => {
31+ this . txTrackingLoading = false ;
32+ this . txShowTxNotFound = response . notFound ;
33+ if ( this . txShowTxNotFound ) {
34+ setTimeout ( ( ) => { this . txShowTxNotFound = false ; } , 2000 ) ;
10235 }
103- } ,
104- ( err : Error ) => console . log ( err )
105- ) ;
36+ } ) ;
37+
10638 this . renderer . addClass ( document . body , 'disable-scroll' ) ;
10739
10840 this . route . paramMap
@@ -112,73 +44,27 @@ export class BlockchainComponent implements OnInit, OnDestroy {
11244 return ;
11345 }
11446 this . txTrackingLoading = true ;
115- this . socket . next ( { 'action' : 'track-tx' , 'txId' : txId } ) ;
47+ this . apiService . sendWebSocket ( { 'action' : 'track-tx' , 'txId' : txId } ) ;
11648 } ) ;
11749
118- this . memPoolService . txIdSearch
50+ this . memPoolService . txIdSearch$
11951 . subscribe ( ( txId ) => {
12052 if ( txId ) {
12153 this . txTrackingLoading = true ;
122- this . socket . next ( { 'action' : 'track-tx' , 'txId' : txId } ) ;
54+ this . apiService . sendWebSocket ( { 'action' : 'track-tx' , 'txId' : txId } ) ;
12355 }
12456 } ) ;
125- }
126-
127- moveTxBubbleToPosition ( ) {
128- let element : HTMLElement | null = null ;
129- if ( this . txTrackingBlockHeight === 0 ) {
130- const index = this . projectedBlocks . findIndex ( ( pB ) => pB . hasMytx ) ;
131- if ( index > - 1 ) {
132- element = document . getElementById ( 'projected-block-' + index ) ;
133- } else {
134- return ;
135- }
136- } else {
137- element = document . getElementById ( 'bitcoin-block-' + this . txTrackingBlockHeight ) ;
138- }
139-
140- this . txBubbleStyle [ 'visibility' ] = 'visible' ;
141- this . txBubbleStyle [ 'position' ] = 'absolute' ;
14257
143- if ( ! element ) {
144- if ( window . innerWidth <= 768 ) {
145- this . txBubbleArrowPosition = 'bottom' ;
146- this . txBubbleStyle [ 'left' ] = window . innerWidth / 2 - 50 + 'px' ;
147- this . txBubbleStyle [ 'bottom' ] = '270px' ;
148- this . txBubbleStyle [ 'top' ] = 'inherit' ;
149- this . txBubbleStyle [ 'position' ] = 'fixed' ;
150- } else {
151- this . txBubbleStyle [ 'left' ] = window . innerWidth - 220 + 'px' ;
152- this . txBubbleArrowPosition = 'right' ;
153- this . txBubbleStyle [ 'top' ] = '425px' ;
154- }
155- } else {
156- this . txBubbleArrowPosition = 'top' ;
157- const domRect : DOMRect | ClientRect = element . getBoundingClientRect ( ) ;
158- this . txBubbleStyle [ 'left' ] = domRect . left - 50 + 'px' ;
159- this . txBubbleStyle [ 'top' ] = domRect . top + 125 + window . scrollY + 'px' ;
160-
161- if ( domRect . left + 100 > window . innerWidth ) {
162- this . txBubbleStyle [ 'left' ] = window . innerWidth - 220 + 'px' ;
163- this . txBubbleArrowPosition = 'right' ;
164- } else if ( domRect . left + 220 > window . innerWidth ) {
165- this . txBubbleStyle [ 'left' ] = window . innerWidth - 240 + 'px' ;
166- this . txBubbleArrowPosition = 'top-right' ;
167- } else {
168- this . txBubbleStyle [ 'left' ] = domRect . left + 15 + 'px' ;
169- }
170-
171- if ( domRect . left < 86 ) {
172- this . txBubbleArrowPosition = 'top-left' ;
173- this . txBubbleStyle [ 'left' ] = 125 + 'px' ;
174- }
175- }
58+ this . blocksSubscription = this . memPoolService . blocks$
59+ . pipe (
60+ take ( 1 )
61+ )
62+ . subscribe ( ( block ) => this . isLoading = false ) ;
17663 }
17764
17865 ngOnDestroy ( ) {
179- if ( this . subscription ) {
180- this . subscription . unsubscribe ( ) ;
181- }
66+ this . blocksSubscription . unsubscribe ( ) ;
67+ this . txTrackingSubscription . unsubscribe ( ) ;
18268 this . renderer . removeClass ( document . body , 'disable-scroll' ) ;
18369 }
18470}
0 commit comments