Add electrsApiService cachedRequest function, switch outspends
This commit is contained in:
parent
8aa51c4e80
commit
09f208484a
@ -75,7 +75,7 @@ export class TransactionsListComponent implements OnInit, OnChanges {
|
||||
for (let i = 0; i < txIds.length; i += 50) {
|
||||
batches.push(txIds.slice(i, i + 50));
|
||||
}
|
||||
return forkJoin(batches.map(batch => this.electrsApiService.getOutspendsBatched$(batch)));
|
||||
return forkJoin(batches.map(batch => { return this.electrsApiService.cachedRequest(this.electrsApiService.getOutspendsBatched$, 250, batch); }));
|
||||
} else {
|
||||
return of([]);
|
||||
}
|
||||
|
@ -124,7 +124,7 @@ export class TxBowtieGraphComponent implements OnInit, OnChanges {
|
||||
.pipe(
|
||||
switchMap((txid) => {
|
||||
if (!this.cached) {
|
||||
return this.electrsApiService.getOutspendsBatched$([txid]);
|
||||
return this.electrsApiService.cachedRequest(this.electrsApiService.getOutspendsBatched$, 250, [txid]);
|
||||
} else {
|
||||
return of(null);
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { HttpClient, HttpParams } from '@angular/common/http';
|
||||
import { Observable, from, of, switchMap } from 'rxjs';
|
||||
import { BehaviorSubject, Observable, catchError, filter, from, of, shareReplay, switchMap, take, tap } from 'rxjs';
|
||||
import { Transaction, Address, Outspend, Recent, Asset, ScriptHash } from '../interfaces/electrs.interface';
|
||||
import { StateService } from './state.service';
|
||||
import { BlockExtended } from '../interfaces/node-api.interface';
|
||||
@ -13,6 +13,8 @@ export class ElectrsApiService {
|
||||
private apiBaseUrl: string; // base URL is protocol, hostname, and port
|
||||
private apiBasePath: string; // network path is /testnet, etc. or '' for mainnet
|
||||
|
||||
private requestCache = new Map<string, { subject: BehaviorSubject<any>, expiry: number }>;
|
||||
|
||||
constructor(
|
||||
private httpClient: HttpClient,
|
||||
private stateService: StateService,
|
||||
@ -30,6 +32,46 @@ export class ElectrsApiService {
|
||||
});
|
||||
}
|
||||
|
||||
private generateCacheKey(functionName: string, params: any[]): string {
|
||||
return functionName + JSON.stringify(params);
|
||||
}
|
||||
|
||||
// delete expired cache entries
|
||||
private cleanExpiredCache(): void {
|
||||
this.requestCache.forEach((value, key) => {
|
||||
if (value.expiry < Date.now()) {
|
||||
this.requestCache.delete(key);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
cachedRequest<T, F extends (...args: any[]) => Observable<T>>(
|
||||
apiFunction: F,
|
||||
expireAfter: number, // in ms
|
||||
...params: Parameters<F>
|
||||
): Observable<T> {
|
||||
this.cleanExpiredCache();
|
||||
|
||||
const cacheKey = this.generateCacheKey(apiFunction.name, params);
|
||||
if (!this.requestCache.has(cacheKey)) {
|
||||
const subject = new BehaviorSubject<T | null>(null);
|
||||
this.requestCache.set(cacheKey, { subject, expiry: Date.now() + expireAfter });
|
||||
|
||||
apiFunction.bind(this)(...params).pipe(
|
||||
tap(data => {
|
||||
subject.next(data as T);
|
||||
}),
|
||||
catchError((error) => {
|
||||
subject.error(error);
|
||||
return of(null);
|
||||
}),
|
||||
shareReplay(1),
|
||||
).subscribe();
|
||||
}
|
||||
|
||||
return this.requestCache.get(cacheKey).subject.asObservable().pipe(filter(val => val !== null), take(1));
|
||||
}
|
||||
|
||||
getBlock$(hash: string): Observable<BlockExtended> {
|
||||
return this.httpClient.get<BlockExtended>(this.apiBaseUrl + this.apiBasePath + '/api/block/' + hash);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user