import { WatcherProxy, WebsocketPayload } from "../interfaces"; import { Observable } from "rxjs"; import { shareReplay } from "rxjs/operators"; /** * @description Watcher Proxy decorator * This class can be use to cache a watcher proxy stream so it return an existing stream when it is asked the same request multiple time */ export class CachedWatcherProxy implements WatcherProxy { private watchMap = new Map(); watch(transactionId: string, address: string, network: string, nbConf: number): Observable { const key = `${transactionId}:${address}:${network}:${nbConf}`; if (!this.watchMap.has(key)) { this.watchMap.set(key, this.watcher.watch(transactionId, address, network, nbConf).pipe(shareReplay())); } return this.watchMap.get(key); } unwatch(transactionId: string, address: string, network: string, nbConf: number): Observable { return this.watcher.unwatch(transactionId, address, network, nbConf); } constructor(private watcher: WatcherProxy) { } }