Refactor TV component subscription

This commit is contained in:
nymkappa 2022-02-09 10:18:51 +09:00
parent aa77faf314
commit cd9eaf816b
No known key found for this signature in database
GPG Key ID: E155910B16E8BD04
2 changed files with 43 additions and 30 deletions

View File

@ -7,7 +7,7 @@
[height]="600" [height]="600"
[left]="60" [left]="60"
[right]="10" [right]="10"
[data]="mempoolStats && mempoolStats.length ? mempoolStats : null" [data]="statsSubscription$ | async"
[showZoom]="false" [showZoom]="false"
></app-mempool-graph> ></app-mempool-graph>
</div> </div>

View File

@ -5,7 +5,9 @@ import { StateService } from 'src/app/services/state.service';
import { ApiService } from 'src/app/services/api.service'; import { ApiService } from 'src/app/services/api.service';
import { SeoService } from 'src/app/services/seo.service'; import { SeoService } from 'src/app/services/seo.service';
import { ActivatedRoute } from '@angular/router'; import { ActivatedRoute } from '@angular/router';
import { switchMap } from 'rxjs/operators'; import { map, startWith, switchMap, tap } from 'rxjs/operators';
import { interval, merge, Observable } from 'rxjs';
import { isArray } from 'src/app/shared/pipes/bytes-pipe/utils';
@Component({ @Component({
selector: 'app-television', selector: 'app-television',
@ -15,7 +17,8 @@ import { switchMap } from 'rxjs/operators';
export class TelevisionComponent implements OnInit { export class TelevisionComponent implements OnInit {
mempoolStats: OptimizedMempoolStats[] = []; mempoolStats: OptimizedMempoolStats[] = [];
mempoolVsizeFeesData: any; statsSubscription$: Observable<any>;
fragment: string;
constructor( constructor(
private websocketService: WebsocketService, private websocketService: WebsocketService,
@ -25,36 +28,46 @@ export class TelevisionComponent implements OnInit {
private route: ActivatedRoute private route: ActivatedRoute
) { } ) { }
refreshStats(time: number, fn: Observable<OptimizedMempoolStats[]>) {
return interval(time).pipe(startWith(0), switchMap(() => fn));
}
ngOnInit() { ngOnInit() {
this.seoService.setTitle($localize`:@@46ce8155c9ab953edeec97e8950b5a21e67d7c4e:TV view`); this.seoService.setTitle($localize`:@@46ce8155c9ab953edeec97e8950b5a21e67d7c4e:TV view`);
this.websocketService.want(['blocks', 'live-2h-chart', 'mempool-blocks']); this.websocketService.want(['blocks', 'live-2h-chart', 'mempool-blocks']);
this.route.fragment this.statsSubscription$ = merge(
.pipe( this.stateService.live2Chart$,
switchMap((fragment) => { this.route.fragment
switch (fragment) { .pipe(
case '2h': return this.apiService.list2HStatistics$(); tap(fragment => { this.fragment = fragment; }),
case '24h': return this.apiService.list24HStatistics$(); switchMap((fragment) => {
case '1w': return this.apiService.list1WStatistics$(); const minute = 60000; const hour = 3600000;
case '1m': return this.apiService.list1MStatistics$(); switch (fragment) {
case '3m': return this.apiService.list3MStatistics$(); case '2h': return this.apiService.list2HStatistics$();
case '6m': return this.apiService.list6MStatistics$(); case '24h': return this.apiService.list24HStatistics$();
case '1y': return this.apiService.list1YStatistics$(); case '1w': return this.refreshStats(5 * minute, this.apiService.list1WStatistics$());
case '2y': return this.apiService.list2YStatistics$(); case '1m': return this.refreshStats(30 * minute, this.apiService.list1MStatistics$());
case '3y': return this.apiService.list3YStatistics$(); case '3m': return this.refreshStats(2 * hour, this.apiService.list3MStatistics$());
default: return this.apiService.list2HStatistics$(); 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$());
.subscribe((mempoolStats) => { default: return this.apiService.list2HStatistics$();
this.mempoolStats = mempoolStats; }
}); })
)
this.stateService.live2Chart$ )
.subscribe((mempoolStats) => { .pipe(
this.mempoolStats.unshift(mempoolStats); map(stats => {
this.mempoolStats = this.mempoolStats.slice(0, this.mempoolStats.length - 1); if (isArray(stats)) {
}); this.mempoolStats = stats as OptimizedMempoolStats[];
} else if (['2h', '24h'].includes(this.fragment)) {
this.mempoolStats.unshift(stats as OptimizedMempoolStats);
this.mempoolStats = this.mempoolStats.slice(0, this.mempoolStats.length - 1);
}
return this.mempoolStats;
})
);
} }
} }