mirror of
https://github.com/mempool/mempool.git
synced 2025-01-10 07:26:46 +01:00
f3cfa038d3
Blockchain block arrow.
125 lines
3.9 KiB
TypeScript
125 lines
3.9 KiB
TypeScript
import { Injectable } from '@angular/core';
|
|
import { webSocket, WebSocketSubject } from 'rxjs/webSocket';
|
|
import { WebsocketResponse } from '../interfaces/websocket.interface';
|
|
import { retryWhen, tap, delay } from 'rxjs/operators';
|
|
import { StateService } from './state.service';
|
|
import { Block } from '../interfaces/electrs.interface';
|
|
|
|
const WEB_SOCKET_PROTOCOL = (document.location.protocol === 'https:') ? 'wss:' : 'ws:';
|
|
const WEB_SOCKET_URL = WEB_SOCKET_PROTOCOL + '//' + document.location.hostname + ':8999';
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class WebsocketService {
|
|
private websocketSubject: WebSocketSubject<WebsocketResponse> = webSocket<WebsocketResponse | any>(WEB_SOCKET_URL);
|
|
private goneOffline = false;
|
|
private lastWant: string[] | null = null;
|
|
private trackingTxId: string | null = null;
|
|
private trackingAddress: string | null = null;
|
|
|
|
constructor(
|
|
private stateService: StateService,
|
|
) {
|
|
this.startSubscription();
|
|
}
|
|
|
|
startSubscription() {
|
|
this.websocketSubject.next({'action': 'init'});
|
|
this.websocketSubject
|
|
.pipe(
|
|
retryWhen((errors: any) => errors
|
|
.pipe(
|
|
tap(() => {
|
|
this.goneOffline = true;
|
|
this.websocketSubject.next({'action': 'init'});
|
|
this.stateService.isOffline$.next(true);
|
|
}),
|
|
delay(5000),
|
|
)
|
|
),
|
|
)
|
|
.subscribe((response: WebsocketResponse) => {
|
|
if (response.blocks && response.blocks.length) {
|
|
const blocks = response.blocks;
|
|
blocks.forEach((block: Block) => {
|
|
if (block.height > this.stateService.latestBlockHeight) {
|
|
this.stateService.latestBlockHeight = block.height;
|
|
this.stateService.blocks$.next(block);
|
|
}
|
|
});
|
|
}
|
|
|
|
if (response.block) {
|
|
if (response.block.height > this.stateService.latestBlockHeight) {
|
|
this.stateService.latestBlockHeight = response.block.height;
|
|
this.stateService.blocks$.next(response.block);
|
|
}
|
|
|
|
if (response.txConfirmed) {
|
|
this.trackingTxId = null;
|
|
this.stateService.txConfirmed.next(response.block);
|
|
}
|
|
}
|
|
|
|
if (response.conversions) {
|
|
this.stateService.conversions$.next(response.conversions);
|
|
}
|
|
|
|
if (response['mempool-blocks']) {
|
|
this.stateService.mempoolBlocks$.next(response['mempool-blocks']);
|
|
}
|
|
|
|
if (response['live-2h-chart']) {
|
|
this.stateService.live2Chart$.next(response['live-2h-chart']);
|
|
}
|
|
|
|
if (response.mempoolInfo) {
|
|
this.stateService.mempoolStats$.next({
|
|
memPoolInfo: response.mempoolInfo,
|
|
vBytesPerSecond: response.vBytesPerSecond,
|
|
});
|
|
}
|
|
|
|
if (this.goneOffline === true) {
|
|
this.goneOffline = false;
|
|
if (this.lastWant) {
|
|
this.want(this.lastWant);
|
|
}
|
|
if (this.trackingTxId) {
|
|
this.startTrackTransaction(this.trackingTxId);
|
|
}
|
|
if (this.trackingAddress) {
|
|
this.startTrackTransaction(this.trackingAddress);
|
|
}
|
|
this.stateService.isOffline$.next(false);
|
|
}
|
|
},
|
|
(err: Error) => {
|
|
console.log(err);
|
|
this.goneOffline = true;
|
|
console.log('Error, retrying in 10 sec');
|
|
window.setTimeout(() => this.startSubscription(), 10000);
|
|
});
|
|
}
|
|
|
|
startTrackTransaction(txId: string) {
|
|
this.websocketSubject.next({ 'track-tx': txId });
|
|
this.trackingTxId = txId;
|
|
}
|
|
|
|
startTrackAddress(address: string) {
|
|
this.websocketSubject.next({ 'track-address': address });
|
|
this.trackingAddress = address;
|
|
}
|
|
|
|
fetchStatistics(historicalDate: string) {
|
|
this.websocketSubject.next({ historicalDate });
|
|
}
|
|
|
|
want(data: string[]) {
|
|
this.websocketSubject.next({action: 'want', data: data});
|
|
this.lastWant = data;
|
|
}
|
|
}
|