import { Component, OnInit, ChangeDetectionStrategy, Input } from '@angular/core'; import { Observable, combineLatest, of, timer } from 'rxjs'; import { delayWhen, filter, map, share, shareReplay, switchMap, tap, throttleTime } from 'rxjs/operators'; import { ApiService } from '../../../services/api.service'; import { Env, StateService } from '../../../services/state.service'; import { AuditStatus, CurrentPegs, FederationUtxo } from '../../../interfaces/node-api.interface'; import { WebsocketService } from '../../../services/websocket.service'; @Component({ selector: 'app-federation-utxos-list', templateUrl: './federation-utxos-list.component.html', styleUrls: ['./federation-utxos-list.component.scss'], changeDetection: ChangeDetectionStrategy.OnPush, }) export class FederationUtxosListComponent implements OnInit { @Input() widget: boolean = false; @Input() federationUtxos$: Observable; env: Env; isLoading = true; page = 1; pageSize = 15; maxSize = window.innerWidth <= 767.98 ? 3 : 5; skeletonLines: number[] = []; auditStatus$: Observable; auditUpdated$: Observable; lastReservesBlockUpdate: number = 0; currentPeg$: Observable; lastPegBlockUpdate: number = 0; lastPegAmount: string = ''; isLoad: boolean = true; constructor( private apiService: ApiService, public stateService: StateService, private websocketService: WebsocketService, ) { } ngOnInit(): void { this.isLoading = !this.widget; this.env = this.stateService.env; this.skeletonLines = this.widget === true ? [...Array(5).keys()] : [...Array(15).keys()]; if (!this.widget) { this.websocketService.want(['blocks']); this.auditStatus$ = this.stateService.blocks$.pipe( throttleTime(40000), delayWhen(_ => this.isLoad ? timer(0) : timer(2000)), tap(() => this.isLoad = false), switchMap(() => this.apiService.federationAuditSynced$()), shareReplay(1) ); this.currentPeg$ = this.auditStatus$.pipe( filter(auditStatus => auditStatus.isAuditSynced === true), switchMap(_ => this.apiService.liquidPegs$().pipe( filter((currentPegs) => currentPegs.lastBlockUpdate >= this.lastPegBlockUpdate), tap((currentPegs) => { this.lastPegBlockUpdate = currentPegs.lastBlockUpdate; }) ) ), share() ); this.auditUpdated$ = combineLatest([ this.auditStatus$, this.currentPeg$ ]).pipe( filter(([auditStatus, _]) => auditStatus.isAuditSynced === true), map(([auditStatus, currentPeg]) => ({ lastBlockAudit: auditStatus.lastBlockAudit, currentPegAmount: currentPeg.amount })), switchMap(({ lastBlockAudit, currentPegAmount }) => { const blockAuditCheck = lastBlockAudit > this.lastReservesBlockUpdate; const amountCheck = currentPegAmount !== this.lastPegAmount; this.lastReservesBlockUpdate = lastBlockAudit; this.lastPegAmount = currentPegAmount; return of(blockAuditCheck || amountCheck); }), share() ); this.federationUtxos$ = this.auditUpdated$.pipe( filter(auditUpdated => auditUpdated === true), throttleTime(40000), switchMap(_ => this.apiService.federationUtxos$()), tap(_ => this.isLoading = false), share() ); } } pageChange(page: number): void { this.page = page; } }