2022-10-22 16:16:32 +00:00
import { Component , OnInit , OnDestroy } from '@angular/core' ;
2022-09-21 17:23:45 +02:00
import { WebsocketService } from '../../services/websocket.service' ;
2020-02-17 00:26:57 +07:00
import { OptimizedMempoolStats } from '../../interfaces/node-api.interface' ;
2022-09-21 17:23:45 +02:00
import { StateService } from '../../services/state.service' ;
import { ApiService } from '../../services/api.service' ;
import { SeoService } from '../../services/seo.service' ;
2022-02-02 17:49:38 +09:00
import { ActivatedRoute } from '@angular/router' ;
2022-02-10 00:04:14 +09:00
import { map , scan , startWith , switchMap , tap } from 'rxjs/operators' ;
2022-10-22 16:16:32 +00:00
import { interval , merge , Observable , Subscription } from 'rxjs' ;
2022-02-10 00:04:14 +09:00
import { ChangeDetectionStrategy } from '@angular/core' ;
2019-07-27 18:43:17 +03:00
@Component ( {
selector : 'app-television' ,
templateUrl : './television.component.html' ,
2022-02-10 00:04:14 +09:00
styleUrls : [ './television.component.scss' ] ,
changeDetection : ChangeDetectionStrategy.OnPush
2019-07-27 18:43:17 +03:00
} )
2022-10-22 16:16:32 +00:00
export class TelevisionComponent implements OnInit , OnDestroy {
2019-07-27 18:43:17 +03:00
2020-02-17 00:26:57 +07:00
mempoolStats : OptimizedMempoolStats [ ] = [ ] ;
2022-02-10 00:04:14 +09:00
statsSubscription$ : Observable < OptimizedMempoolStats [ ] > ;
2022-02-09 10:18:51 +09:00
fragment : string ;
2022-10-22 16:16:32 +00:00
timeLtrSubscription : Subscription ;
timeLtr : boolean = this . stateService . timeLtr . value ;
2019-07-27 18:43:17 +03:00
constructor (
2020-02-16 22:15:07 +07:00
private websocketService : WebsocketService ,
private apiService : ApiService ,
private stateService : StateService ,
2020-03-24 00:52:08 +07:00
private seoService : SeoService ,
2022-02-02 17:49:38 +09:00
private route : ActivatedRoute
2019-07-27 18:43:17 +03:00
) { }
2022-02-09 10:18:51 +09:00
refreshStats ( time : number , fn : Observable < OptimizedMempoolStats [ ] > ) {
return interval ( time ) . pipe ( startWith ( 0 ) , switchMap ( ( ) = > fn ) ) ;
}
2019-07-27 18:43:17 +03:00
ngOnInit() {
2020-12-03 18:34:19 +07:00
this . seoService . setTitle ( $localize ` :@@46ce8155c9ab953edeec97e8950b5a21e67d7c4e:TV view ` ) ;
2023-08-30 20:26:07 +09:00
this . seoService . setDescription ( $localize ` :@@meta.description.tv:See Bitcoin blocks and mempool congestion in real-time in a simplified format perfect for a TV. ` ) ;
2020-03-14 13:48:01 +07:00
this . websocketService . want ( [ 'blocks' , 'live-2h-chart' , 'mempool-blocks' ] ) ;
2019-08-15 14:50:05 +03:00
2022-10-22 16:16:32 +00:00
this . timeLtrSubscription = this . stateService . timeLtr . subscribe ( ( ltr ) = > {
this . timeLtr = ! ! ltr ;
} ) ;
2022-02-09 10:18:51 +09:00
this . statsSubscription $ = merge (
2022-02-10 00:04:14 +09:00
this . stateService . live2Chart $ . pipe ( map ( stats = > [ stats ] ) ) ,
2022-02-09 10:18:51 +09:00
this . route . fragment
. pipe (
2022-02-10 00:04:14 +09:00
tap ( fragment = > { this . fragment = fragment ? ? '2h' ; } ) ,
2022-02-09 10:18:51 +09:00
switchMap ( ( fragment ) = > {
const minute = 60000 ; const hour = 3600000 ;
switch ( fragment ) {
case '24h' : return this . apiService . list24HStatistics $ ( ) ;
case '1w' : return this . refreshStats ( 5 * minute , this . apiService . list1WStatistics $ ( ) ) ;
case '1m' : return this . refreshStats ( 30 * minute , this . apiService . list1MStatistics $ ( ) ) ;
case '3m' : return this . refreshStats ( 2 * hour , this . apiService . list3MStatistics $ ( ) ) ;
case '6m' : return this . refreshStats ( 3 * hour , this . apiService . list6MStatistics $ ( ) ) ;
case '1y' : return this . refreshStats ( 8 * hour , this . apiService . list1YStatistics $ ( ) ) ;
case '2y' : return this . refreshStats ( 8 * hour , this . apiService . list2YStatistics $ ( ) ) ;
case '3y' : return this . refreshStats ( 12 * hour , this . apiService . list3YStatistics $ ( ) ) ;
2022-02-10 00:04:14 +09:00
default /* 2h */ : return this . apiService . list2HStatistics $ ( ) ;
2022-02-09 10:18:51 +09:00
}
} )
)
)
. pipe (
2022-02-10 00:04:14 +09:00
scan ( ( mempoolStats , newStats ) = > {
if ( newStats . length > 1 ) {
mempoolStats = newStats ;
2022-02-09 10:18:51 +09:00
} else if ( [ '2h' , '24h' ] . includes ( this . fragment ) ) {
2022-02-10 00:04:14 +09:00
mempoolStats . unshift ( newStats [ 0 ] ) ;
mempoolStats = mempoolStats . slice ( 0 , mempoolStats . length - 1 ) ;
2022-02-09 10:18:51 +09:00
}
2022-02-10 00:04:14 +09:00
return mempoolStats ;
2022-02-09 10:18:51 +09:00
} )
) ;
2019-07-27 18:43:17 +03:00
}
2022-10-22 16:16:32 +00:00
ngOnDestroy() {
this . timeLtrSubscription . unsubscribe ( ) ;
}
2019-07-27 18:43:17 +03:00
}